Skip to content

Commit

Permalink
internal/core/eval: reduce per-node allocations
Browse files Browse the repository at this point in the history
Removed resultNode from nodeShared and reuse
allocations of nodeShared and nodeContext.

Reduces running time of all tests by about 15%.

This also simplifies disjunction handling and prepares
for optimizing disjunction handling further.

Note that with this change, it would be possible to
merge nodeShared and nodeContext into a single
type. This is left for later work.

This also delays the creation of the arcMap to
when it is really needed, avoiding another allocation
for the majority of nodes.

Change-Id: I40f39dbc5d07992cbc3490c4512caf9845ac1881
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7403
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Oct 19, 2020
1 parent e05eee7 commit 30704a7
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ Result:
xa4: (int){ 10 }
xb1: (int){ |((int){ 8 }, (int){ 9 }) }
xb2: (_|_){
// [incomplete]
// [incomplete] xb2: unresolved disjunction 6 | 9 (type int):
// ./in.cue:18:6
}
xb3: (int){ |((int){ 6 }, (int){ 9 }) }
xb4: (_|_){
Expand Down Expand Up @@ -213,7 +214,8 @@ Result:
}
xf1: (int){ |((int){ 8 }, (int){ 9 }) }
xf2: (_|_){
// [incomplete]
// [incomplete] xf2: unresolved disjunction 6 | 9 (type int):
// ./in.cue:52:6
}
xf3: (int){ |((int){ 6 }, (int){ 9 }) }
xf4: (_|_){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,8 @@ Result:
xa2: (int){ 8 }
xa3: (int){ 6 }
xa4: (int){ 10 }
xb1: (int){ |(*(int){ 8 }, (int){ 9 }) }
xb2: (_|_){
// [incomplete]
}
xb1: (int){ 8 }
xb2: (int){ 8 }
xb3: (int){ 6 }
xb4: (_|_){
// [cycle] cycle error
Expand Down
33 changes: 11 additions & 22 deletions internal/core/eval/disjunct.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,8 @@ func (n *nodeContext) updateResult() (isFinal bool) {
return n.isFinal
}

d := n.nodeShared.disjunct
if d == nil {
d = &adt.Disjunction{}
n.nodeShared.disjunct = d
}
n.touched = true
d := &n.nodeShared.disjunct

result := *n.node
if result.Value == nil {
Expand All @@ -152,6 +149,11 @@ func (n *nodeContext) updateResult() (isFinal bool) {

p := &result
d.Values = append(d.Values, p)

if n.done() && (!n.isDefault() || n.isDefault()) {
n.nodeShared.isDone = true
}

if n.defaultMode == isDefault {
// Keep defaults sorted first.
i := d.NumDefaults
Expand All @@ -172,22 +174,9 @@ func (n *nodeContext) updateResult() (isFinal bool) {
case !n.nodeShared.isDefault() && n.defaultMode == isDefault:

default:
if x := n.result(); x == nil && Equal(n.ctx, n.node, x) {
return n.isFinal
}

// TODO: Compute fancy error message.
n.nodeShared.resultNode = n
// n.nodeShared.result.AddErr(n.ctx, &adt.Bottom{
// Code: adt.IncompleteError,
// Err: errors.Newf(n.ctx.Pos(), "ambiguous disjunction"),
// })
n.nodeShared.result_.Arcs = nil
n.nodeShared.result_.Structs = nil
return n.isFinal // n.defaultMode == isDefault
}

n.nodeShared.resultNode = n
n.nodeShared.setResult(n.node)

return n.isFinal
Expand Down Expand Up @@ -225,11 +214,11 @@ func (n *nodeContext) insertDisjuncts() (inserted bool) {
p := 0
inserted = true

disjunctions := []envDisjunct{}
n.subDisjunctions = n.subDisjunctions[:0]

// fmt.Println("----", debug.NodeString(n.ctx, n.node, nil))
for _, d := range n.disjunctions {
disjunctions = append(disjunctions, d)
n.subDisjunctions = append(n.subDisjunctions, d)

sub := len(n.disjunctions)
defMode, ok := n.insertSingleDisjunct(p, d, false)
Expand Down Expand Up @@ -260,7 +249,7 @@ func (n *nodeContext) insertDisjuncts() (inserted bool) {
// 0 to a referenced number (forces the default to be discarded).
wasScalar := n.scalar != nil // Hack line 1

disjunctions = append(disjunctions, d)
n.subDisjunctions = append(n.subDisjunctions, d)
mode, ok := n.insertSingleDisjunct(p, d, true)
p++
if !ok {
Expand All @@ -278,7 +267,7 @@ func (n *nodeContext) insertDisjuncts() (inserted bool) {
}

// Find last disjunction at which there is no overflow.
for ; p > 0 && n.stack[p-1]+1 >= len(disjunctions[p-1].values); p-- {
for ; p > 0 && n.stack[p-1]+1 >= len(n.subDisjunctions[p-1].values); p-- {
}
if p > 0 {
// Increment a valid position and set all subsequent entries to 0.
Expand Down

0 comments on commit 30704a7

Please sign in to comment.