From f14b2c29b2effccb857de30be8f64b3b953a39f0 Mon Sep 17 00:00:00 2001 From: "liuqiang.06" Date: Wed, 11 Dec 2024 17:16:27 +0800 Subject: [PATCH] fix: reduce alloc --- compat.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compat.go b/compat.go index 819e0d4d5..6aa5eef7e 100644 --- a/compat.go +++ b/compat.go @@ -77,7 +77,12 @@ func (cfg frozenConfig) MarshalIndent(val interface{}, prefix, indent string) ([ // UnmarshalFromString is implemented by sonic func (cfg frozenConfig) UnmarshalFromString(buf string, val interface{}) error { - r := bytes.NewBufferString(buf) + return cfg.Unmarshal([]byte(buf), val) +} + +// Unmarshal is implemented by sonic +func (cfg frozenConfig) Unmarshal(buf []byte, val interface{}) error { + r := bytes.NewBuffer(buf) dec := json.NewDecoder(r) if cfg.UseNumber { dec.UseNumber() @@ -88,11 +93,6 @@ func (cfg frozenConfig) UnmarshalFromString(buf string, val interface{}) error { return dec.Decode(val) } -// Unmarshal is implemented by sonic -func (cfg frozenConfig) Unmarshal(buf []byte, val interface{}) error { - return cfg.UnmarshalFromString(string(buf), val) -} - // NewEncoder is implemented by sonic func (cfg frozenConfig) NewEncoder(writer io.Writer) Encoder { enc := json.NewEncoder(writer)