Compile string constants into unique.Handle[string]
Feature request checklist
- [X] There are no issues that match the desired change
- [X] The change is large enough it can't be addressed with a simple Pull Request
- [X] If this is a bug, please file a Bug Report.
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.
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?
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.