MM-27456: Use reflect-free serialization for hot structs (#15171)

Automatic Merge
Этот коммит содержится в:
Agniva De Sarker
2020-08-13 13:05:57 +05:30
коммит произвёл GitHub
родитель 32b7d2b5f1
Коммит 91a76b2df9
34 изменённых файлов: 6224 добавлений и 6 удалений

48
services/cache/lru.go поставляемый
Просмотреть файл

@@ -8,6 +8,8 @@ import (
"sync"
"time"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/tinylib/msgp/msgp"
"github.com/vmihailenco/msgpack/v5"
)
@@ -141,9 +143,21 @@ func (l *LRU) set(key string, value interface{}, ttl time.Duration) error {
expires = time.Now().Add(ttl)
}
buf, err := msgpack.Marshal(value)
if err != nil {
return err
var buf []byte
var err error
// We use a fast path for hot structs.
if msgpVal, ok := value.(msgp.Marshaler); ok {
buf, err = msgpVal.MarshalMsg(nil)
if err != nil {
return err
}
} else {
// Slow path for other structs.
buf, err = msgpack.Marshal(value)
if err != nil {
return err
}
}
// Check for existing item, ignoring expiry since we'd update anyway.
@@ -182,6 +196,34 @@ func (l *LRU) get(key string, value interface{}) error {
l.evictList.MoveToFront(ent)
// We use a fast path for hot structs.
if msgpVal, ok := value.(msgp.Unmarshaler); ok {
_, err := msgpVal.UnmarshalMsg(e.value)
return err
}
// This is ugly and makes the cache package aware of the model package.
// But this is due to 2 things.
// 1. The msgp package works on methods on structs rather than functions.
// 2. Our cache interface passes pointers to empty pointers, and not pointers
// to values. This is mainly how all our model structs are passed around.
// It might be technically possible to use values _just_ for hot structs
// like these and then return a pointer while returning from the cache function,
// but it will make the codebase inconsistent, and has some edge-cases to take care of.
switch v := value.(type) {
case **model.User:
var u model.User
_, err := u.UnmarshalMsg(e.value)
*v = &u
return err
case **model.Session:
var s model.Session
_, err := s.UnmarshalMsg(e.value)
*v = &s
return err
}
// Slow path for other structs.
return msgpack.Unmarshal(e.value, value)
}
return ErrKeyNotFound