Skip to content

Ray tracer: a benchmark without interfaces at Rust's time; @final interfaces bring the one with interfaces to the C++ port's - #808

Merged
ASDAlexander77 merged 10 commits into
mainfrom
claude/wonderful-hawking-lgndml
Nov 11, 2026
Merged

ASDAlexander77 merged 10 commits into
mainfrom
claude/wonderful-hawking-lgndml

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

What

A ray tracer benchmark written without interfaces and closures, the way a Rust program with enums and match is, its Rust counterpart, and optimizer changes that bring it to that Rust program's time under every memory model. For the existing benchmark with interfaces: two optimizer changes and a language addition, @final on an interface.

Ubuntu 24.04 x64, a 4-core cloud VM, median of 21 runs; a few percent is noise there.

rtbench_static.ts (no interfaces) before after Rust, enums: 50-52 ms
tslang gc 60 ms 51 ms 1.0x
tslang rc 62 ms 51 ms 1.0x
tslang none 61 ms 51 ms 1.0x
tslang own 60 ms 50 ms 1.0x
rtbench.ts (interfaces, closures) before direct calls tried first and @final interface Thing
tslang gc 84 ms 77 ms 71 ms
tslang none 83 ms 78 ms 71 ms
tslang rc 119 ms 111 ms 106 ms
tslang own 87 ms 82 ms not measured in a quiet run

Against these: the C++ port (unique_ptr<Thing>) 68-73 ms, the Rust port (dyn Thing) 116 ms. The Rust port is slow on this machine for the instructions it executes; on Windows it was level with the C++ port (57 and 52 ms, tslang 73-77 ms on 2026-11-10). Nothing here was run on Windows, so for rtbench.ts take the C++ port as the target: gc and none are at it within this machine's noise, rc is about 1.5x.

Language: @final interface

@final interface I says only classes of this module implement I. To make that true the compiler refuses:

  • a cast to I from an object literal, another interface's value, or any;
  • an interface that extends I;
  • export (or declare) on I, and I being @value;
  • an exported, imported or declared class that implements I.

A value of I is then a class object of this module or null. The read of one of I's methods and every vtable made for I are marked in the IR, and at --opt_level=3 the call becomes the functions in that slot of those vtables (at most four), the last called without a test: no call through a pointer is left.

Benchmark

  • tslang/benchmarks/raytrace/rtbench_static.ts: a thing is one @final class with a shape tag and a switch, a surface is an enum, the rest are @value classes and free functions. One file builds under every memory model.
  • tslang/benchmarks/raytrace/rust/src/bin/static_dispatch.rs: the Rust program it mirrors.
  • rtbench.ts, rtbench_own.ts: Thing is @final.
  • bench.ps1 builds both; bench.sh is the same script for Linux.

Compiler

tslang/tslang/transform.cpp unless noted.

  • Array reads without a branch (ArrayLayout.h): length and data are loaded through a select between the header and one constant empty header, so a loop hoists both.
  • Math calls as intrinsics: a call of a C math function (sqrt, pow, floor, ...) is the LLVM intrinsic, since TypeScript has no errno.
  • Internal functions in an executable: with --opt, --emit=exe makes functions that are not exported internal.
  • Inliner threshold 325 at --opt_level=3 (was LLVM's 250).
  • SROA and jump threading before the first SimplifyCFG at levels 2 and 3.
  • A select over a phi made a branch again at levels 2 and 3, after a function's callees are inlined, when its condition is decided for one of the phi's constants; jump threading follows.
  • -mm=rc: a local alias that needs no count at levels 2 and 3, before the inliner: a retain of a local and its release on every path are removed when the function and everything it calls can release nothing.
  • Direct calls tried first at --opt_level=3, before the inliner: a call through a function value whose type at most four address-taken functions of the module have tests the pointer against each and calls it directly; the indirect call is kept for anything else. For a method of a @final interface the candidates are exact and the indirect call is dropped.
  • null of a @value type writes only its pointers (CastLogicHelper.h): its numbers are left undefined.
  • @final interface (MLIRGenInterfaces.cpp, MLIRGenClasses.cpp, MLIRGenCast.cpp, LowerToLLVM.cpp): the checks above; the method read is a GEP over a struct named ts.final:<I>, and each vtable of I gets a global ts.final:<I>:<vtable> holding its address.

Docs

  • README.md: both benchmarks in the performance section.
  • docs/raytrace-rust-parity.md: where it stands, what was done, open questions, what is left, how to measure.
  • docs/own-memory-model.md: @final interface beside @final class.
  • tslang/benchmarks/raytrace/README.md: every step with its measurements.

Testing

  • ctest: all pass (Linux, release programs).
  • New: 00final_interface.ts under every memory model and the JIT (a class that declares the interface, a derived class, a class that does not declare it, null), and three error tests (object literal, extends, export).
  • New: 00rc_local_alias_no_count.ts, whose callee releases the array an alias names.
  • Changed: 00fresh_objects_folded.ts. Its callback is now inlined into keep, where its read of v.x was a load the test's check took for one of the fresh objects' fields; the callback no longer reads the field.
  • Every build of both benchmarks prints the same checksum.
  • tslang/benchmarks/numeric: the six kernels take the same time with the old and the new compiler.

For the reviewer

  • @final interface rests on the list of refused casts being complete. I refuse tuples, object literals, other interfaces, any, unknown and opaque values; a path that makes an interface value some other way (I found none) would be called through the wrong function. A vtable filled when the program starts (no constant initializer) makes the pass leave the call alone.
  • @final interface cannot be exported in this version, and no class implementing one can be: stricter than needed for a @final exported class, chosen because it is simple to check.
  • With more than four implementers nothing is done for the call.
  • The inliner threshold, the direct-call pass (up to four tests and calls at a call through a pointer) and the extra passes apply to every program at --opt_level 2 or 3; code size and compile time of a large program were not measured, and no program but the benchmarks was timed.
  • Without @final the direct-call pass picks candidates by LLVM function type, so an unrelated function of the same type is tried too; it skips the runtime's routines by name (tsrel, tsret, __ts, ts_).
  • The rc pass decides by its own reading of the LLVM IR what a function can release, and finds the pair by the names tsret_ and tsrel_. A wrong answer is a use after free. It does not consider an object released by another thread while the alias is in use.
  • A null @value record now has undefined numbers; code that read them (none found by the tests) would see garbage where it saw zero.
  • Internal linkage: a program whose other objects call a function of the main module by name without export would no longer link.
  • Nothing was run on Windows, including the bench.ps1 change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb

… port with enums

rtbench_static.ts is rtbench.ts with no interfaces and no closures: a thing is
one @Final class with a shape tag and a switch, a surface is an enum, the rest
are @value classes and free functions. rust/src/bin/static_dispatch.rs is the
Rust program it mirrors (enum Thing, enum Surface, match). bench.ps1 builds
both, and bench.sh is the same script for Linux.

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

Hit names the thing by its index, which -mm=own accepts; locals that are never
reassigned are const. README: what the two new programs are and their timings.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb
…ternal functions in an executable, inline threshold 325

- An array's length and data are read through a select between the header and
  a constant empty header, not a branch on null, so a loop hoists both.
- A call of a C math function (sqrt, pow, floor, ...) is the LLVM intrinsic:
  TypeScript has no errno.
- With --opt, --emit=exe makes functions that are not exported internal, so
  the inliner knows every caller.
- The inliner's threshold at --opt_level=3 is 325.

Test suite still running; benchmark figures follow.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb
A path that knows its answer (a phi of floating-point constants and a test of
it) was made a select by SimplifyCFG, which jump threading does not unfold.
At --opt_level 2 and 3 the pipeline now starts with SROA and jump threading.
Ray tracer README: the timings, and that the 32-bit counter was not a cost.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb
…-rust-parity.md: what is left

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb
…nch again; the ray tracer takes Rust's time

After a function's callees are inlined, a select whose condition compares a phi
of its block and is decided for one of the phi's constants becomes a branch,
and jump threading sends those predecessors past it. rtbench_static.ts: 53 to
50 ms under gc, none and own (Rust 50.4 ms); rc 57 to 53 ms.
README, the benchmark's README and docs/raytrace-rust-parity.md updated.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb
@ASDAlexander77 ASDAlexander77 changed the title Ray tracer benchmark without interfaces, and optimizer changes that bring it within 6% of Rust Ray tracer benchmark without interfaces, and optimizer changes that bring it to Rust's time Nov 11, 2026
…mes; the ray tracer takes Rust's time under every model

Under -mm=rc, before the inliner, a retain of a local (tsret_<type>(slot)) and
its release on every path are removed when the function and everything it
calls store only into locals and call nothing that could release. The ray
tracer's intersections counted the array of things up and down on every ray:
rtbench_static.ts under rc 53 to 50.6 ms (Rust 49.6 ms).
Test: 00rc_local_alias_no_count.ts, with a callee that releases the array.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb
…ly; null of a @value type writes only its pointers

- --opt_level=3, before the inliner: a call through a function value whose
  type at most four address-taken functions of the module have tests the
  pointer against each and calls it directly; the indirect call is kept for
  anything else. The runtime's own routines are not candidates.
- null of a @value type that holds null in a pointer is its pointers null and
  its numbers undefined, not every field zeroed.
rtbench.ts: 84 to 77 ms under gc, 83 to 78 under none, 87 to 82 under own,
119 to 111 under rc (Rust dyn Thing 116 ms, C++ 70 ms on this machine).
00fresh_objects_folded.ts: the callback, now inlined into keep, reads no field.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb
@ASDAlexander77 ASDAlexander77 changed the title Ray tracer benchmark without interfaces, and optimizer changes that bring it to Rust's time Ray tracer: a benchmark without interfaces at Rust's time, and the one with interfaces ahead of the Rust port Nov 11, 2026

Copy link
Copy Markdown
Owner Author

CI: "Build and test (Windows, debug programs, exe)" failed on 2dd49b8 (two commits behind the head) in one test of 3971, test-compile-none-corpus-00object: the program compiled and its output had no done. line; the log has nothing more.

It does not look like this PR's:

  • the job compiles the test programs without optimization, and the only code that commit changed is a pass in transform.cpp that runs at --opt_level 2 and 3;
  • the commit before it (e7c10c4), which gives the same code for an unoptimized program, passed this job, and the other seven jobs passed on 2dd49b8.

I have not found the cause, so "flaky" is a guess. I re-ran the failed job; the same job is also running on the current head (8bb570e).


Generated by Claude Code

…its methods are called directly

- @Final interface I: a value of I is made from a class object or null. A cast
  from an object literal, another interface's value or any, an interface that
  extends I, export, and an exported or declared class implementing I are errors.
- The read of a method of I and every vtable made for I are marked in the IR; at
  --opt_level=3 SpeculativeDirectCallPass takes the functions in the method's
  slot of those vtables (at most four) and calls the last without a test, so no
  call through a pointer is left.
- rtbench.ts and rtbench_own.ts: @Final interface Thing. 75.5 to 70.7 ms under
  gc and none (C++ port 68-73 ms on this machine), 111.6 to 105.5 under rc.
Tests: 00final_interface.ts under every model, three error tests.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb
@ASDAlexander77 ASDAlexander77 changed the title Ray tracer: a benchmark without interfaces at Rust's time, and the one with interfaces ahead of the Rust port Ray tracer: a benchmark without interfaces at Rust's time; @final interfaces bring the one with interfaces to the C++ port's Nov 11, 2026
@ASDAlexander77
ASDAlexander77 merged commit b835b26 into main Nov 11, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the claude/wonderful-hawking-lgndml branch November 11, 2026 13:55
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