cue/ast/astutil: avoid walking to the root to reuse scopes
As suggested by Matthew, this can avoid some overhead when having
more than a few levels of scope nesting. Plus, a pointer is smaller
than a slice, so each scope is a bit smaller.
Even with the AWS schema, which tops at a scope depth of 14,
this does lead to a measurable savings in CPU cost.
On the flip side, dealing with pointers makes the code a bit trickier,
but overall it's a coin toss as we can remove two methods.
│ old │ mid │
│ sec/op │ sec/op vs base │
FmtAwsSchema 2.210 ± 1% 2.167 ± 1% -1.91% (p=0.001 n=8)
│ old │ mid │
│ B/op │ B/op vs base │
FmtAwsSchema 1.064Gi ± 0% 1.064Gi ± 0% -0.00% (p=0.005 n=8)
│ old │ mid │
│ allocs/op │ allocs/op vs base │
FmtAwsSchema 12.92M ± 0% 12.92M ± 0% ~ (p=0.063 n=8)
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iea3af538bc94da349456f9e9918052a0e6ae192b
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1225195
Reviewed-by: Matthew Sackman <matthew@cue.works>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>