| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package interpreterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ |
| 16 | |
| 17 | import ( |
| 18 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 19 | "mathscip-go gomod github.com/golang/go/src go1.23.0 math/" |
| 20 | |
| 21 | "github.com/google/cel-go/commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/" |
| 22 | "github.com/google/cel-go/common/overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/" |
| 23 | "github.com/google/cel-go/common/typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/" |
| 24 | "github.com/google/cel-go/common/types/refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/" |
| 25 | "github.com/google/cel-go/common/types/traitsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/traits`/" |
| 26 | ) |
| 27 | |
| 28 | // WARNING: Any changes to cost calculations in this file require a corresponding change in checker/cost.go |
| 29 | |
| 30 | // ActualCostEstimator provides function call cost estimations at runtime |
| 31 | // CallCost returns an estimated cost for the function overload invocation with the given args, or nil if it has no |
| 32 | // estimate to provide. CEL attempts to provide reasonable estimates for its standard function library, so CallCost |
| 33 | // should typically not need to provide an estimate for CELs standard function. |
| 34 | type ActualCostEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ActualCostEstimator# interface { |
| 35 | CallCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ActualCostEstimator#CallCost.(function, overloadID string, args []refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, result refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) *uint64 |
| 36 | } |
| 37 | |
| 38 | // costTrackPlanOption modifies the cost tracking factory associatied with the CostObserver |
| 39 | type costTrackPlanOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackPlanOption# func(*costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#) *costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory# |
| 40 | |
| 41 | // CostTrackerFactory configures the factory method to generate a new cost-tracker per-evaluation. |
| 42 | func CostTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTrackerFactory().(factory func() (*CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#, error)) costTrackPlanOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackPlanOption# { |
| 43 | return func(fac *costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#) *costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory# { |
| 44 | fac.factoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#factory. = factory |
| 45 | return fac |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // CostObserver provides an observer that tracks runtime cost. |
| 50 | func CostObserverscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostObserver().(opts ...costTrackPlanOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackPlanOption#) PlannerOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/PlannerOption# { |
| 51 | ct := &costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#{} |
| 52 | for _, o := range opts { |
| 53 | ct = o(ct) |
| 54 | } |
| 55 | return func(p *plannerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/planner#) (*plannerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/planner#, error) { |
| 56 | if ct.factoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#factory. == nil { |
| 57 | return nil, errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("cost tracker factory not configured") |
| 58 | } |
| 59 | p.observersscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/planner#observers. = append(p.observersscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/planner#observers., ct) |
| 60 | p.decoratorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/planner#decorators. = append(p.decoratorsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/planner#decorators., decObserveEvalscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/decObserveEval().(ct.Observescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#Observe().)) |
| 61 | return p, nil |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // costTrackerConverter identifies an object which is convertible to a CostTracker instance. |
| 66 | type costTrackerConverterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerConverter# interface { |
| 67 | asCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerConverter#asCostTracker.() *CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker# |
| 68 | } |
| 69 | |
| 70 | // costTrackActivation hides state in the Activation in a manner not accessible to expressions. |
| 71 | type costTrackActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation# struct { |
| 72 | varsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#vars. Activationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation# |
| 73 | costTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#costTracker. *CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker# |
| 74 | } |
| 75 | |
| 76 | // ResolveName proxies variable lookups to the backing activation. |
| 77 | func (cta costTrackActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#) ResolveNamescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#ResolveName().(name string) (any, bool) { |
| 78 | return cta.varsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#vars..ResolveNamescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation#ResolveName.(name) |
| 79 | } |
| 80 | |
| 81 | // Parent proxies parent lookups to the backing activation. |
| 82 | func (cta costTrackActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#) Parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#Parent().() Activationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation# { |
| 83 | return cta.varsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#vars. |
| 84 | } |
| 85 | |
| 86 | // AsPartialActivation supports conversion to a partial activation in order to detect unknown attributes. |
| 87 | func (cta costTrackActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#) AsPartialActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#AsPartialActivation().() (PartialActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/PartialActivation#, bool) { |
| 88 | return AsPartialActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/AsPartialActivation().(cta.varsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#vars.) |
| 89 | } |
| 90 | |
| 91 | // asCostTracker implements the costTrackerConverter method. |
| 92 | func (cta costTrackActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#) asCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#asCostTracker().() *CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker# { |
| 93 | return cta.costTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#costTracker. |
| 94 | } |
| 95 | |
| 96 | // asCostTracker walks the Activation hierarchy and returns the first cost tracker found, if present. |
| 97 | func asCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/asCostTracker().(vars Activationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation#) (*CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#, bool) { |
| 98 | if conv, ok := vars.(costTrackerConverterscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerConverter#); ok { |
| 99 | return conv.asCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerConverter#asCostTracker.(), true |
| 100 | } |
| 101 | if vars.Parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation#Parent.() != nil { |
| 102 | return asCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/asCostTracker().(vars.Parentscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation#Parent.()) |
| 103 | } |
| 104 | return nil, false |
| 105 | } |
| 106 | |
| 107 | // costTrackerFactory holds a factory for producing new CostTracker instances on each Eval call. |
| 108 | type costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory# struct { |
| 109 | factoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#factory. func() (*CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#, error) |
| 110 | } |
| 111 | |
| 112 | // InitState produces a CostTracker and bundles it into an Activation in a way which is not visible |
| 113 | // to expression evaluation. |
| 114 | func (ct *costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#) InitStatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#InitState().(vars Activationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation#) (Activationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation#, error) { |
| 115 | tracker, err := ct.factoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#factory.() |
| 116 | if err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | return costTrackActivationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#{varsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#vars.: vars, costTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackActivation#costTracker.: tracker}, nil |
| 120 | } |
| 121 | |
| 122 | // GetState extracts the CostTracker from the Activation. |
| 123 | func (ct *costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#) GetStatescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#GetState().(vars Activationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation#) any { |
| 124 | if tracker, found := asCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/asCostTracker().(vars); found { |
| 125 | return tracker |
| 126 | } |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | // Observe computes the incremental cost of each step and records it into the CostTracker associated |
| 131 | // with the evaluation. |
| 132 | func (ct *costTrackerFactoryscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#) Observescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/costTrackerFactory#Observe().(vars Activationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Activation#, id int64, programStep any, val refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) { |
| 133 | tracker, found := asCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/asCostTracker().(vars) |
| 134 | if !found { |
| 135 | return |
| 136 | } |
| 137 | switch t := programStep.(type) { |
| 138 | case ConstantQualifierscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ConstantQualifier#: |
| 139 | // TODO: Push identifiers on to the stack before observing constant qualifiers that apply to them |
| 140 | // and enable the below pop. Once enabled this can case can be collapsed into the Qualifier case. |
| 141 | tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost.++ |
| 142 | case InterpretableConstscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableConst#: |
| 143 | // zero cost |
| 144 | case InterpretableAttributescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableAttribute#: |
| 145 | switch a := t.Attrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableAttribute#Attr.().(type) { |
| 146 | case *conditionalAttributescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/conditionalAttribute#: |
| 147 | // Ternary has no direct cost. All cost is from the conditional and the true/false branch expressions. |
| 148 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(a.falsyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/conditionalAttribute#falsy..IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Qualifier#ID.(), a.truthyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/conditionalAttribute#truthy..IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Qualifier#ID.(), a.exprscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/conditionalAttribute#expr..IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#ID.()) |
| 149 | default: |
| 150 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(t.Attrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableAttribute#Attr.().IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Qualifier#ID.()) |
| 151 | tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. += commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.SelectAndIdentCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/SelectAndIdentCost. |
| 152 | } |
| 153 | if !tracker.presenceTestHasCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#presenceTestHasCost. { |
| 154 | if _, isTestOnly := programStep.(*evalTestOnlyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalTestOnly#); isTestOnly { |
| 155 | tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. -= commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.SelectAndIdentCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/SelectAndIdentCost. |
| 156 | } |
| 157 | } |
| 158 | case *evalExhaustiveConditionalscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalExhaustiveConditional#: |
| 159 | // Ternary has no direct cost. All cost is from the conditional and the true/false branch expressions. |
| 160 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(t.attrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalExhaustiveConditional#attr..falsyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/conditionalAttribute#falsy..IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Qualifier#ID.(), t.attrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalExhaustiveConditional#attr..truthyscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/conditionalAttribute#truthy..IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Qualifier#ID.(), t.attrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalExhaustiveConditional#attr..exprscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/conditionalAttribute#expr..IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#ID.()) |
| 161 | |
| 162 | // While the field names are identical, the boolean operation eval structs do not share an interface and so |
| 163 | // must be handled individually. |
| 164 | case *evalOrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalOr#: |
| 165 | for _, term := range t.termsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalOr#terms. { |
| 166 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(term.IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#ID.()) |
| 167 | } |
| 168 | case *evalAndscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalAnd#: |
| 169 | for _, term := range t.termsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalAnd#terms. { |
| 170 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(term.IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#ID.()) |
| 171 | } |
| 172 | case *evalExhaustiveOrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalExhaustiveOr#: |
| 173 | for _, term := range t.termsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalExhaustiveOr#terms. { |
| 174 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(term.IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#ID.()) |
| 175 | } |
| 176 | case *evalExhaustiveAndscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalExhaustiveAnd#: |
| 177 | for _, term := range t.termsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalExhaustiveAnd#terms. { |
| 178 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(term.IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#ID.()) |
| 179 | } |
| 180 | case *evalFoldscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalFold#: |
| 181 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(t.iterRangescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/evalFold#iterRange..IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#ID.()) |
| 182 | case Qualifierscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Qualifier#: |
| 183 | tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost.++ |
| 184 | case InterpretableCallscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableCall#: |
| 185 | if argVals, ok := tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropArgsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#dropArgs().(t.Argsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableCall#Args.()); ok { |
| 186 | tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. += tracker.costCallscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#costCall().(t, argVals, val) |
| 187 | } |
| 188 | case InterpretableConstructorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableConstructor#: |
| 189 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..dropArgsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#dropArgs().(t.InitValsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableConstructor#InitVals.()) |
| 190 | switch t.Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableConstructor#Type.() { |
| 191 | case typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.ListTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/ListType.: |
| 192 | tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. += commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.ListCreateBaseCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ListCreateBaseCost. |
| 193 | case typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.MapTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/MapType.: |
| 194 | tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. += commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.MapCreateBaseCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/MapCreateBaseCost. |
| 195 | default: |
| 196 | tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. += commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StructCreateBaseCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StructCreateBaseCost. |
| 197 | } |
| 198 | } |
| 199 | tracker.stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack..pushscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#push().(val, id) |
| 200 | |
| 201 | if tracker.Limitscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#Limit. != nil && tracker.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. > *tracker.Limitscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#Limit. { |
| 202 | panic(EvalCancelledErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/EvalCancelledError#{Causescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/EvalCancelledError#Cause.: CostLimitExceededscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostLimitExceeded., Messagescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/EvalCancelledError#Message.: "operation cancelled: actual cost limit exceeded"}) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // CostTrackerOption configures the behavior of CostTracker objects. |
| 207 | type CostTrackerOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTrackerOption# func(*CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#) error |
| 208 | |
| 209 | // CostTrackerLimit sets the runtime limit on the evaluation cost during execution and will terminate the expression |
| 210 | // evaluation if the limit is exceeded. |
| 211 | func CostTrackerLimitscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTrackerLimit().(limit uint64) CostTrackerOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTrackerOption# { |
| 212 | return func(tracker *CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#) error { |
| 213 | tracker.Limitscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#Limit. = &limit |
| 214 | return nil |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // PresenceTestHasCost determines whether presence testing has a cost of one or zero. |
| 219 | // Defaults to presence test has a cost of one. |
| 220 | func PresenceTestHasCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/PresenceTestHasCost().(hasCost bool) CostTrackerOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTrackerOption# { |
| 221 | return func(tracker *CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#) error { |
| 222 | tracker.presenceTestHasCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#presenceTestHasCost. = hasCost |
| 223 | return nil |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // NewCostTracker creates a new CostTracker with a given estimator and a set of functional CostTrackerOption values. |
| 228 | func NewCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/NewCostTracker().(estimator ActualCostEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ActualCostEstimator#, opts ...CostTrackerOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTrackerOption#) (*CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#, error) { |
| 229 | tracker := &CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#{ |
| 230 | Estimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#Estimator.: estimator, |
| 231 | overloadTrackersscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#overloadTrackers.: map[string]FunctionTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/FunctionTracker#{}, |
| 232 | presenceTestHasCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#presenceTestHasCost.: true, |
| 233 | } |
| 234 | for _, opt := range opts { |
| 235 | err := opt(tracker) |
| 236 | if err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | } |
| 240 | return tracker, nil |
| 241 | } |
| 242 | |
| 243 | // OverloadCostTracker binds an overload ID to a runtime FunctionTracker implementation. |
| 244 | // |
| 245 | // OverloadCostTracker instances augment or override ActualCostEstimator decisions, allowing for versioned and/or |
| 246 | // optional cost tracking changes. |
| 247 | func OverloadCostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/OverloadCostTracker().(overloadID string, fnTracker FunctionTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/FunctionTracker#) CostTrackerOptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTrackerOption# { |
| 248 | return func(tracker *CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#) error { |
| 249 | tracker.overloadTrackersscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#overloadTrackers.[overloadID] = fnTracker |
| 250 | return nil |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // FunctionTracker computes the actual cost of evaluating the functions with the given arguments and result. |
| 255 | type FunctionTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/FunctionTracker# func(args []refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, result refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) *uint64 |
| 256 | |
| 257 | // CostTracker represents the information needed for tracking runtime cost. |
| 258 | type CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker# struct { |
| 259 | Estimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#Estimator. ActualCostEstimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ActualCostEstimator# |
| 260 | overloadTrackersscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#overloadTrackers. map[string]FunctionTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/FunctionTracker# |
| 261 | Limitscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#Limit. *uint64 |
| 262 | presenceTestHasCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#presenceTestHasCost. bool |
| 263 | |
| 264 | costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. uint64 |
| 265 | stackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#stack. refValStackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack# |
| 266 | } |
| 267 | |
| 268 | // ActualCost returns the runtime cost |
| 269 | func (c *CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#) ActualCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#ActualCost().() uint64 { |
| 270 | return c.costscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#cost. |
| 271 | } |
| 272 | |
| 273 | func (c *CostTrackerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#) costCallscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#costCall().(call InterpretableCallscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableCall#, args []refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, result refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) uint64 { |
| 274 | var cost uint64 |
| 275 | if len(c.overloadTrackersscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#overloadTrackers.) != 0 { |
| 276 | if tracker, found := c.overloadTrackersscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#overloadTrackers.[call.OverloadIDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableCall#OverloadID.()]; found { |
| 277 | callCost := tracker(args, result) |
| 278 | if callCost != nil { |
| 279 | cost += *callCost |
| 280 | return cost |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | if c.Estimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#Estimator. != nil { |
| 285 | callCost := c.Estimatorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/CostTracker#Estimator..CallCostscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/ActualCostEstimator#CallCost.(call.Functionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableCall#Function.(), call.OverloadIDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableCall#OverloadID.(), args, result) |
| 286 | if callCost != nil { |
| 287 | cost += *callCost |
| 288 | return cost |
| 289 | } |
| 290 | } |
| 291 | // if user didn't specify, the default way of calculating runtime cost would be used. |
| 292 | // if user has their own implementation of ActualCostEstimator, make sure to cover the mapping between overloadId and cost calculation |
| 293 | switch call.OverloadIDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/InterpretableCall#OverloadID.() { |
| 294 | // O(n) functions |
| 295 | case overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.StartsWithStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/StartsWithString., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.EndsWithStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/EndsWithString., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.StringToBytesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/StringToBytes., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.BytesToStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/BytesToString., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.ExtQuoteStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/ExtQuoteString., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.ExtFormatStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/ExtFormatString.: |
| 296 | cost += uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().(float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[0])) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.)) |
| 297 | case overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.InListscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/InList.: |
| 298 | // If a list is composed entirely of constant values this is O(1), but we don't account for that here. |
| 299 | // We just assume all list containment checks are O(n). |
| 300 | cost += actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[1]) |
| 301 | // O(min(m, n)) functions |
| 302 | case overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.LessStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/LessString., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.GreaterStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/GreaterString., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.LessEqualsStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/LessEqualsString., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.GreaterEqualsStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/GreaterEqualsString., |
| 303 | overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.LessBytesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/LessBytes., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.GreaterBytesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/GreaterBytes., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.LessEqualsBytesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/LessEqualsBytes., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.GreaterEqualsBytesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/GreaterEqualsBytes., |
| 304 | overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.Equalsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/Equals., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.NotEqualsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/NotEquals.: |
| 305 | // When we check the equality of 2 scalar values (e.g. 2 integers, 2 floating-point numbers, 2 booleans etc.), |
| 306 | // the CostTracker.ActualSize() function by definition returns 1 for each operand, resulting in an overall cost |
| 307 | // of 1. |
| 308 | lhsSize := actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[0]) |
| 309 | rhsSize := actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[1]) |
| 310 | minSize := lhsSize |
| 311 | if rhsSize < minSize { |
| 312 | minSize = rhsSize |
| 313 | } |
| 314 | cost += uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().(float64(minSize) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.)) |
| 315 | // O(m+n) functions |
| 316 | case overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.AddStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/AddString., overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.AddBytesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/AddBytes.: |
| 317 | // In the worst case scenario, we would need to reallocate a new backing store and copy both operands over. |
| 318 | cost += uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().(float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[0])+actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[1])) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.)) |
| 319 | // O(nm) functions |
| 320 | case overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.MatchesStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/MatchesString.: |
| 321 | // https://swtch.com/~rsc/regexp/regexp1.html applies to RE2 implementation supported by CEL |
| 322 | // Add one to string length for purposes of cost calculation to prevent product of string and regex to be 0 |
| 323 | // in case where string is empty but regex is still expensive. |
| 324 | strCost := uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().((1.0 + float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[0]))) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.)) |
| 325 | // We don't know how many expressions are in the regex, just the string length (a huge |
| 326 | // improvement here would be to somehow get a count the number of expressions in the regex or |
| 327 | // how many states are in the regex state machine and use that to measure regex cost). |
| 328 | // For now, we're making a guess that each expression in a regex is typically at least 4 chars |
| 329 | // in length. |
| 330 | regexCost := uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().(float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[1])) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.RegexStringLengthCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/RegexStringLengthCostFactor.)) |
| 331 | cost += strCost * regexCost |
| 332 | case overloadsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/.ContainsStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/overloads`/ContainsString.: |
| 333 | strCost := uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().(float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[0])) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.)) |
| 334 | substrCost := uint64(mathscip-go gomod github.com/golang/go/src go1.23.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.23.0 math/Ceil().(float64(actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(args[1])) * commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/.StringTraversalCostFactorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/StringTraversalCostFactor.)) |
| 335 | cost += strCost * substrCost |
| 336 | |
| 337 | default: |
| 338 | // The following operations are assumed to have O(1) complexity. |
| 339 | // - AddList due to the implementation. Index lookup can be O(c) the |
| 340 | // number of concatenated lists, but we don't track that is cost calculations. |
| 341 | // - Conversions, since none perform a traversal of a type of unbound length. |
| 342 | // - Computing the size of strings, byte sequences, lists and maps. |
| 343 | // - Logical operations and all operators on fixed width scalars (comparisons, equality) |
| 344 | // - Any functions that don't have a declared cost either here or in provided ActualCostEstimator. |
| 345 | cost++ |
| 346 | |
| 347 | } |
| 348 | return cost |
| 349 | } |
| 350 | |
| 351 | // actualSize returns the size of the value for all traits.Sizer values, a fixed size for all proto-based |
| 352 | // objects, and a size of 1 for all other value types. |
| 353 | func actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(value refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) uint64 { |
| 354 | if sz, ok := value.(traitsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/traits`/.Sizerscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/traits`/Sizer#); ok { |
| 355 | return uint64(sz.Sizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/traits`/Sizer#Size.().(typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Intscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Int#)) |
| 356 | } |
| 357 | if opt, ok := value.(*typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/.Optionalscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Optional#); ok && opt.HasValuescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Optional#HasValue().() { |
| 358 | return actualSizescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/actualSize().(opt.GetValuescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Optional#GetValue().()) |
| 359 | } |
| 360 | return 1 |
| 361 | } |
| 362 | |
| 363 | type stackValscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal# struct { |
| 364 | Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal#Val. refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# |
| 365 | IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal#ID. int64 |
| 366 | } |
| 367 | |
| 368 | // refValStack keeps track of values of the stack for cost calculation purposes |
| 369 | type refValStackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack# []stackValscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal# |
| 370 | |
| 371 | func (s *refValStackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#) pushscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#push().(val refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, id int64) { |
| 372 | value := stackValscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal#{Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal#Val.: val, IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal#ID.: id} |
| 373 | *s = append(*s, value) |
| 374 | } |
| 375 | |
| 376 | // TODO: Allowing drop and dropArgs to remove stack items above the IDs they are provided is a workaround. drop and dropArgs |
| 377 | // should find and remove only the stack items matching the provided IDs once all attributes are properly pushed and popped from stack. |
| 378 | |
| 379 | // drop searches the stack for each ID and removes the ID and all stack items above it. |
| 380 | // If none of the IDs are found, the stack is not modified. |
| 381 | // WARNING: It is possible for multiple expressions with the same ID to exist (due to how macros are implemented) so it's |
| 382 | // possible that a dropped ID will remain on the stack. They should be removed when IDs on the stack are popped. |
| 383 | func (s *refValStackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#) dropscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#drop().(ids ...int64) { |
| 384 | for _, id := range ids { |
| 385 | for idx := len(*s) - 1; idx >= 0; idx-- { |
| 386 | if (*s)[idx].IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal#ID. == id { |
| 387 | *s = (*s)[:idx] |
| 388 | break |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // dropArgs searches the stack for all the args by their IDs, accumulates their associated ref.Vals and drops any |
| 395 | // stack items above any of the arg IDs. If any of the IDs are not found the stack, false is returned. |
| 396 | // Args are assumed to be found in the stack in reverse order, i.e. the last arg is expected to be found highest in |
| 397 | // the stack. |
| 398 | // WARNING: It is possible for multiple expressions with the same ID to exist (due to how macros are implemented) so it's |
| 399 | // possible that a dropped ID will remain on the stack. They should be removed when IDs on the stack are popped. |
| 400 | func (s *refValStackscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#) dropArgsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/refValStack#dropArgs().(args []Interpretablescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#) ([]refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, bool) { |
| 401 | result := make([]refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, len(args)) |
| 402 | argloop: |
| 403 | for nIdx := len(args) - 1; nIdx >= 0; nIdx-- { |
| 404 | for idx := len(*s) - 1; idx >= 0; idx-- { |
| 405 | if (*s)[idx].IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal#ID. == args[nIdx].IDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/Interpretable#ID.() { |
| 406 | el := (*s)[idx] |
| 407 | *s = (*s)[:idx] |
| 408 | result[nIdx] = el.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/interpreter`/stackVal#Val. |
| 409 | continue argloop |
| 410 | } |
| 411 | } |
| 412 | return nil, false |
| 413 | } |
| 414 | return result, true |
| 415 | } |