Skip to content

Commit 24cc414

Browse files
committed
internal/core/adt: hoist loop
Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: Icd2738f2da3e1caea87e5ccb1cc2bc25d399228c Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1218694 Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 5ee4254 commit 24cc414

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

internal/core/adt/typocheck.go

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ func (n *nodeContext) hasEvidenceForAll(a reqSets, conjuncts []conjunctInfo) boo
612612
continue
613613
}
614614

615-
if !hasEvidenceForOne(a, i, conjuncts) {
615+
if !n.hasEvidenceForOne(a, i, conjuncts) {
616616
if n.ctx.LogEval > 0 {
617617
n.Logf("DENIED BY %d", a[i].id)
618618
}
@@ -624,14 +624,12 @@ func (n *nodeContext) hasEvidenceForAll(a reqSets, conjuncts []conjunctInfo) boo
624624

625625
// hasEvidenceForOne reports whether a single typo-checked set has evidence for
626626
// any of its defIDs.
627-
func hasEvidenceForOne(all reqSets, i uint32, conjuncts []conjunctInfo) bool {
627+
func (n *nodeContext) hasEvidenceForOne(all reqSets, i uint32, conjuncts []conjunctInfo) bool {
628628
a := all[i : i+all[i].size]
629629

630-
for _, c := range conjuncts {
631-
for _, ri := range a {
632-
if c.id == ri.id {
633-
return true
634-
}
630+
for _, x := range conjuncts {
631+
if n.containsDefID(requirement(a), x.id) {
632+
return true
635633
}
636634
}
637635

@@ -649,11 +647,9 @@ func hasEvidenceForOne(all reqSets, i uint32, conjuncts []conjunctInfo) bool {
649647

650648
outer:
651649
for _, c := range conjuncts {
652-
for _, x := range embedScope {
653-
if x.id == c.embed {
654-
// Within the scope of the embedding.
655-
continue outer
656-
}
650+
if n.containsDefID(requirement(embedScope), c.embed) {
651+
// Within the scope of the embedding.
652+
continue outer
657653
}
658654

659655
if len(outerScope) == 0 || a[0].parent == 0 {
@@ -663,10 +659,17 @@ outer:
663659
// If this conjunct is within the outer struct, but outside the
664660
// embedding scope, this means it was "added" and we do not have
665661
// to verify it within the embedding scope.
666-
for _, x := range outerScope {
667-
if x.id == c.id {
668-
return true
669-
}
662+
if n.containsDefID(requirement(outerScope), c.id) {
663+
return true
664+
}
665+
}
666+
return false
667+
}
668+
669+
func (n *nodeContext) containsDefID(node requirement, child defID) bool {
670+
for _, ri := range node {
671+
if child == ri.id {
672+
return true
670673
}
671674
}
672675
return false
@@ -957,10 +960,8 @@ outer:
957960
if allowedInClosed(v.Label) {
958961
n.filterSets(&a, func(n *nodeContext, a requirement) bool {
959962
for _, c := range n.conjunctInfo {
960-
for _, e := range a {
961-
if c.id == e.id {
962-
return true // keep the set
963-
}
963+
if n.containsDefID(requirement(a), c.id) {
964+
return true // keep the set
964965
}
965966
}
966967
return false // discard the set
@@ -986,10 +987,7 @@ func (n *nodeContext) filterTop(a *reqSets, conjuncts, parentConjuncts []conjunc
986987
var f conjunctFlags
987988
hasAny := false
988989
for _, c := range conjuncts {
989-
for _, e := range a {
990-
if e.id != c.id {
991-
continue
992-
}
990+
if n.containsDefID(requirement(a), c.id) {
993991
hasAny = true
994992
flags := c.flags
995993
if c.id < a[0].id {
@@ -1020,10 +1018,8 @@ func hasParentEllipsis(n *nodeContext, a requirement, conjuncts []conjunctInfo)
10201018
if !c.flags.hasEllipsis() {
10211019
continue
10221020
}
1023-
for _, e := range a {
1024-
if e.id == c.id {
1025-
return true
1026-
}
1021+
if n.containsDefID(requirement(a), c.id) {
1022+
return true
10271023
}
10281024
}
10291025
return false

0 commit comments

Comments
 (0)