From 30d0f5b73a7c729a2c244e5d6b72fe4ca61f0f24 Mon Sep 17 00:00:00 2001 From: wangminxiang Date: Fri, 22 Dec 2023 00:16:17 +0800 Subject: [PATCH] fix processing group keys --- config.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index 7c53c877..4e949073 100644 --- a/config.go +++ b/config.go @@ -85,11 +85,7 @@ func (c Config) Freeze() API { } api.processingGroup = new(singleflight.Group) - api.processingGroupKeys = &sync.Pool{ - New: func() any { - return make([]byte, 64) - }, - } + api.processingGroupKeys = &sync.Pool{} return api } @@ -297,7 +293,12 @@ func (c *frozenConfig) getProcessingEncoderFromCache(fingerprint [32]byte, rtype } func (c *frozenConfig) borrowProcessEncoderGroupKey(schema Schema, typ reflect2.Type) (key []byte) { - key = c.processingGroupKeys.Get().([]byte) + k := c.processingGroupKeys.Get() + if k != nil { + key = k.([]byte) + } else { + key = make([]byte, 64) + } fingerprint := schema.Fingerprint() copy(key[:32], fingerprint[:]) binary.LittleEndian.PutUint64(key[32:], uint64(typ.RType())) @@ -312,7 +313,12 @@ func (c *frozenConfig) borrowProcessEncoderGroupKey(schema Schema, typ reflect2. } func (c *frozenConfig) borrowProcessDecoderGroupKey(schema Schema, typ reflect2.Type) (key []byte) { - key = c.processingGroupKeys.Get().([]byte) + k := c.processingGroupKeys.Get() + if k != nil { + key = k.([]byte) + } else { + key = make([]byte, 64) + } fingerprint := schema.Fingerprint() copy(key[:32], fingerprint[:]) binary.LittleEndian.PutUint64(key[32:], uint64(typ.RType())) @@ -328,8 +334,7 @@ func (c *frozenConfig) borrowProcessDecoderGroupKey(schema Schema, typ reflect2. func (c *frozenConfig) returnProcessGroupKey(key []byte) { c.processingGroup.Forget(*(*string)(unsafe.Pointer(&key))) - c.processingGroupKeys.Put(key) - return + c.processingGroupKeys.Put(key[:]) } func (c *frozenConfig) getTagKey() string {