Skip to content

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

Open
@pohly

Description

@pohly

Feature request checklist

  • There are no issues that match the desired change
  • The change is large enough it can't be addressed with a simple Pull Request
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions