Repository navigation
Renaming functions during compile #1282
|
We have been using cel-go for many years and introduced a set of utilities that used cel.Function and cel.Overload, instead of receiver-style custom types. These functions required context from the activation and, since the function/overload does not have access to the activation, we ended up creating a new instance of the library for each call to execute the expression. Example code: // SomeLib constructs a library
func SomeLib(activation cel.Activation) cel.EnvOption {
return cel.Lib(
someLib{
ctx: activation,
})
}
type someLib struct {
ctx cel.Activation
}
func (c someLib) CompileOptions() []cel.EnvOption {
return []cel.EnvOption{
cel.Function(
"someFunc",
cel.Overload("someFuncWithFilter", []*cel.Type{cel.StringType}, cel.DynType,
cel.UnaryBinding(func(filter ref.Val) ref.Val {
// extract some context from the activation using c.ctx
// perform the function and return value
}),
),
),
}
}We then extend the environment with a new instance of someLib per invocation. Given each extend results in copying objects, this has an impact overall on memory utilization. Unfortunately, since this has been in use for many years, replacing Is there any way that we might be able to do this during compile time when the AST is built? |
Replies: 1 comment 1 reply
|
Build the env once and pass the context as an ordinary CEL value instead: If changing all the expressions is painful, do the rewrite while the AST is built with a macro, so existing with Because macros match on name plus arity, do not also declare an old |
cel.Function/cel.Overloadbindings only receive their arguments, not the activation, so a binding that closes over a per-call activation cannot be reused safely. That is why you end up creating a new env on every invocation.Build the env once and pass the context as an ordinary CEL value instead: