google / cel-go

Fast, portable, non-Turing complete expression evaluation with gradual typing (Go)
https://cel.dev
Apache License 2.0
2.2k stars 218 forks source link

Compile string constants into unique.Handle[string] #1018

Open pohly opened 1 week ago

pohly commented 1 week ago

Feature request checklist

Change

Go 1.23 added support for internalizing strings with a standard API (https://pkg.go.dev/unique). When compiling an expression, the AST could store string constants as unique.Handle[string]. Conversion to a normal string could happen on the fly.

Example

I'm implementing a custom map with strings as key:

func (d deviceAttributeDomains) Contains(index ref.Val) ref.Val {
    strKey, ok := index.ConvertToType(types.UniqueStringType).Value().(unique.Handle[string])
    if !ok {
        return types.False
    }
    _, ok = d[strKey]
    return types.Bool(ok)
}

With unique strings, the map becomes more efficient.

Alternatives considered

A normal string extracted from CEL could be turned into a handle before looking it up in a map which uses internalized strings. But that causes overhead during each evaluation which could be moved into the one-time compilation.

TristonianJones commented 1 week ago

It's an interesting feature. I imagine it'd be most useful for literals and the AST. Is that what you have in mind as well? Or something else?

pohly commented 1 week ago

Yes, exactly that.

In my experience, unique.Make can be expensive. But if it is done once and the result is used often, it pays off. Using internalized strings might have to be a compile option because the user should decide about this tradeoff.