Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0c21849
Compile a cppia script's local functions under the JIT
dimensionscape Nov 27, 2026
faf90bd
Call script closures from cppia scripts without boxing
dimensionscape Nov 27, 2026
da09051
Read object array elements in place under the cppia JIT
dimensionscape Nov 27, 2026
f635250
Add a cppia workload shaped like BunnyMark's update loop
dimensionscape Nov 27, 2026
78ce891
Compare the cppia workloads with HashLink in the bench
dimensionscape Nov 28, 2026
8da1825
Add a cppia workload shaped like an OpenFL application's frame
dimensionscape Nov 28, 2026
81bfce1
Call compiled script closures directly from jitted cppia code
dimensionscape Nov 28, 2026
d88c533
Merge main into feature/cppia-perf-b621b5
dimensionscape Nov 28, 2026
83aaedf
Merge main into feature/cppia-perf-b621b5
dimensionscape Nov 28, 2026
fdd330c
Call unoverridden host methods directly from jitted cppia code
dimensionscape Nov 28, 2026
12e3bb9
Give a scriptable host's static functions typed entries for scripts
dimensionscape Nov 28, 2026
915255b
Merge main into feature/cppia-perf-b621b5
dimensionscape Nov 28, 2026
d74ecf2
Check unused cppia casts to classes and interfaces under the JIT
dimensionscape Nov 28, 2026
152b1bd
Call script closures from host code without boxing their arguments
dimensionscape Nov 28, 2026
2b76f39
Add a cppia workload that sorts a script's Ints with its comparator
dimensionscape Nov 28, 2026
818b6e9
Sort with a script comparator without boxing the values
dimensionscape Nov 28, 2026
7f2efdd
Copy a cppia closure's captures from a list its function keeps
dimensionscape Nov 28, 2026
b7e7e20
Keep the host's typed call into a compiled cppia closure in line
dimensionscape Nov 29, 2026
6862d3e
Merge main into feature/cppia-perf-b621b5
dimensionscape Nov 29, 2026
695a02d
Call host instance methods from jitted cppia code through typed entries
dimensionscape Nov 29, 2026
20dc636
Call host static functions from jitted cppia code through typed entries
dimensionscape Nov 29, 2026
f417921
Keep a typed host call's object and result in registers
dimensionscape Nov 29, 2026
12ed1b3
Box Ints and Floats with the allocator's fast path in line
dimensionscape Nov 29, 2026
ce4bc1a
Check and enter a cppia closure in line from jitted code
dimensionscape Nov 29, 2026
373a966
Add a cppia workload whose closure result is an operator's operand
dimensionscape Nov 29, 2026
142b4d3
Add a cppia workload of a script's map and filter
dimensionscape Nov 29, 2026
5fa2bef
Sort with a script comparator as a run of typed calls
dimensionscape Nov 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions BENCHMARKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ driver also checks that the interpreter and JIT return the same checksum.
| `closure_call` | Script calls to a closure that updates a captured variable |
| `native_call` | Script-to-native calls with an Int argument and result |
| `native_callback` | A native loop calling a script closure that updates a captured variable |
| `bunny_update` | The update loop of OpenFL's BunnyMark over 2,000 script objects that extend a host class, moved through its Float properties, in a listener the host calls once a frame |
| `openfl_frame` | An OpenFL application's frame: 1,000 script objects extending a host display object, moved by an ENTER_FRAME listener the host calls, one in ten with a closure listener of its own, a small object made each update, dead ones replaced, and a host render reading every transform |
| `call_by_name` | Script calls of an abstract's methods over a host type, which the host gives no binding for, as openfl.Vector's are: length, element reads and writes |
| `host_sort` | `Array.sort`, which is host code, sorting a copy of 1,000 script Ints with a comparator the script gives it, ten times a run |
| `host_statics` | Script reads and writes of a host class's static object fields |
| `closure_expr` | Script calls to a closure whose result is an operand of an operator, which takes it as a Dynamic |
| `script_map` | `Array.map` and `filter` on 1,000 Ints, ten times a run, which Haxe inlines into the script as loops calling the closures |

The native timing harness loads one script module per process. Loading, linking,
booting and JIT compilation happen in `setup()`, outside the timed samples.
Expand All @@ -151,6 +158,18 @@ so those flags do not change the native benchmarks. Peak memory for these cases
includes the host, loaded module and any JIT code. The `noop` startup row still
measures the native executable.

### HashLink as a reference

`--hl <path to hl>` also runs the CPPIA workloads on HashLink's JIT. `bench/hl`
builds `Script.hx` and `bench/cppia/shared` straight to HashLink with the same
harness, workload table and timing, so there the host's side of each workload
is HashLink JIT code too, where under cppia it is native C++. Its processes take
turns with the others on the `cppia_jit_` benchmarks and must give the same
checksums, and the CPPIA table gains an HL/JIT column and the JIT's ratio to it.
A host with its framework compiled in should not run a script slower than
HashLink runs the whole application, so a ratio below 1 there is a target.


## Running it yourself

You need Haxe 4.3, the C++ compiler hxcpp uses on your platform, and curl.
Expand All @@ -160,6 +179,7 @@ cd bench
haxe --run Run # every benchmark, both runtimes
haxe --run Run --only map_int,sort_int
haxe --run Run --only cppia_interp_native_call,cppia_jit_native_call
haxe --run Run --only cppia_jit_closure_call --hl path/to/hl # with HashLink beside it
haxe --run Run --no-build # reuse the last builds
haxe --run Run --only gc_object_ids --define HXCPP_GC_GENERATIONAL
haxe --run Run --readme # publish: this page and the README summary
Expand Down
17 changes: 17 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@
libhxcpp 6.1.0 (unreleased)
------------------------------------------------------------

* The cppia JIT compiles a script's local functions, the closures written inside a function or an initializer, as it compiles class functions, when each is first called. No class holds them, so they were never compiled: under the JIT their bodies ran in the interpreter, whether jitted code called them or the host called them back. Jitted code calling a closure is 1.9x faster (closure_call), and the host calling one back 2.5x (native_callback). HXCPP_CPPIA_EAGER_JIT compiles them as the module loads, with the rest (HaxeFoundation/hxcpp#1213)
* A cppia script calling one of its closures, or a bound method held as a value, passes the arguments and takes the result as they are when the function takes the same types, interpreted and under the JIT. Each argument was boxed into a Dynamic for __run and unboxed into the function's frame, and the result boxed and unboxed again, which allocated for most Ints and every Float. Calling a closure that takes and returns an Int is 1.8x faster under the JIT and 1.2x interpreted (closure_call). An argument of another type, more arguments than the function takes, and host functions are boxed as before
* The cppia JIT reads an element of an array of objects, such as an Array of a script class, in place when the element is wanted as an object, as it reads one wanted as anything else. It called a helper that went through the array's virtual __GetItem and built a Dynamic of the element, which an object element does not need. Under the JIT, virtual calls through an array of objects are 1.4x faster (virtual_call) and allocation that reads one 1.2x (alloc_objects). Arrays of Ints, Bools, Floats and Strings still box their elements through __GetItem
* Jitted cppia code calls a script closure whose function takes its arguments and gives its result as they are, and is compiled, as it calls a known function: one check of the closure and its function's signature, its captured variables put in the frame, and a call of the compiled code. The general way through a helper, which compiled the function on its first call and boxed what did not match, is left for everything else. Calling a closure that takes and returns an Int is 1.6x faster under the JIT (closure_call), 0.61x of HashLink 1.16's JIT where it was 0.38x
* Jitted cppia code calling a host method or property accessor on a script class that extends a host class calls the host's method directly, as a super call does, when the object is of that host class and no script class overrides the method. It went through the stub the host generates and the host class's virtual method, which checks the script vtable for an override before it runs the host's. Updating script objects through a host display object's properties, as BunnyMark's loop does, is 1.4x faster under the JIT (bunny_update). Script overrides, inherited ones and a host subclass's own still run
* A cppia script calling a host static function the host gives it no binding for, as with any method of an abstract over a host type such as openfl.Vector, whose implementation class is private, passes the arguments and takes the result as they are. It called the function's Dynamic wrapper, boxing every Int, Float and Bool on the way in and the result on the way out, and converted an Array argument with a dynamic_cast. In a scriptable build, each static function's wrapper now registers a typed entry, made from the function's own C++ signature, that a script links to as it links to a binding, and an Array of the element type wanted is passed without the cast. Calls by name are 5.9x faster under the JIT and 2x interpreted (call_by_name), 0.73x of HashLink 1.16's JIT where they were 0.12x. A scriptable host is larger by about 2% for the entries; other builds are unchanged
* Host code calling a cppia script closure, in a scriptable build, passes Int, Float, Bool and String arguments as they are, interpreted and under the JIT. Haxe 4 makes a function value a Dynamic, so a call such as callback(i) boxed each argument into a Dynamic for __run, which the closure unboxed into its frame. Dynamic::operator(), and the Variant a field of an anonymous structure or a Dynamic is read as, now take those arguments as their own types and call hx::callFunction, a typed call in the new hx/TypedCall.h (HXCPP_TYPED_CALLS), which runs a script closure whose function takes exactly those types through its compiled code, as jitted code calling a closure directly runs it. Any other function value, and any argument a closure takes as another type, is boxed as __run had it, so every closure gets the same values, and calls with only objects keep the old overloads. The host calling back a closure that takes and returns an Int is 1.5x faster under the JIT and 1.1x interpreted (native_callback), 0.30x of HashLink 1.16's JIT where it was 0.20x. The result is still boxed, since generated code uses it as a Dynamic, and is now about a third of such a call. No class changes its layout: code compiled against the older headers, such as a prebuilt static library, keeps calling through __run until it is rebuilt
* Array.sort of Ints, Floats, Bools and the other plain values calls a cppia script comparator that takes two values of the element type with the values as they are, interpreted and under the JIT, in a scriptable build. Sort boxed each value once and called the comparator with the boxes, which the script unboxed again for every comparison. It now sorts the values themselves through hx::callFunction, in the same steps, so the comparator is asked about the same pairs in the same order and the array ends in the same order, whatever it answers, and an Int result is read as it is. Sorting with a script comparator is 2.0x faster under the JIT and 1.3x interpreted (host_sort), 0.43x of HashLink 1.16's JIT where it was 0.22x. A comparator that takes other types, a host function as the comparator, and arrays of objects, Strings and Int64s sort as before
* A cppia closure copies the variables it captures into the frame of each call, and out of the frame that makes it, from a list its function keeps of where each one sits and how big it is, set when the function links, with a move of that size. For each capture it went from the closure to its function, to the function's captured variables, to the variable for its offsets and size, and then called memcpy. Jitted code calling a closure that captures a variable is 13% faster (closure_call), 0.65x of HashLink 1.16's JIT where it was 0.57x, and the host calling one back 5% (native_callback). Interpreted calls are level
* Host code calling a compiled cppia closure back through hx::callFunction, as Dynamic::operator() and Array.sort do in a scriptable build, takes a path that does only what that call needs: the arguments pushed, the captured variables copied and the result written in line, and the compiled code called. It shared one function with the general path, and called out to copy the captures, to write the result and to box it. Under the JIT, the host calling back a closure that takes and returns an Int is 15% faster (native_callback), 0.34x of HashLink 1.16's JIT, and sorting with a script comparator 10% (host_sort). Interpreted calls are level
* Jitted cppia code calls a host instance method that takes up to three Ints, Bools, Floats or objects, and gives one of those or nothing, through a typed entry that takes the object and the arguments in registers, in a scriptable build. It went through the method's script stub, which took the arguments from the script frame and wrote the result back there, by way of a helper that catches what the stub throws. Each instance method's Dynamic wrapper (HX_DEFINE_DYNAMIC_FUNC) now makes two entries: one calling the method virtually, as the stub does, and one calling the class's own, as the stub for a super call does, which a C++ class with pure virtual methods does not get. A scriptable class gives the runtime its methods' entries as it registers, and an entry leaves what the method throws for the script, as the helper does. Updating script objects through a host display object's properties, as BunnyMark's loop does, is 1.56x faster under the JIT (bunny_update), 0.77x of HashLink 1.16's JIT where it was 0.5x, and an OpenFL-shaped frame 7% faster (openfl_frame). Interpreted calls, methods with a String or more than three arguments, and code built against older headers take the stub as before. A scriptable host is larger by about 1.4% for the entries
* Jitted cppia code calls a host static function that takes up to four Ints, Bools, Floats or objects, and gives one of those or nothing, through a typed entry that takes the arguments in registers, in a scriptable build, whether the host gives the script a binding for it or the script calls it by name. Each static function's Dynamic wrapper registers the entry with the function's typed stub. Script-to-native calls are 1.26x faster under the JIT (native_call), ahead of HashLink 1.16's JIT, and calls by name 1.41x (call_by_name), 0.79x of HashLink's JIT where they were 0.56x
* Where every way a host call can go is a typed entry, jitted cppia code keeps the object and the result in registers: a call with no arguments evaluates its object straight into the register the entry takes it in, the result is converted from the register the entry gives it in, and the context's frame, which only a stub reads, is left alone. Each went through the script frame, and the object was read back from it twice. Updating script objects through a host display object's properties is a further 1.18x faster under the JIT (bunny_update), and script-to-native calls 1.17x (native_call)
* An Int or a Float that a Dynamic holds is boxed with the allocator's fast path in line, where new reached it through calls the compiler left out of line, and Ints from -1 to 1023, not 255, share boxes made once. A whole Float still boxes as an Int from -1 to 255 only, so Type.typeof and Std.isOfType see the same types. The host calling back a script closure that returns an Int is 1.18x faster under the JIT and 1.08x interpreted (native_callback, whose results run from 0 to 2046, so about half are shared boxes). Every boxed Int and Float in Haxe 4 code takes the shorter path
* Jitted cppia code calling a script closure checks in line that the value is one, that its function takes the arguments and gives the result as the call has them, that the function is compiled and that its frame fits the stack, and puts this and the captured variables into the frame in line, a word or 4 bytes at a time from a list of moves the function keeps, before it calls the compiled code. It called a helper for all of that on every call, and the native call was most of what the helper cost. Jitted code calling a closure that captures a variable is 10% faster (closure_call). Calls from host code and interpreted calls are level
* Array.sort with a cppia script comparator calls it as a run of typed calls, hx::TypedCallRun in hx/TypedCall.h: what each comparison checked, that the comparator is a script closure taking the values as they are and compiled, and that its frame fits, is checked once, and each comparison then puts the two values and the captured variables in the frame and runs the compiled code. Sorting with a script comparator is 1.15x faster under the JIT (host_sort). Interpreted sorts are level

libhxcpp 6.0.3
------------------------------------------------------------

Expand Down
Loading
Loading