Skip to content

Speed up the IR evaluator and build releases with opt-level 3 - #19628

Merged
fdncred merged 2 commits into
nushell:mainfrom
fdncred:ir_eval_speed
Nov 22, 2026
Merged

fdncred merged 2 commits into
nushell:mainfrom
fdncred:ir_eval_speed

Conversation

@fdncred

@fdncred fdncred commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Description

Scripts that loop, do arithmetic or call custom commands spent most of their time on evaluator bookkeeping. This PR cuts most of it:

  • Register writes no longer allocate. PipelineExecutionData filled its exit-status list with vec![None] for every value. Now only an external's output adds an entry.
  • binary_op runs on the operands in their registers and writes the result in place. Literal loads, variable loads, span and collect_reg also write in place.
  • The compiler drops the load-literal + drain pair after an assignment or let used as a statement, since neither instruction did anything. The drain stays when a jump lands on it, because a value from another branch still needs draining there.
  • The callee's parent stack, which scope variables reads, was a clone of the whole caller stack on every call. Custom command calls now lend the caller's variables to it and take them back afterwards. Closure calls copy only the variables. Argument normalization and the NU_BACKTRACE lookup are skipped when they have nothing to do.
  • Release builds use opt-level = 3 instead of "s".

Before and after

main is bc405a2 with its release profile. The scripts come from shellarena, and the 100x rows run 100 times the iterations. Measured on macOS arm64. Wall time is from hyperfine with the load average under 3, and instructions retired are the median of 3 runs.

script wall, main wall, this PR instructions, main instructions, this PR
empty script 11.7 ms 11.6 ms 85.7M 82.6M
arith (10k iterations) 29.6 ms 18.8 ms 300.2M 169.6M
strings (10k appends) 37.8 ms 25.0 ms 393.3M 241.7M
func (5k custom command calls) 27.2 ms 18.0 ms 277.0M 163.5M
arith 100x 1.77 s 0.73 s 21,193M 8,691M
strings 100x 2.54 s 1.29 s 29,987M 15,567M
func 100x 1.47 s 0.63 s 17,590M 7,675M
40k command and closure calls, caller holds three 100 KB strings 341 ms 175 ms 3,687M 1,868M

Startup didn't change, so short scripts gain less. opt-level = 3 alone cuts instructions by 6-8%, and it grows nu from 81.1 MB to 89.9 MB. Plugins use the same profile, so they grow too. I didn't measure them.

view ir for the arith script drops from 58 to 46 instructions. The first mut and the end of the loop:

main                                      this PR
 1: store-variable  var 74, %0  # let      1: store-variable  var 74, %0  # let
 2: load-literal    %0, empty              2: load-literal    %0, empty
 3: drain           %0                     3: load-literal    %0, empty
 4: load-literal    %0, empty              4: load-literal    %0, int(1)
 5: load-literal    %0, empty             ...
 6: load-literal    %0, int(1)            42: store-variable  var 74, %0
...                                       43: jump            12  # while
52: store-variable  var 74, %0
53: load-literal    %0, nothing
54: drain           %0
55: jump            18  # while

User-facing changes (Release notes)

  • Loops, arithmetic and custom command calls run about twice as fast in longer scripts.
  • view ir no longer shows a load-literal + drain pair after an assignment or let used as a statement.
  • Release builds are optimized for speed instead of size, so nu is about 11% larger.

Additional notes

- Register writes no longer allocate: the exit-status list only gets an
  entry for an external's output.
- `binary_op` computes on the operand values in their registers and
  writes the result in place; literal and variable loads, `span` and
  `collect_reg` update registers in place too.
- The compiler leaves out the `load-literal` + `drain` pair after an
  assignment or `let` used as a statement, unless a jump lands on the
  drain.
- Custom command calls lend the caller's variables to the callee's parent
  stack and take them back afterwards, instead of cloning the whole
  caller stack. Closure calls copy only the variables. Argument
  normalization returns early when there is nothing to rewrite, and
  `NU_BACKTRACE` is looked up only when needed.
- Release builds use `opt-level = 3` instead of "s".
- `drain_statement` checked for a jump target by scanning every label
  in the block, so compiling a block with many branches got quadratic.
  It now compares against the highest label target, which the builder
  tracks as labels are set.
- `Stack::lend_vars` returns a guard that gives the variables back to
  the caller when it drops. Before, a panic in the callee that the
  caller caught (as the completion menu does) left the caller's stack
  with no variables.
- Update the `drain_statement` docs and the `loop`/`while`/`for`
  pseudocode for the skipped drain.
@fdncred fdncred added notes:perf Performance related PRs notes:ready Indicates Ready for Release notes labels Nov 21, 2026
@fdncred
fdncred merged commit 933b486 into nushell:main Nov 22, 2026
16 checks passed
@fdncred
fdncred deleted the ir_eval_speed branch November 22, 2026 19:59
@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:perf Performance related PRs notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant