this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

+7
+7
cue/ast/ast.go
··· 167 167 168 168 func (c *comments) SetComments(cgs []*CommentGroup) { 169 169 if c.groups == nil { 170 + if cgs == nil { 171 + // Replacing no comments with a nil slice is a no-op. 172 + // Avoid allocating below. 173 + // Note that we continue for other zero-length slices, 174 + // as the caller may want to reuse memory. 175 + return 176 + } 170 177 a := cgs 171 178 c.groups = &a 172 179 return