Skip to content

Commit a4251a3

Browse files
valyalatruepele
authored andcommitted
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
1 parent fdc7fde commit a4251a3

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

lib/logstorage/stream_id.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ func (sid *streamID) reset() {
2828

2929
// marshalString returns _stream_id value for the given sid.
3030
func (sid *streamID) marshalString(dst []byte) []byte {
31-
bb := bbPool.Get()
32-
bb.B = sid.marshal(bb.B)
33-
dst = hex.AppendEncode(dst, bb.B)
34-
bbPool.Put(bb)
31+
dst = sid.tenantID.marshalString(dst)
32+
dst = sid.id.marshalString(dst)
3533
return dst
3634
}
3735

lib/logstorage/tenant_id.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ func (tid *TenantID) less(a *TenantID) bool {
4444
return tid.ProjectID < a.ProjectID
4545
}
4646

47+
func (tid *TenantID) marshalString(dst []byte) []byte {
48+
n := uint64(tid.AccountID)<<32 | uint64(tid.ProjectID)
49+
dst = marshalUint64Hex(dst, n)
50+
return dst
51+
}
52+
4753
// marshal appends the marshaled tid to dst and returns the result
4854
func (tid *TenantID) marshal(dst []byte) []byte {
4955
dst = encoding.MarshalUint32(dst, tid.AccountID)

lib/logstorage/u128.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ func (u *u128) equal(a *u128) bool {
3232
return u.hi == a.hi && u.lo == a.lo
3333
}
3434

35+
func (u *u128) marshalString(dst []byte) []byte {
36+
dst = marshalUint64Hex(dst, u.hi)
37+
dst = marshalUint64Hex(dst, u.lo)
38+
return dst
39+
}
40+
41+
func marshalUint64Hex(dst []byte, n uint64) []byte {
42+
dst = marshalByteHex(dst, byte(n>>56))
43+
dst = marshalByteHex(dst, byte(n>>48))
44+
dst = marshalByteHex(dst, byte(n>>40))
45+
dst = marshalByteHex(dst, byte(n>>32))
46+
dst = marshalByteHex(dst, byte(n>>24))
47+
dst = marshalByteHex(dst, byte(n>>16))
48+
dst = marshalByteHex(dst, byte(n>>8))
49+
dst = marshalByteHex(dst, byte(n))
50+
return dst
51+
}
52+
53+
func marshalByteHex(dst []byte, x byte) []byte {
54+
return append(dst, hexByteMap[(x>>4)&15], hexByteMap[x&15])
55+
}
56+
57+
var hexByteMap = [16]byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
58+
3559
// marshal appends the marshaled u to dst and returns the result.
3660
func (u *u128) marshal(dst []byte) []byte {
3761
dst = encoding.MarshalUint64(dst, u.hi)

0 commit comments

Comments
 (0)