# Sema: optimize runtime array_mul · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/d99bed1b10f85f2becca2a1c2587e1c2cb1968e6)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: d99bed1b10f85f2becca2a1c2587e1c2cb1968e6

Requested commit: d99bed1b10f85f2becca2a1c2587e1c2cb1968e6

Commit: d99bed1b10f85f2becca2a1c2587e1c2cb1968e6

Tree: e23b0ea094d2c4541c8dfa17d2a8681e892349ef

Author: mlugg

Committer: Andrew Kelley

## Message

```
Sema: optimize runtime array_mul

There are two optimizations here, which work together to avoid a
pathological case.

The first optimization is that AstGen now records the result type of an
array multiplication expression where possible. This type is not used
according to the language specification, but instead as an optimization.
In the expression '.{x} ** 1000', if we know that the result must be an
array, then it is much more efficient to coerce the LHS to an array with
length 1 before doing the multiplication. Otherwise, we end up with a
1000-element tuple which we must coerce to an array by individually
extracting each field.

Secondly, the previous logic would repeatedly extract element/field
values from the LHS when initializing the result. This is unnecessary:
each element must only be extracted once, and the result reused.

These changes together give huge improvements to compiler performance on
a pathological case: AIR instructions go from 65551 to 15, and total AIR
bytes go from 1.86MiB to 264.57KiB. Codegen time spent on this function
(in a debug compiler build) goes from minutes to essentially zero.

Resolves: #17586

```

## Parents

- [a1d688b86aeba7ab72ae12679ff04a7730931af3](https://git.cafe/gitcafe/zig/commit/a1d688b86aeba7ab72ae12679ff04a7730931af3?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/d99bed1b10f85f2becca2a1c2587e1c2cb1968e6?format=markdown)
