Files
mostlymatter/vendor/github.com/vmihailenco/msgpack/v5/ext.go
Agniva De Sarker ef85001523 MM-25710: Use an efficient cache serialization algorithm (#14826)
* 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
2020-06-18 17:21:39 +05:30

245 строки
4.7 KiB
Go

package msgpack
import (
"bytes"
"fmt"
"reflect"
"sync"
"github.com/vmihailenco/msgpack/v5/codes"
)
type extInfo struct {
Type reflect.Type
Decoder decoderFunc
}
var extTypes = make(map[int8]*extInfo)
var bufferPool = &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
// RegisterExt records a type, identified by a value for that type,
// under the provided id. That id will identify the concrete type of a value
// sent or received as an interface variable. Only types that will be
// transferred as implementations of interface values need to be registered.
// Expecting to be used only during initialization, it panics if the mapping
// between types and ids is not a bijection.
func RegisterExt(id int8, value interface{}) {
typ := reflect.TypeOf(value)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
ptr := reflect.PtrTo(typ)
if _, ok := extTypes[id]; ok {
panic(fmt.Errorf("msgpack: ext with id=%d is already registered", id))
}
registerExt(id, ptr, getEncoder(ptr), getDecoder(ptr))
registerExt(id, typ, getEncoder(typ), getDecoder(typ))
}
func registerExt(id int8, typ reflect.Type, enc encoderFunc, dec decoderFunc) {
if enc != nil {
typeEncMap.Store(typ, makeExtEncoder(id, enc))
}
if dec != nil {
extTypes[id] = &extInfo{
Type: typ,
Decoder: dec,
}
typeDecMap.Store(typ, makeExtDecoder(id, dec))
}
}
func (e *Encoder) EncodeExtHeader(typeID int8, length int) error {
if err := e.encodeExtLen(length); err != nil {
return err
}
if err := e.w.WriteByte(byte(typeID)); err != nil {
return err
}
return nil
}
func makeExtEncoder(typeID int8, enc encoderFunc) encoderFunc {
return func(e *Encoder, v reflect.Value) error {
buf := bufferPool.Get().(*bytes.Buffer)
defer bufferPool.Put(buf)
buf.Reset()
oldw := e.w
e.w = buf
err := enc(e, v)
e.w = oldw
if err != nil {
return err
}
err = e.EncodeExtHeader(typeID, buf.Len())
if err != nil {
return err
}
return e.write(buf.Bytes())
}
}
func makeExtDecoder(typeID int8, dec decoderFunc) decoderFunc {
return func(d *Decoder, v reflect.Value) error {
c, err := d.PeekCode()
if err != nil {
return err
}
if !codes.IsExt(c) {
return dec(d, v)
}
id, extLen, err := d.DecodeExtHeader()
if err != nil {
return err
}
if id != typeID {
return fmt.Errorf("msgpack: got ext type=%d, wanted %d", id, typeID)
}
d.extLen = extLen
return dec(d, v)
}
}
func (e *Encoder) encodeExtLen(l int) error {
switch l {
case 1:
return e.writeCode(codes.FixExt1)
case 2:
return e.writeCode(codes.FixExt2)
case 4:
return e.writeCode(codes.FixExt4)
case 8:
return e.writeCode(codes.FixExt8)
case 16:
return e.writeCode(codes.FixExt16)
}
if l < 256 {
return e.write1(codes.Ext8, uint8(l))
}
if l < 65536 {
return e.write2(codes.Ext16, uint16(l))
}
return e.write4(codes.Ext32, uint32(l))
}
func (d *Decoder) parseExtLen(c codes.Code) (int, error) {
switch c {
case codes.FixExt1:
return 1, nil
case codes.FixExt2:
return 2, nil
case codes.FixExt4:
return 4, nil
case codes.FixExt8:
return 8, nil
case codes.FixExt16:
return 16, nil
case codes.Ext8:
n, err := d.uint8()
return int(n), err
case codes.Ext16:
n, err := d.uint16()
return int(n), err
case codes.Ext32:
n, err := d.uint32()
return int(n), err
default:
return 0, fmt.Errorf("msgpack: invalid code=%x decoding ext length", c)
}
}
func (d *Decoder) extHeader(c codes.Code) (int8, int, error) {
length, err := d.parseExtLen(c)
if err != nil {
return 0, 0, err
}
typeID, err := d.readCode()
if err != nil {
return 0, 0, err
}
return int8(typeID), length, nil
}
func (d *Decoder) DecodeExtHeader() (typeID int8, length int, err error) {
c, err := d.readCode()
if err != nil {
return
}
return d.extHeader(c)
}
func (d *Decoder) extInterface(c codes.Code) (interface{}, error) {
extID, extLen, err := d.extHeader(c)
if err != nil {
return nil, err
}
info, ok := extTypes[extID]
if !ok {
return nil, fmt.Errorf("msgpack: unknown ext id=%d", extID)
}
v := reflect.New(info.Type)
d.extLen = extLen
err = info.Decoder(d, v.Elem())
d.extLen = 0
if err != nil {
return nil, err
}
return v.Interface(), nil
}
func (d *Decoder) skipExt(c codes.Code) error {
n, err := d.parseExtLen(c)
if err != nil {
return err
}
return d.skipN(n + 1)
}
func (d *Decoder) skipExtHeader(c codes.Code) error {
// Read ext type.
_, err := d.readCode()
if err != nil {
return err
}
// Read ext body len.
for i := 0; i < extHeaderLen(c); i++ {
_, err := d.readCode()
if err != nil {
return err
}
}
return nil
}
func extHeaderLen(c codes.Code) int {
switch c {
case codes.Ext8:
return 1
case codes.Ext16:
return 2
case codes.Ext32:
return 4
}
return 0
}