Skip to content

Commit 2be20f8

Browse files
committed
internal/core/eval: do not precompute matchers
This will allow more caching in the end, by moving precomputed collations into the ADT. No noticeable performance impact. Change-Id: Ia7d9ca8ee3cf6578a903bbbd3acc1f2b8a062770 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7984 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 9ca9e04 commit 2be20f8

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

internal/core/eval/closed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ func (c *acceptor) verifySets(ctx *adt.OpContext, id adt.ID, f adt.Feature) (fou
740740
}
741741

742742
for _, b := range o.Bulk {
743-
if b.check.Match(ctx, o.env, f) {
743+
if matchBulk(ctx, o.env, b, f) {
744744
return true, false
745745
}
746746
}

internal/core/eval/optionals.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type fieldSet struct {
3838
Dynamic []*adt.DynamicField
3939

4040
// excluded are all literal fields that already exist.
41-
Bulk []bulkField
41+
Bulk []*adt.BulkOptionalField
4242

4343
Additional []adt.Expr
4444
IsOpen bool // has a ...
@@ -80,12 +80,6 @@ type FieldInfo struct {
8080
Optional []adt.Node
8181
}
8282

83-
type bulkField struct {
84-
check fieldMatcher
85-
expr adt.Node // *adt.BulkOptionalField // Conjunct
86-
additional bool // used with ...
87-
}
88-
8983
func (o *fieldSet) Accept(c *adt.OpContext, f adt.Feature) bool {
9084
if len(o.Additional) > 0 {
9185
return true
@@ -100,7 +94,7 @@ func (o *fieldSet) Accept(c *adt.OpContext, f adt.Feature) bool {
10094
}
10195
}
10296
for _, b := range o.Bulk {
103-
if b.check.Match(c, o.env, f) {
97+
if matchBulk(c, o.env, b, f) {
10498
return true
10599
}
106100
}
@@ -136,15 +130,13 @@ outer:
136130
bulkEnv.Cycles = nil
137131

138132
// match bulk optional fields / pattern properties
139-
for _, f := range o.Bulk {
140-
if matched && f.additional {
141-
continue
142-
}
143-
if f.check.Match(c, o.env, arc.Label) {
133+
for _, b := range o.Bulk {
134+
// if matched && f.additional {
135+
// continue
136+
// }
137+
if matchBulk(c, o.env, b, arc.Label) {
144138
matched = true
145-
if f.expr != nil {
146-
arc.AddConjunct(adt.MakeConjunct(&bulkEnv, f.expr, o.id))
147-
}
139+
arc.AddConjunct(adt.MakeConjunct(&bulkEnv, b, o.id))
148140
}
149141
}
150142
if matched {
@@ -190,18 +182,10 @@ func (o *fieldSet) AddDynamic(c *adt.OpContext, x *adt.DynamicField) {
190182
}
191183

192184
func (o *fieldSet) AddBulk(c *adt.OpContext, x *adt.BulkOptionalField) {
193-
v, ok := c.Evaluate(o.env, x.Filter)
194-
if !ok {
195-
// TODO: handle dynamically
196-
return
197-
}
198-
199-
if m := o.getMatcher(c, v); m != nil {
200-
o.Bulk = append(o.Bulk, bulkField{m, x, false})
201-
}
185+
o.Bulk = append(o.Bulk, x)
202186
}
203187

204-
func (o *fieldSet) getMatcher(c *adt.OpContext, v adt.Value) fieldMatcher {
188+
func getMatcher(c *adt.OpContext, env *adt.Environment, v adt.Value) fieldMatcher {
205189
switch f := v.(type) {
206190
case *adt.Top:
207191
return typeMatcher(adt.TopKind)
@@ -210,8 +194,22 @@ func (o *fieldSet) getMatcher(c *adt.OpContext, v adt.Value) fieldMatcher {
210194
return typeMatcher(f.K)
211195

212196
default:
213-
return o.newPatternMatcher(c, v)
197+
return newPatternMatcher(c, env, v)
198+
}
199+
}
200+
201+
func matchBulk(c *adt.OpContext, env *adt.Environment, x *adt.BulkOptionalField, f adt.Feature) bool {
202+
v, ok := c.Evaluate(env, x.Filter)
203+
if !ok {
204+
// TODO: handle dynamically
205+
return false
206+
}
207+
208+
m := getMatcher(c, env, v)
209+
if m == nil {
210+
return false
214211
}
212+
return m.Match(c, env, f)
215213
}
216214

217215
func (o *fieldSet) AddEllipsis(c *adt.OpContext, x *adt.Ellipsis) {
@@ -272,7 +270,7 @@ func (m patternMatcher) Match(c *adt.OpContext, env *adt.Environment, f adt.Feat
272270
return b == nil
273271
}
274272

275-
func (o *fieldSet) newPatternMatcher(ctx *adt.OpContext, x adt.Value) fieldMatcher {
276-
c := adt.MakeRootConjunct(o.env, x)
273+
func newPatternMatcher(ctx *adt.OpContext, env *adt.Environment, x adt.Value) fieldMatcher {
274+
c := adt.MakeRootConjunct(env, x)
277275
return patternMatcher(c)
278276
}

0 commit comments

Comments
 (0)