Skip to content

gc: allocations are noalias, so array loops are vectorized; a numeric benchmark; parser: no copies of the line table - #805

Merged
ASDAlexander77 merged 3 commits into
mainfrom
claude/vigilant-brahmagupta-9oymny
Nov 10, 2026
Merged

ASDAlexander77 merged 3 commits into
mainfrom
claude/vigilant-brahmagupta-9oymny

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Adds a benchmark of tight numeric loops over arrays, fixes what it found (under -mm=gc none of the loops was vectorized), and removes two hot spots from the parser that a profile of compile time showed.

The gc fix

An array is a header holding the pointer to its elements. Under rc, none and own both come from malloc / realloc, whose results LLVM knows by name to be noalias. Under gc they come from GC_malloc / GC_realloc, which were declared without it, so a store to an element might have written the header: every loop loaded the elements' pointer again on each iteration, and the vectorizer gave up.

GCPass now declares the results of GC_malloc, GC_malloc_atomic, GC_malloc_uncollectable, GC_memalign and GC_realloc noalias.

gc executable, median ms, before → after (Ubuntu 24.04 x64, 4-core cloud VM):

saxpy triad stencil dot matmul hash
before 190.5 225.3 206.9 94.1 91.2 220.4
after 85.5 93.3 97.5 93.2 23.2 44.6

gc is now level with the other three memory models. The ray tracer benchmark is unchanged by it.

The benchmark

tslang/benchmarks/numeric/:

  • nbench.ts: six kernels over flat 4096-element arrays (saxpy, triad, a 3-point stencil, dot, 64x64 matmul, an integer hash), each ending in a check value.
  • rust/, cpp/: ports of the same kernels; every build prints the same six values.
  • bench.sh: builds under every memory model (and under the JIT with JIT_LIB), checks the values, times each kernel by itself.
  • README.md: results, gc before and after, and the JIT before and after Optimizer: code generator's CPU for the cost model, --size_level at every level, inlining of functions with a try off Windows #804 (saxpy, triad, matmul and hash about 20-26% faster under -mm=none, now vectorized for the host's AVX2).

READMEs

  • Main README, Performance: the numeric benchmark's table.
  • benchmarks/raytrace/README.md: results on Linux, including that the baseline-x86-64 Rust port is unexpectedly slow there (not looked into).

The parser (compile time)

From a callgrind profile of compiling a small program that uses the default library:

  • getLineStarts returned the file's table of line starts by value, and computeLineAndCharacterOfPosition and computeLineOfPosition took it by value again: three copies of the table per source location. They pass a reference now. Instructions executed for that compile: 983M → 725M.
  • ^#!.* can only match at the start of the text, but regex_search tried it at every position of every file (3% of the same compile). The first two characters are tested before the regex is used.
  • getDirectiveFromComment took its regex by value, a copy per comment; it takes a reference, and a comment with no @ is not searched.

Testing

Built on Ubuntu 24.04 against the prebuilt LLVM 22.1.8, release test programs, before the parser commit:

  • exe leg (ctest -E '^test-jit'): 3955 of 3955 passed
  • JIT leg (ctest -R '^test-jit'): 2951 of 2951 passed

The parser commit: its first change was built and profiled; the other two had not been built when it was pushed, so this PR's CI is their first build.

Not run locally: the debug test programs, and anything on Windows.

🤖 Generated with Claude Code

https://claude.ai/code/session_01J8KRfVk4FT2FALW47Ux2Fp

Six kernels over flat arrays (saxpy, triad, stencil, dot, matmul and an
integer hash), each ending in a check value every build prints. bench.sh
builds nbench.ts under every memory model, optionally under the JIT, and
the two ports, and times each kernel by itself. The README has the first
results, the JIT before and after the optimizer was given the host's CPU,
and the finding that the gc build's loops are not vectorized.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfVk4FT2FALW47Ux2Fp
… vectorized

An array is a header holding the pointer to its elements. GC_malloc and
GC_realloc were declared without noalias, which LLVM gives malloc's and
realloc's results by name, so a store to an element might have written the
header: every loop loaded the elements' pointer again on each iteration and
none was vectorized. The numeric benchmark ran 2 to 5 times slower under gc
than under the other memory models; it is now level with them.

The README's Performance section has the numeric benchmark's table, and the
benchmarks' READMEs the results on Linux.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfVk4FT2FALW47Ux2Fp
…no regex over a whole file for a shebang

- getLineStarts returned the file's table of line starts by value, and
  computeLineAndCharacterOfPosition and computeLineOfPosition took it by
  value again: three copies of the table for every source location. They
  now pass a reference. Compiling a small program with the default library
  executed 983M instructions, a quarter of them these copies; it is 725M.
- '^#!.*' can only match at the start of the text, but regex_search tried it
  at every position of every file (3% of the same compile). The first two
  characters are tested before the regex is used.
- getDirectiveFromComment took its regex by value, a copy per comment; it
  takes a reference, and a comment with no '@' is not searched.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfVk4FT2FALW47Ux2Fp
@ASDAlexander77 ASDAlexander77 changed the title gc: the collector's allocations are noalias, so loops over arrays are vectorized; a numeric benchmark gc: allocations are noalias, so array loops are vectorized; a numeric benchmark; parser: no copies of the line table Nov 10, 2026
@ASDAlexander77
ASDAlexander77 merged commit d64978e into main Nov 10, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the claude/vigilant-brahmagupta-9oymny branch November 10, 2026 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants