mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-13 03:42:35 -06:00
037c247b73
Avoid wrapping expressions in comprehensions if they are known
to be defined. References that can't be undefined does not need
to be wrapped in a comprehension for safety, and as this incurs
a runtime cost, we should avoid it if we can! The goal is of course
to make interpolated strings the most performant option of all!
We're not there yet, as the recursive eval implementation remains
slower than e.g. sprintf or concat. We'll get there next! This PR
ensures that there's at least no runtime penalty passed from compilation
by not rewriting:
- A reference to a rule with default assignment
- A reference to a "constant" rule — single definition, ground value (`pi := 3.14`)
- A plain variable reference (`x`, not `x.y`)
In the case of constant rules, we could go one step further next,
and skip even a local assignment in favor of adding their value
directly to the interpolation, e.g:
```rego
x := 1
r := $"{x} + 2"
```
Would simply be rewritten like:
```rego
internal.template_string(["1" " + 2"], __local0__)
```
But saving that for another day.
Signed-off-by: Anders Eknert <anders.eknert@apple.com>