Skip to content

Commit 1d028fc

Browse files
committed
internal/core/adt: reuse reqSets slices via OpContext
Benchmarking the reduction at https://cuelang.org/issue/3334 via benchcmd -n 12 CueVetIssue3334 cue vet -c f.cue: │ old │ new │ │ sec/op │ sec/op vs base │ CueVetIssue3334 175.3m ± 1% 154.9m ± 1% -11.67% (p=0.000 n=12) │ old │ new │ │ user-sec/op │ user-sec/op vs base │ CueVetIssue3334 332.1m ± 4% 270.8m ± 5% -18.46% (p=0.000 n=12) │ old │ new │ │ sys-sec/op │ sys-sec/op vs base │ CueVetIssue3334 27.61m ± 16% 21.92m ± 30% ~ (p=0.089 n=12) │ old │ new │ │ peak-RSS-bytes │ peak-RSS-bytes vs base │ CueVetIssue3334 182.2Mi ± 4% 177.4Mi ± 1% -2.61% (p=0.000 n=12) Updates #3334. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I1f066de341e8fd2e9d508304e3de1b86234f60a6 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1213055 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent e778137 commit 1d028fc

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

internal/core/adt/context.go

+2
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ type OpContext struct {
188188
currentDisjunctionID int // sequence number for call to processDisjunctions
189189

190190
disjunctStack []disjunctInfo // stack of disjunct IDs
191+
192+
reqSetsBuf reqSets // reuse a reqSets slice
191193
}
192194

193195
func (c *OpContext) CloseInfo() CloseInfo { return c.ci }

internal/core/adt/typocheck.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func (n *nodeContext) checkTypos() {
487487
replacements := na.getReplacements(nil) // TODO(perf): use buffer
488488
required := slices.Clone(required) // TODO(perf): use buffer
489489
// do the right thing in appendRequired either way.
490-
required.replaceIDs(replacements...)
490+
required.replaceIDs(n.ctx, replacements...)
491491

492492
a = a.DerefDisjunct()
493493
// TODO(perf): somehow prevent error generation of recursive structures,
@@ -617,10 +617,10 @@ func (a reqSets) assert() {
617617
// - If in active definition, replace old definition
618618
// - If in embed, replace embed in respective sets. definition starts new group
619619
// - child definition replaces parent definition
620-
func (a *reqSets) replaceIDs(b ...replaceID) {
620+
func (a *reqSets) replaceIDs(ctx *OpContext, b ...replaceID) {
621621
temp := *a
622622
temp = temp[:0]
623-
var buf reqSets
623+
buf := ctx.reqSetsBuf[:0]
624624
outer:
625625
for i := 0; i < len(*a); {
626626
e := (*a)[i]
@@ -641,7 +641,7 @@ outer:
641641
} else {
642642
temp = append(temp, buf...)
643643
}
644-
buf = buf[:0] // TODO(perf): use OpContext buffer.
644+
buf = buf[:0]
645645
}
646646
}
647647

@@ -653,6 +653,7 @@ outer:
653653
buf[0].size = uint32(len(buf))
654654
temp = append(temp, buf...)
655655
}
656+
ctx.reqSetsBuf = buf[:0] // to be reused later on
656657
*a = temp
657658
}
658659

@@ -772,7 +773,7 @@ outer:
772773
})
773774
}
774775

775-
a.replaceIDs(n.replaceIDs...)
776+
a.replaceIDs(n.ctx, n.replaceIDs...)
776777

777778
// If 'v' is a hidden field, then all reqSets in 'a' for which there is no
778779
// corresponding entry in conjunctInfo should be removed from 'a'.

internal/core/adt/typocheck_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func TestReplaceIDs(t *testing.T) {
477477
tt.reqSets.assert()
478478
tt.expected.assert()
479479

480-
tt.reqSets.replaceIDs(tt.replace...)
480+
tt.reqSets.replaceIDs(&OpContext{}, tt.replace...)
481481
if !slices.Equal(tt.reqSets, tt.expected) {
482482
t.Errorf("got: \n%v, want:\n%v", tt.reqSets, tt.expected)
483483
}

0 commit comments

Comments
 (0)