lib/logstorage: improve performance for streamID.marshalString() by more than 2x

The streamID.marshalString() is executed in hot path if the query selects _stream_id field.

Command to run the benchmark:

go test ./lib/logstorage/ -run=NONE -bench=BenchmarkStreamIDMarshalString -benchtime=5s

Results before the commit:

BenchmarkStreamIDMarshalString-16    	438480714	        14.04 ns/op	  71.23 MB/s	       0 B/op	       0 allocs/op

Results after the commit:

BenchmarkStreamIDMarshalString-16    	982459660	         6.049 ns/op	 165.30 MB/s	       0 B/op	       0 allocs/op
This commit is contained in:
Aliaksandr Valialkin 2024-09-24 18:33:23 +02:00
parent 919d2dc90e
commit f86e093b20
No known key found for this signature in database
GPG Key ID: 52C003EE2BCDB9EB
3 changed files with 32 additions and 4 deletions

View File

@ -28,10 +28,8 @@ func (sid *streamID) reset() {
// marshalString returns _stream_id value for the given sid.
func (sid *streamID) marshalString(dst []byte) []byte {
bb := bbPool.Get()
bb.B = sid.marshal(bb.B)
dst = hex.AppendEncode(dst, bb.B)
bbPool.Put(bb)
dst = sid.tenantID.marshalString(dst)
dst = sid.id.marshalString(dst)
return dst
}

View File

@ -44,6 +44,12 @@ func (tid *TenantID) less(a *TenantID) bool {
return tid.ProjectID < a.ProjectID
}
func (tid *TenantID) marshalString(dst []byte) []byte {
n := uint64(tid.AccountID)<<32 | uint64(tid.ProjectID)
dst = marshalUint64Hex(dst, n)
return dst
}
// marshal appends the marshaled tid to dst and returns the result
func (tid *TenantID) marshal(dst []byte) []byte {
dst = encoding.MarshalUint32(dst, tid.AccountID)

View File

@ -32,6 +32,30 @@ func (u *u128) equal(a *u128) bool {
return u.hi == a.hi && u.lo == a.lo
}
func (u *u128) marshalString(dst []byte) []byte {
dst = marshalUint64Hex(dst, u.hi)
dst = marshalUint64Hex(dst, u.lo)
return dst
}
func marshalUint64Hex(dst []byte, n uint64) []byte {
dst = marshalByteHex(dst, byte(n>>56))
dst = marshalByteHex(dst, byte(n>>48))
dst = marshalByteHex(dst, byte(n>>40))
dst = marshalByteHex(dst, byte(n>>32))
dst = marshalByteHex(dst, byte(n>>24))
dst = marshalByteHex(dst, byte(n>>16))
dst = marshalByteHex(dst, byte(n>>8))
dst = marshalByteHex(dst, byte(n))
return dst
}
func marshalByteHex(dst []byte, x byte) []byte {
return append(dst, hexByteMap[(x>>4)&15], hexByteMap[x&15])
}
var hexByteMap = [16]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
// marshal appends the marshaled u to dst and returns the result.
func (u *u128) marshal(dst []byte) []byte {
dst = encoding.MarshalUint64(dst, u.hi)