From bccd6e48f4f1136ff48e67d344f8aab3209c77d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 24 May 2024 19:49:06 +0100 Subject: [PATCH] allocate unstable.Parser as part of decoder (#953) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This way, calls to Unmarshal or Decoder.Decode allocate once at the start rather than twice. │ old │ new │ │ allocs/op │ allocs/op vs base │ Unmarshal/HugoFrontMatter-8 141.0 ± 0% 140.0 ± 0% -0.71% (p=0.002 n=6) --- unmarshaler.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/unmarshaler.go b/unmarshaler.go index 38cb008f..94f1113e 100644 --- a/unmarshaler.go +++ b/unmarshaler.go @@ -20,10 +20,8 @@ import ( // // It is a shortcut for Decoder.Decode() with the default options. func Unmarshal(data []byte, v interface{}) error { - p := unstable.Parser{} - p.Reset(data) - d := decoder{p: &p} - + d := decoder{} + d.p.Reset(data) return d.FromParser(v) } @@ -121,22 +119,20 @@ func (d *Decoder) Decode(v interface{}) error { return fmt.Errorf("toml: %w", err) } - p := unstable.Parser{} - p.Reset(b) dec := decoder{ - p: &p, strict: strict{ Enabled: d.strict, }, unmarshalerInterface: d.unmarshalerInterface, } + dec.p.Reset(b) return dec.FromParser(v) } type decoder struct { // Which parser instance in use for this decoding session. - p *unstable.Parser + p unstable.Parser // Flag indicating that the current expression is stashed. // If set to true, calling nextExpr will not actually pull a new expression