Code generation optimizations
- Dominant language
- Bicep
- Stars
- 3.6k
- Forks
- 830
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 79
Description
There are a number of cases where we are generating sub-optimal JSON expressions. When authoring Bicep files from scratch I think it makes to just treat it as an opaque file and not worry too much about content within.
However, when you are converting from the IL into Bicep, you need to verify that the conversion was correct. This can be accomplished by diffing the Bicep-generated template and via what-if. The issues I mention below affect the functionality of the template but they do add some visual noise to it which makes the migration harder.
I'm proposing that we implement the following optimizations to start with and can consider adding more:
## Proposed Optimizations
### Integer loops
Loops relying on the `range()` function such as `[for i in range(0, 3): ...]` generate the expression `range(0,3)[copyIndex()]` for every reference to `i` in Bicep.
Technically there is a workaround for this in the form of `[for (_, i) in range(0, 3): ...]`, but it feels rather awkward to have to do that.
We should optimize the expression for `i` to just `copyIndex()`.
### n-ary functions (`and()`, `or()`, `coalesce()`)
Our codegen currently does not take advantage of `and()`, `or()`, `coalesce()` functions supporting more than 2 parameters. We should do that. This is covered by #1387 as well, but the solution in this issue is more widely applicable.
### Nested `format()`
I don't have an example available right now, but some of our scope logic sometimes generates nested `format()` function calls. We should collapse them into a single format call.
## Implementation Notes
To keep code gen simple, we should implement this as a second pass on generated expressions after code generation runs. It should be done via a `LanguageExpressionVisitor` in the expressions library, so it can be reused elsewhere.
Contributor guide
Research direction
Start in the expressions library by locating LanguageExpressionVisitor and the generated-expression codegen pass. Review the proposed integer-loop, n-ary function, and nested format cases, then assess a reusable second pass for these optimizations. Done means generated expressions avoid the described noise while preserving diff and what-if correctness.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100