Skip to content

Commit

Permalink
allocate unstable.Parser as part of decoder (#953)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
mvdan authored May 24, 2024
1 parent 9b890cf commit bccd6e4
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bccd6e4

Please sign in to comment.