Skip to content

Commit

Permalink
internal/core/adt: add indirection for closeContext
Browse files Browse the repository at this point in the history
We want to be able to move the location of the
closeContext without much effort. This change introduces
an accessor method that allows for an indirection down
the road.

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: I81d6f2f2588e5beaf4421d16225b5aebef11bfeb
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202771
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
mpvl committed Oct 21, 2024
1 parent b990eb7 commit 0fa94ff
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
20 changes: 12 additions & 8 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ type Vertex struct {
//
state *nodeContext

// cc manages the closedness logic for this Vertex. It is created
// _cc manages the closedness logic for this Vertex. It is created
// by rootCloseContext.
// TODO: move back to nodeContext, but be sure not to clone it.
cc *closeContext
_cc *closeContext

// Label is the feature leading to this vertex.
Label Feature
Expand Down Expand Up @@ -299,25 +299,29 @@ func equalDeref(a, b *Vertex) bool {
return deref(a) == deref(b)
}

func (v *Vertex) cc() *closeContext {
return v._cc
}

// rootCloseContext creates a closeContext for this Vertex or returns the
// existing one.
func (v *Vertex) rootCloseContext(ctx *OpContext) *closeContext {
if v.cc == nil {
v.cc = &closeContext{
if v._cc == nil {
v._cc = &closeContext{
group: (*ConjunctGroup)(&v.Conjuncts),
parent: nil,
src: v,
parentConjuncts: v,
}
v.cc.incDependent(ctx, ROOT, nil) // matched in REF(decrement:nodeDone)
v._cc.incDependent(ctx, ROOT, nil) // matched in REF(decrement:nodeDone)
}
v.cc.origin = v.cc
v._cc.origin = v._cc
if p := v.Parent; p != nil {
pcc := p.rootCloseContext(ctx)
v.cc.origin = pcc.origin
v._cc.origin = pcc.origin
}

return v.cc
return v._cc
}

// newInlineVertex creates a Vertex that is needed for computation, but for
Expand Down
10 changes: 5 additions & 5 deletions internal/core/adt/cycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ func (n *nodeContext) markCyclicV3(arc *Vertex, env *Environment, x Resolver, ci
// TODO: investigate if we can get rid of cyclicConjuncts in the new
// evaluator.
v := Conjunct{env, x, ci}
n.node.cc.incDependent(n.ctx, DEFER, nil)
n.node.cc().incDependent(n.ctx, DEFER, nil)
n.cyclicConjuncts = append(n.cyclicConjuncts, cyclicConjunct{v, arc})
return ci, true
}
Expand All @@ -703,7 +703,7 @@ func (n *nodeContext) markCyclicPathV3(arc *Vertex, env *Environment, x Resolver
// TODO: investigate if we can get rid of cyclicConjuncts in the new
// evaluator.
v := Conjunct{env, x, ci}
n.node.cc.incDependent(n.ctx, DEFER, nil)
n.node.cc().incDependent(n.ctx, DEFER, nil)
n.cyclicConjuncts = append(n.cyclicConjuncts, cyclicConjunct{v, arc})
return ci, true
}
Expand Down Expand Up @@ -1057,7 +1057,7 @@ outer:
// evaluator.
v := Conjunct{env, x, ci}
if n.ctx.isDevVersion() {
n.node.cc.incDependent(n.ctx, DEFER, nil)
n.node.cc().incDependent(n.ctx, DEFER, nil)
}
n.cyclicConjuncts = append(n.cyclicConjuncts, cyclicConjunct{v, arc})
return ci, true
Expand Down Expand Up @@ -1092,7 +1092,7 @@ func (n *nodeContext) updateCyclicStatusV3(c CloseInfo) {
ci := c.c.CloseInfo
ci.cc = n.node.rootCloseContext(n.ctx)
n.scheduleVertexConjuncts(c.c, c.arc, ci)
n.node.cc.decDependent(n.ctx, DEFER, nil)
n.node.cc().decDependent(n.ctx, DEFER, nil)
}
n.cyclicConjuncts = n.cyclicConjuncts[:0]
}
Expand All @@ -1115,7 +1115,7 @@ func (n *nodeContext) updateCyclicStatus(c CloseInfo) {
func assertStructuralCycleV3(n *nodeContext) bool {
// TODO: is this the right place to put it?
for range n.cyclicConjuncts {
n.node.cc.decDependent(n.ctx, DEFER, nil)
n.node.cc().decDependent(n.ctx, DEFER, nil)
}
n.cyclicConjuncts = n.cyclicConjuncts[:0]

Expand Down
13 changes: 7 additions & 6 deletions internal/core/adt/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (cc *closeContext) linkNotify(ctx *OpContext, dst *Vertex, key *closeContex
}
}

cc.addDependency(ctx, NOTIFY, key, key, dst.cc)
cc.addDependency(ctx, NOTIFY, key, key, dst.cc())
return true
}

Expand Down Expand Up @@ -528,7 +528,7 @@ func (c *closeContext) addDependency(ctx *OpContext, kind depKind, key, child, r
// if child.src.Parent != c.src {
// panic("addArc: inconsistent parent")
// }
if child.src.cc != root.src.cc {
if child.src.cc() != root.src.cc() {
panic("addArc: inconsistent root")
}
c.arcs = append(c.arcs, ccArc{
Expand Down Expand Up @@ -828,12 +828,13 @@ func (n *nodeContext) insertArcCC(f Feature, mode ArcType, c Conjunct, id CloseI
// return v, nil
// }

if v.cc == nil {
v.cc = v.rootCloseContext(n.ctx)
v.cc.generation = n.node.cc.generation
if v.cc() == nil {
v.rootCloseContext(n.ctx)
// TODO(evalv3): reevaluate need for generation
v._cc.generation = n.node._cc.generation
}

arc, added := cc.insertConjunct(n.ctx, v.cc, c, id, mode, check, true)
arc, added := cc.insertConjunct(n.ctx, v.cc(), c, id, mode, check, true)
if !added {
return v, arc
}
Expand Down
8 changes: 4 additions & 4 deletions internal/core/adt/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ func (ctx *overlayContext) cloneVertex(x *Vertex) *Vertex {

ctx.vertices = append(ctx.vertices, v)

v.cc = ctx.allocCC(x.cc)
v._cc = ctx.allocCC(x.cc())

v.cc.src = v
v.cc.parentConjuncts = v
v.Conjuncts = *v.cc.group
v._cc.src = v
v._cc.parentConjuncts = v
v.Conjuncts = *v._cc.group

if a := x.Arcs; len(a) > 0 {
// TODO(perf): reuse buffer.
Expand Down
6 changes: 3 additions & 3 deletions internal/core/adt/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ func (n *nodeContext) unshare() {

n.scheduleVertexConjuncts(n.shared, v, n.sharedID)

n.sharedID.cc.decDependent(n.ctx, SHARED, n.node.cc)
n.sharedID.cc.decDependent(n.ctx, SHARED, n.node.cc())
n.sharedID.cc = nil
}

// finalizeSharing should be called when it is known for sure a node can be
// shared.
func (n *nodeContext) finalizeSharing() {
if n.sharedID.cc != nil {
n.sharedID.cc.decDependent(n.ctx, SHARED, n.node.cc)
n.sharedID.cc.decDependent(n.ctx, SHARED, n.node.cc())
n.sharedID.cc = nil
}
if n.isShared {
Expand Down Expand Up @@ -98,7 +98,7 @@ func (n *nodeContext) share(c Conjunct, arc *Vertex, id CloseInfo) {
// have been processed and it is clear that sharing is possible. Delaying
// such a count should not hurt performance, as a shared node is completed
// anyway.
id.cc.incDependent(n.ctx, SHARED, n.node.cc)
id.cc.incDependent(n.ctx, SHARED, n.node.cc())
}

func (n *nodeContext) shareIfPossible(c Conjunct, arc *Vertex, id CloseInfo) bool {
Expand Down
6 changes: 3 additions & 3 deletions internal/core/adt/unify.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (v *Vertex) unify(c *OpContext, needs condition, mode runMode) bool {

// validationCompleted
if n.completed&(subFieldsProcessed) != 0 {
n.node.HasEllipsis = n.node.cc.hasEllipsis
n.node.HasEllipsis = n.node.cc().hasEllipsis

// The next piece of code used to address the following case
// (order matters)
Expand Down Expand Up @@ -415,7 +415,7 @@ func (n *nodeContext) completeNodeTasks(mode runMode) {
// - pending arcs (or incoming COMPS)
// TODO: replace with something more principled that does not piggyback on
// debug information.
for _, r := range v.cc.externalDeps {
for _, r := range v.cc().externalDeps {
src := r.src
a := &src.arcs[r.index]
if a.decremented || a.kind != NOTIFY {
Expand Down Expand Up @@ -493,7 +493,7 @@ func (n *nodeContext) completeAllArcs(needs condition, mode runMode) bool {
// Investigate how to work around this.
n.completeNodeTasks(finalize)

for _, r := range n.node.cc.externalDeps {
for _, r := range n.node.cc().externalDeps {
src := r.src
a := &src.arcs[r.index]
if a.decremented {
Expand Down

0 comments on commit 0fa94ff

Please sign in to comment.