cue/ast: do not allocate in SetComments when it's a no-op
That is, when we're setting a node's comments slice to nil via
ast.SetComments(node, nil)
and the node has not had any comments set yet,
we would allocate the nil slice so that we can take a pointer to it.
This is unnecessary; the resulting node has zero comments either way,
and having a non-nil pointer to a nil slice is in no way an advantage.
For example, cue/parser often parses syntax nodes with no attached
comments in CUE code. We now avoid an allocation for those:
│ old │ new │
│ sec/op │ sec/op vs base │
FmtAwsSchema 2.362 ± 3% 2.286 ± 1% -3.21% (p=0.001 n=8)
│ old │ new │
│ B/op │ B/op vs base │
FmtAwsSchema 1.547Gi ± 0% 1.526Gi ± 0% -1.38% (p=0.000 n=8)
│ old │ new │
│ allocs/op │ allocs/op vs base │
FmtAwsSchema 16.29M ± 0% 15.33M ± 0% -5.88% (p=0.000 n=8)
Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iffdae6914e2abcf571a700002d63c7bb2d66ce00
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1225192
Reviewed-by: Matthew Sackman <matthew@cue.works>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>