cue/parser: reuse commentState allocations
parser.openComments is called for practically every node being parsed,
even if there are no comments to be parsed in the input.
Given that this function allocates, this leads to GC overhead.
Reuse the allocations of commentState by keeping a stack of free ones.
Note that we also tweak the code to allow reusing its groups slice.
As measured by CUE_BENCH and benchstat, formatting the 412k lines of CUE
in https://github.com/uhthomas/automata is about 10% faster,
thanks to allocating 30% less memory:
│ old │ new │
│ sec/op │ sec/op vs base │
FmtAutomata 370.5m ± 7% 332.2m ± 14% ~ (p=0.083 n=8)
│ old │ new │
│ B/op │ B/op vs base │
FmtAutomata 292.5Mi ± 0% 206.6Mi ± 0% -29.36% (p=0.000 n=8)
│ old │ new │
│ allocs/op │ allocs/op vs base │
FmtAutomata 3.363M ± 0% 2.237M ± 0% -33.49% (p=0.000 n=8)
Similarly, a 900k line CUE data file with no comments from
https://github.com/roman-mazur/cuetf sees a much more significant
speed-up, likely due to the large amount of CUE fields being parsed:
│ old │ new │
│ sec/op │ sec/op vs base │
FmtAwsSchema 3.059 ± 2% 2.364 ± 1% -22.73% (p=0.000 n=8)
│ old │ new │
│ B/op │ B/op vs base │
FmtAwsSchema 2.355Gi ± 0% 1.547Gi ± 0% -34.30% (p=0.000 n=8)
│ old │ new │
│ allocs/op │ allocs/op vs base │
FmtAwsSchema 27.13M ± 0% 16.29M ± 0% -39.95% (p=0.000 n=8)
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I91089f2b5cffa6dc5fc84151b6cbbeb3cd5eeceb
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1225189
Reviewed-by: Matthew Sackman <matthew@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>