Repository navigation
Ray tracer: a benchmark without interfaces at Rust's time; @final interfaces bring the one with interfaces to the C++ port's - #808
Merged
Conversation
… 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
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
…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
Owner
Author
|
CI: "Build and test (Windows, debug programs, exe)" failed on It does not look like this PR's:
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 ( 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A ray tracer benchmark written without interfaces and closures, the way a Rust program with enums and
matchis, 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,@finalon 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)rtbench.ts(interfaces, closures)@final interface ThingAgainst 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 forrtbench.tstake 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 Isays only classes of this module implementI. To make that true the compiler refuses:Ifrom an object literal, another interface's value, orany;I;export(ordeclare) onI, andIbeing@value;I.A value of
Iis then a class object of this module or null. The read of one ofI's methods and every vtable made forIare marked in the IR, and at--opt_level=3the 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@finalclass with a shape tag and aswitch, a surface is an enum, the rest are@valueclasses 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:Thingis@final.bench.ps1builds both;bench.shis the same script for Linux.Compiler
tslang/tslang/transform.cppunless noted.ArrayLayout.h): length and data are loaded through a select between the header and one constant empty header, so a loop hoists both.sqrt,pow,floor, ...) is the LLVM intrinsic, since TypeScript has noerrno.--opt,--emit=exemakes functions that are not exported internal.--opt_level=3(was LLVM's 250).-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.--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@finalinterface the candidates are exact and the indirect call is dropped.@valuetype 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 namedts.final:<I>, and each vtable ofIgets a globalts.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 interfacebeside@final class.tslang/benchmarks/raytrace/README.md: every step with its measurements.Testing
ctest: all pass (Linux, release programs).00final_interface.tsunder 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).00rc_local_alias_no_count.ts, whose callee releases the array an alias names.00fresh_objects_folded.ts. Its callback is now inlined intokeep, where its read ofv.xwas a load the test's check took for one of the fresh objects' fields; the callback no longer reads the field.tslang/benchmarks/numeric: the six kernels take the same time with the old and the new compiler.For the reviewer
@final interfacerests on the list of refused casts being complete. I refuse tuples, object literals, other interfaces,any,unknownand 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 interfacecannot be exported in this version, and no class implementing one can be: stricter than needed for a@finalexported class, chosen because it is simple to check.--opt_level2 or 3; code size and compile time of a large program were not measured, and no program but the benchmarks was timed.@finalthe 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_).tsret_andtsrel_. A wrong answer is a use after free. It does not consider an object released by another thread while the alias is in use.@valuerecord now has undefined numbers; code that read them (none found by the tests) would see garbage where it saw zero.exportwould no longer link.bench.ps1change.🤖 Generated with Claude Code
https://claude.ai/code/session_01J8KRfZ6cLp88qLss3XkUxb