1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
// Copyright 2024 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package adt
// This file contains logic regarding structure sharing.
// Notes
//
// TODO:
// We may want to consider tracking closedness in parallel to the Vertex
// structure, for instance in a CloseInfo or in a cue.Value itself.
//
// reg: {}
// #def: sub: reg
//
// By tracking closedness inside the CloseInfo, we can still share the
// structure and only have to change
//
// Maybe this is okay, though, as #Def itself can be shared, at least.
func (n *nodeContext) unshare() {
n.noSharing = true
if !n.isShared {
return
}
n.isShared = false
n.node.IsShared = false
v := n.node.BaseValue.(*Vertex)
// TODO: the use of cycle for BaseValue is getting increasingly outdated.
// Find another mechanism once we get rid of the old evaluator.
n.node.BaseValue = n.origBaseValue
for _, id := range n.sharedIDs {
n.scheduleVertexConjuncts(n.shared, v, id)
}
n.decSharedIDs()
}
// finalizeSharing should be called when it is known for sure a node can be
// shared.
func (n *nodeContext) finalizeSharing() {
n.decSharedIDs()
if !n.isShared {
return
}
switch v := n.node.BaseValue.(type) {
case *Vertex:
if n.shareCycleType == NoCycle {
v.Finalize(n.ctx)
} else if !v.isFinal() {
// TODO: ideally we just handle cycles in optional chains directly,
// rather than relying on this mechanism. This requires us to add
// a mechanism to detect that.
n.ctx.toFinalize = append(n.ctx.toFinalize, v)
}
// If state.parent is non-nil, we determined earlier that this Vertex
// is not rooted and that it can safely be shared. Because it is
// not-rooted, though, it will not have a path location, resulting in
// bad error messages, and in some cases dropped errors. To avoid this,
// we reset the parent and label of the Vertex so that its path reflects
// its assigned location.
if v.state != nil && v.state.parent != nil {
v.Parent = v.state.parent
// TODO: see if this can be removed and why some errors are not
// propagated when removed.
n.isShared = false
}
case *Bottom:
// An error trumps sharing. We can leave it as is.
default:
panic("unreachable")
}
}
func (n *nodeContext) addShared(id CloseInfo) {
if len(n.sharedIDs) == 0 || n.shareCycleType < id.CycleType {
n.shareCycleType = id.CycleType
}
// At this point, the node may still be unshared at a later point. For this
// purpose we need to keep the retain count above zero until all conjuncts
// 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.
n.sharedIDs = append(n.sharedIDs, id)
if id.cc != nil {
id.cc.incDependent(n.ctx, SHARED, n.node.cc())
}
}
func (n *nodeContext) decSharedIDs() {
if n.shareDecremented {
return
}
n.shareDecremented = true
for _, id := range n.sharedIDs {
if cc := id.cc; cc != nil {
cc.decDependent(n.ctx, SHARED, n.node.cc())
}
}
}
func (n *nodeContext) share(c Conjunct, arc *Vertex, id CloseInfo) {
if n.isShared {
panic("already sharing")
}
n.origBaseValue = n.node.BaseValue
n.node.BaseValue = arc
n.node.IsShared = true
n.isShared = true
n.shared = c
n.addShared(id)
if arc.IsDetached() && arc.MayAttach() { // TODO: Second check necessary?
// This node can safely be shared. Since it is not rooted, though, it
// does not have a path location. Instead of setting the parent path
// directly, though, we record the prospective parent in the state: as
// the evaluator uses the Parent field during evaluation, setting the
// field directly here can result in incorrect evaluation results.
// Setting the parent in the state instead allows us to defer setting
// Parent until it is safe to do so..
if s := arc.getState(n.ctx); s != nil {
s.parent = n.node
}
}
}
func (n *nodeContext) shareIfPossible(c Conjunct, arc *Vertex, id CloseInfo) bool {
if !n.ctx.Sharing {
return false
}
// We do not allowing sharing if the conjunct has a cycle. Sharing is only
// possible if there is a single conjunct. We want to further evaluate this
// conjunct to force recognition of a structural cycle.
if id.CycleType == IsCyclic && (n.node.nonRooted || n.node.IsDynamic) {
return false
}
if n.noSharing || n.isShared || n.ctx.errs != nil {
return false
}
// This line is to deal with this case:
//
// reg: {}
// #def: sub: reg
//
// Ideally we find a different solution, like passing closedness
// down elsewhere. In fact, as we do this in closeContexts, it probably
// already works, it will just not be reflected in the debug output.
// We could fix that by not printing structure shared nodes, which is
// probably a good idea anyway.
//
// TODO: come up with a mechanism to allow this case.
if n.node.ClosedRecursive && !arc.ClosedRecursive {
return false
}
// Sharing let expressions is not supported and will result in unmarked
// structural cycles. Processing will still terminate, but printing the
// result will result in an infinite loop.
//
// TODO: allow this case.
if arc.Label.IsLet() {
return false
}
n.share(c, arc, id)
return true
}
// Vertex values that are held in BaseValue will be wrapped in the following
// order:
//
// disjuncts -> (shared | computed | data)
//
// DerefDisjunct
// - get the current value under computation
//
// DerefValue
// - get the value the node ultimately represents.
//
// DerefValue unrolls indirections of Vertex values. These may be introduced,
// for instance, by temporary bindings such as comprehension values.
// It returns v itself if v does not point to another Vertex.
func (v *Vertex) DerefValue() *Vertex {
for {
arc, ok := v.BaseValue.(*Vertex)
if !ok {
return v
}
v = arc
}
}
// DerefDisjunct indirects a node that points to a disjunction.
func (v *Vertex) DerefDisjunct() *Vertex {
for {
arc, ok := v.BaseValue.(*Vertex)
if !ok || !arc.IsDisjunct {
return v
}
v = arc
}
}
// DerefNonDisjunct indirects a node that points to a disjunction.
func (v *Vertex) DerefNonDisjunct() *Vertex {
for {
arc, ok := v.BaseValue.(*Vertex)
if !ok || arc.IsDisjunct {
return v
}
v = arc
}
}
// DerefNonRooted indirects a node that points to a value that is not rooted.
// This includes structure-shared nodes that point to a let field: let fields
// may or may not be part of a struct, and thus should be treated as non-rooted.
func (v *Vertex) DerefNonRooted() *Vertex {
for {
arc, ok := v.BaseValue.(*Vertex)
if !ok || arc.IsDisjunct || (v.IsShared && !arc.Label.IsLet()) {
return v
}
v = arc
}
}
// DerefNonShared finds the indirection of an arc that is not the result of
// structure sharing. This is especially relevant when indirecting disjunction
// values.
func (v *Vertex) DerefNonShared() *Vertex {
if v.state != nil && v.state.isShared {
return v
}
for {
arc, ok := v.BaseValue.(*Vertex)
if !ok {
return v
}
v = arc
}
}
|