* MM-25710: Use an efficient cache serialization algorithm We investigate 3 packages for selecting a suitable replacement for gob encoding. The algorithm chosen was msgpack which gives a decent boost over the standard gob encoding. Any external schema dependent algorithms like protobuf, flatbuffers, avro, capn'proto were not considered as that would entail converting the model structs into separate schema objects and then code generating the Go structs. It could be done theoretically at a later stage specifically for structs which are in the hot path. This is a general solution for now. The packages considered were: - github.com/tinylib/msgp - github.com/ugorji/go/codec - github.com/vmihailenco/msgpack/v5 msgp uses code generation to generate encoding/decoding code without the reflection overhead. Theoretically, therefore this is supposed to give the fastest performance. However, a major flaw in that package is that it works only at a file/directory level, not at a package level. Therefore, for structs which are spread across multiple files, it becomes near to impossible to chase down all transitive dependencies to generate the code. Even if that's done, it fails on some complex type like xml.Name and time.Time. (See: https://github.com/tinylib/msgp/issues/274#issuecomment-643654611) Therefore, we are left with 2 choices. Both of them use the same underlying algorithm. But msgpack/v5 wraps the encoders/decoders in a sync.Pool. To make a perfect apples-apples comparison, I wrote a sync.Pool for ugorji/go/codec too and compared performance. msgpack/v5 came out to be the fastest by a small margin. benchstat master.txt ugorji.txt name old time/op new time/op delta LRU/simple=new-8 5.62µs ± 3% 3.68µs ± 2% -34.64% (p=0.000 n=10+10) LRU/complex=new-8 38.4µs ± 2% 9.1µs ± 2% -76.38% (p=0.000 n=9+9) LRU/User=new-8 75.8µs ± 2% 23.5µs ± 2% -69.01% (p=0.000 n=10+10) LRU/Post=new-8 125µs ± 2% 21µs ± 3% -82.92% (p=0.000 n=9+10) LRU/Status=new-8 27.6µs ± 1% 5.4µs ± 4% -80.34% (p=0.000 n=10+10) name old alloc/op new alloc/op delta LRU/simple=new-8 3.20kB ± 0% 1.60kB ± 0% -49.97% (p=0.000 n=10+10) LRU/complex=new-8 15.7kB ± 0% 4.4kB ± 0% -71.89% (p=0.000 n=9+10) LRU/User=new-8 33.5kB ± 0% 9.2kB ± 0% -72.48% (p=0.000 n=10+8) LRU/Post=new-8 38.7kB ± 0% 4.8kB ± 0% -87.48% (p=0.000 n=10+10) LRU/Status=new-8 10.6kB ± 0% 1.7kB ± 0% -83.50% (p=0.000 n=10+10) name old allocs/op new allocs/op delta LRU/simple=new-8 46.0 ± 0% 20.0 ± 0% -56.52% (p=0.000 n=10+10) LRU/complex=new-8 324 ± 0% 48 ± 0% -85.19% (p=0.000 n=10+10) LRU/User=new-8 622 ± 0% 108 ± 0% -82.64% (p=0.000 n=10+10) LRU/Post=new-8 902 ± 0% 74 ± 0% -91.80% (p=0.000 n=10+10) LRU/Status=new-8 242 ± 0% 22 ± 0% -90.91% (p=0.000 n=10+10) 11:31:48-~/mattermost/mattermost-server/services/cache2$benchstat master.txt vmi.txt name old time/op new time/op delta LRU/simple=new-8 5.62µs ± 3% 3.68µs ± 3% -34.59% (p=0.000 n=10+10) LRU/complex=new-8 38.4µs ± 2% 8.7µs ± 3% -77.45% (p=0.000 n=9+10) LRU/User=new-8 75.8µs ± 2% 20.9µs ± 1% -72.45% (p=0.000 n=10+10) LRU/Post=new-8 125µs ± 2% 21µs ± 2% -83.08% (p=0.000 n=9+10) LRU/Status=new-8 27.6µs ± 1% 5.1µs ± 3% -81.66% (p=0.000 n=10+10) name old alloc/op new alloc/op delta LRU/simple=new-8 3.20kB ± 0% 1.60kB ± 0% -49.89% (p=0.000 n=10+10) LRU/complex=new-8 15.7kB ± 0% 4.6kB ± 0% -70.87% (p=0.000 n=9+8) LRU/User=new-8 33.5kB ± 0% 10.3kB ± 0% -69.40% (p=0.000 n=10+9) LRU/Post=new-8 38.7kB ± 0% 6.0kB ± 0% -84.62% (p=0.000 n=10+10) LRU/Status=new-8 10.6kB ± 0% 1.9kB ± 0% -82.41% (p=0.000 n=10+10) name old allocs/op new allocs/op delta LRU/simple=new-8 46.0 ± 0% 20.0 ± 0% -56.52% (p=0.000 n=10+10) LRU/complex=new-8 324 ± 0% 46 ± 0% -85.80% (p=0.000 n=10+10) LRU/User=new-8 622 ± 0% 106 ± 0% -82.96% (p=0.000 n=10+10) LRU/Post=new-8 902 ± 0% 89 ± 0% -90.13% (p=0.000 n=10+10) LRU/Status=new-8 242 ± 0% 23 ± 0% -90.50% (p=0.000 n=10+10) In general, we can see that the time to marshal/unmarshal pays off as the size of the struct increases. We can see that msgpack/v5 is faster for CPU but very slightly heavier on memory. Since we are interested in fastest speed, we choose msgpack/v5. As a future optimization, we can use a mix of msgpack and msgp for hot structs. To do that, we would need to shuffle around some code so that for the hot struct, all its dependencies are in the same file. Let's use this in production for some time, watch grafana graphs for the hottest caches and come back to optimizing this more once we have more data. Side note: we have to do with micro-benchmarks for the time being, because all the caches aren't migrated to cache2 interface yet. Once that's in, we can actually run some load tests and do comparisons. * Bring back missing import * Fix tests
245 строки
6.0 KiB
Go
245 строки
6.0 KiB
Go
package msgpack
|
|
|
|
import (
|
|
"math"
|
|
"reflect"
|
|
|
|
"github.com/vmihailenco/msgpack/v5/codes"
|
|
)
|
|
|
|
// EncodeUint8 encodes an uint8 in 2 bytes preserving type of the number.
|
|
func (e *Encoder) EncodeUint8(n uint8) error {
|
|
return e.write1(codes.Uint8, n)
|
|
}
|
|
|
|
func (e *Encoder) encodeUint8Cond(n uint8) error {
|
|
if e.flags&useCompactIntsFlag != 0 {
|
|
return e.EncodeUint(uint64(n))
|
|
}
|
|
return e.EncodeUint8(n)
|
|
}
|
|
|
|
// EncodeUint16 encodes an uint16 in 3 bytes preserving type of the number.
|
|
func (e *Encoder) EncodeUint16(n uint16) error {
|
|
return e.write2(codes.Uint16, n)
|
|
}
|
|
|
|
func (e *Encoder) encodeUint16Cond(n uint16) error {
|
|
if e.flags&useCompactIntsFlag != 0 {
|
|
return e.EncodeUint(uint64(n))
|
|
}
|
|
return e.EncodeUint16(n)
|
|
}
|
|
|
|
// EncodeUint32 encodes an uint16 in 5 bytes preserving type of the number.
|
|
func (e *Encoder) EncodeUint32(n uint32) error {
|
|
return e.write4(codes.Uint32, n)
|
|
}
|
|
|
|
func (e *Encoder) encodeUint32Cond(n uint32) error {
|
|
if e.flags&useCompactIntsFlag != 0 {
|
|
return e.EncodeUint(uint64(n))
|
|
}
|
|
return e.EncodeUint32(n)
|
|
}
|
|
|
|
// EncodeUint64 encodes an uint16 in 9 bytes preserving type of the number.
|
|
func (e *Encoder) EncodeUint64(n uint64) error {
|
|
return e.write8(codes.Uint64, n)
|
|
}
|
|
|
|
func (e *Encoder) encodeUint64Cond(n uint64) error {
|
|
if e.flags&useCompactIntsFlag != 0 {
|
|
return e.EncodeUint(n)
|
|
}
|
|
return e.EncodeUint64(n)
|
|
}
|
|
|
|
// EncodeInt8 encodes an int8 in 2 bytes preserving type of the number.
|
|
func (e *Encoder) EncodeInt8(n int8) error {
|
|
return e.write1(codes.Int8, uint8(n))
|
|
}
|
|
|
|
func (e *Encoder) encodeInt8Cond(n int8) error {
|
|
if e.flags&useCompactIntsFlag != 0 {
|
|
return e.EncodeInt(int64(n))
|
|
}
|
|
return e.EncodeInt8(n)
|
|
}
|
|
|
|
// EncodeInt16 encodes an int16 in 3 bytes preserving type of the number.
|
|
func (e *Encoder) EncodeInt16(n int16) error {
|
|
return e.write2(codes.Int16, uint16(n))
|
|
}
|
|
|
|
func (e *Encoder) encodeInt16Cond(n int16) error {
|
|
if e.flags&useCompactIntsFlag != 0 {
|
|
return e.EncodeInt(int64(n))
|
|
}
|
|
return e.EncodeInt16(n)
|
|
}
|
|
|
|
// EncodeInt32 encodes an int32 in 5 bytes preserving type of the number.
|
|
func (e *Encoder) EncodeInt32(n int32) error {
|
|
return e.write4(codes.Int32, uint32(n))
|
|
}
|
|
|
|
func (e *Encoder) encodeInt32Cond(n int32) error {
|
|
if e.flags&useCompactIntsFlag != 0 {
|
|
return e.EncodeInt(int64(n))
|
|
}
|
|
return e.EncodeInt32(n)
|
|
}
|
|
|
|
// EncodeInt64 encodes an int64 in 9 bytes preserving type of the number.
|
|
func (e *Encoder) EncodeInt64(n int64) error {
|
|
return e.write8(codes.Int64, uint64(n))
|
|
}
|
|
|
|
func (e *Encoder) encodeInt64Cond(n int64) error {
|
|
if e.flags&useCompactIntsFlag != 0 {
|
|
return e.EncodeInt(n)
|
|
}
|
|
return e.EncodeInt64(n)
|
|
}
|
|
|
|
// EncodeUnsignedNumber encodes an uint64 in 1, 2, 3, 5, or 9 bytes.
|
|
// Type of the number is lost during encoding.
|
|
func (e *Encoder) EncodeUint(n uint64) error {
|
|
if n <= math.MaxInt8 {
|
|
return e.w.WriteByte(byte(n))
|
|
}
|
|
if n <= math.MaxUint8 {
|
|
return e.EncodeUint8(uint8(n))
|
|
}
|
|
if n <= math.MaxUint16 {
|
|
return e.EncodeUint16(uint16(n))
|
|
}
|
|
if n <= math.MaxUint32 {
|
|
return e.EncodeUint32(uint32(n))
|
|
}
|
|
return e.EncodeUint64(n)
|
|
}
|
|
|
|
// EncodeNumber encodes an int64 in 1, 2, 3, 5, or 9 bytes.
|
|
// Type of the number is lost during encoding.
|
|
func (e *Encoder) EncodeInt(n int64) error {
|
|
if n >= 0 {
|
|
return e.EncodeUint(uint64(n))
|
|
}
|
|
if n >= int64(int8(codes.NegFixedNumLow)) {
|
|
return e.w.WriteByte(byte(n))
|
|
}
|
|
if n >= math.MinInt8 {
|
|
return e.EncodeInt8(int8(n))
|
|
}
|
|
if n >= math.MinInt16 {
|
|
return e.EncodeInt16(int16(n))
|
|
}
|
|
if n >= math.MinInt32 {
|
|
return e.EncodeInt32(int32(n))
|
|
}
|
|
return e.EncodeInt64(n)
|
|
}
|
|
|
|
func (e *Encoder) EncodeFloat32(n float32) error {
|
|
if e.flags&useCompactFloatsFlag != 0 {
|
|
if float32(int64(n)) == n {
|
|
return e.EncodeInt(int64(n))
|
|
}
|
|
}
|
|
return e.write4(codes.Float, math.Float32bits(n))
|
|
}
|
|
|
|
func (e *Encoder) EncodeFloat64(n float64) error {
|
|
if e.flags&useCompactFloatsFlag != 0 {
|
|
// Both NaN and Inf convert to int64(-0x8000000000000000)
|
|
// If n is NaN then it never compares true with any other value
|
|
// If n is Inf then it doesn't convert from int64 back to +/-Inf
|
|
// In both cases the comparison works.
|
|
if float64(int64(n)) == n {
|
|
return e.EncodeInt(int64(n))
|
|
}
|
|
}
|
|
return e.write8(codes.Double, math.Float64bits(n))
|
|
}
|
|
|
|
func (e *Encoder) write1(code codes.Code, n uint8) error {
|
|
e.buf = e.buf[:2]
|
|
e.buf[0] = byte(code)
|
|
e.buf[1] = n
|
|
return e.write(e.buf)
|
|
}
|
|
|
|
func (e *Encoder) write2(code codes.Code, n uint16) error {
|
|
e.buf = e.buf[:3]
|
|
e.buf[0] = byte(code)
|
|
e.buf[1] = byte(n >> 8)
|
|
e.buf[2] = byte(n)
|
|
return e.write(e.buf)
|
|
}
|
|
|
|
func (e *Encoder) write4(code codes.Code, n uint32) error {
|
|
e.buf = e.buf[:5]
|
|
e.buf[0] = byte(code)
|
|
e.buf[1] = byte(n >> 24)
|
|
e.buf[2] = byte(n >> 16)
|
|
e.buf[3] = byte(n >> 8)
|
|
e.buf[4] = byte(n)
|
|
return e.write(e.buf)
|
|
}
|
|
|
|
func (e *Encoder) write8(code codes.Code, n uint64) error {
|
|
e.buf = e.buf[:9]
|
|
e.buf[0] = byte(code)
|
|
e.buf[1] = byte(n >> 56)
|
|
e.buf[2] = byte(n >> 48)
|
|
e.buf[3] = byte(n >> 40)
|
|
e.buf[4] = byte(n >> 32)
|
|
e.buf[5] = byte(n >> 24)
|
|
e.buf[6] = byte(n >> 16)
|
|
e.buf[7] = byte(n >> 8)
|
|
e.buf[8] = byte(n)
|
|
return e.write(e.buf)
|
|
}
|
|
|
|
func encodeUint8CondValue(e *Encoder, v reflect.Value) error {
|
|
return e.encodeUint8Cond(uint8(v.Uint()))
|
|
}
|
|
|
|
func encodeUint16CondValue(e *Encoder, v reflect.Value) error {
|
|
return e.encodeUint16Cond(uint16(v.Uint()))
|
|
}
|
|
|
|
func encodeUint32CondValue(e *Encoder, v reflect.Value) error {
|
|
return e.encodeUint32Cond(uint32(v.Uint()))
|
|
}
|
|
|
|
func encodeUint64CondValue(e *Encoder, v reflect.Value) error {
|
|
return e.encodeUint64Cond(v.Uint())
|
|
}
|
|
|
|
func encodeInt8CondValue(e *Encoder, v reflect.Value) error {
|
|
return e.encodeInt8Cond(int8(v.Int()))
|
|
}
|
|
|
|
func encodeInt16CondValue(e *Encoder, v reflect.Value) error {
|
|
return e.encodeInt16Cond(int16(v.Int()))
|
|
}
|
|
|
|
func encodeInt32CondValue(e *Encoder, v reflect.Value) error {
|
|
return e.encodeInt32Cond(int32(v.Int()))
|
|
}
|
|
|
|
func encodeInt64CondValue(e *Encoder, v reflect.Value) error {
|
|
return e.encodeInt64Cond(v.Int())
|
|
}
|
|
|
|
func encodeFloat32Value(e *Encoder, v reflect.Value) error {
|
|
return e.EncodeFloat32(float32(v.Float()))
|
|
}
|
|
|
|
func encodeFloat64Value(e *Encoder, v reflect.Value) error {
|
|
return e.EncodeFloat64(v.Float())
|
|
}
|