Skip to content

Commit c653b13

Browse files
Store cppia object fields directly unless a host field needs conversion
HaxeFoundation/hxcpp#1324 stopped cppia linking object field access to a direct memory reference, because at API level 500 a host field can hold a typed function, and storing a script closure into it through an hx::Object pointer left something that is not callable. Every object field access then went through a named lookup, in scripts' own classes and in Haxe 4 builds too, where no field needs that conversion. Bisecting the new cppia benchmarks between hxcpp 4.3.53 and the fork point found it: JIT object creation fell from 102M to 30M objects/s. The direct reference is back for every object field except a host class's field at API level 500, as the PR suggested. It stores with the same write barrier as before, in the interpreter and the JIT. With the previous commit, against hxcpp 4.3.53: JIT object creation 100M (stock 106M), JIT virtual calls 287M (290M), interpreted object creation 17.9M (18.6M) and interpreted virtual calls 38.5M (38.9M). CppiaObjectFields stores object, function and array fields from a script into its own objects and a host object, with minor collections in between, in the interpreter and the JIT. Fixes #247 Fixes #245
1 parent 8a0a4ee commit c653b13

7 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ The first libhxcpp version. It is based on hxcpp 4.3 (the changes listed under 4
9090
* Conservative stack scanning no longer scans the whole large-object list for every stack word in its address range: after several such words in a collection it searches a sorted index, and the range shrinks back when large objects are freed. With 40000 large buffers, a collection with native frames on the stack fell from 2.4-4.6 ms to 0.8 ms
9191
* Programs no longer load user32.dll at startup on Windows, which made starting and exiting about 1.1 ms slower than stock hxcpp: the critical-error dialog now looks MessageBoxA up when it is needed
9292
* cppia safe casts to Int, Float, Bool or String skip their type check when the value's static type already passes it: the check boxed every value and called out of the JIT, so Float array code ran at a fifth of its speed
93+
* cppia sets object fields directly again, except host fields at API level 500, which can hold typed functions: since HaxeFoundation/hxcpp#1324 every object field store went through a named lookup, and script allocation and virtual calls ran at a third to a sixth of hxcpp 4.3.53's speed in the JIT
9394

9495
### Garbage collector
9596

‎src/hx/cppia/Cppia.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4739,6 +4739,7 @@ struct GetFieldByLinkage : public CppiaExpr
47394739
int offset = 0;
47404740
CppiaExpr *replace = 0;
47414741
FieldStorage storeType = fsUnknown;
4742+
bool hostField = false;
47424743

47434744
if (type->haxeClass.mPtr)
47444745
{
@@ -4747,6 +4748,7 @@ struct GetFieldByLinkage : public CppiaExpr
47474748
{
47484749
offset = store->offset;
47494750
storeType = store->type;
4751+
hostField = true;
47504752
DBGLOG(" found haxe var %s::%s = %d (%d)\n", type->haxeClass->mName.out_str(), field.out_str(), offset, storeType);
47514753
}
47524754
}
@@ -4787,6 +4789,21 @@ struct GetFieldByLinkage : public CppiaExpr
47874789
(CppiaExpr*)new MemReference<String,locThis>(this,offset);
47884790
break;
47894791
case fsObject:
4792+
// A host field can hold a typed function at API level 500,
4793+
// which a script closure must be converted to on the way in
4794+
// (HaxeFoundation/hxcpp#1324); other object fields hold the
4795+
// object as it is
4796+
#if (HXCPP_API_LEVEL>=500)
4797+
if (hostField)
4798+
{
4799+
forceNamedAccess = true;
4800+
break;
4801+
}
4802+
#endif
4803+
replace = object ?
4804+
(CppiaExpr*)new MemReference<hx::Object *,locObj>(this,offset,object):
4805+
(CppiaExpr*)new MemReference<hx::Object *,locThis>(this,offset);
4806+
break;
47904807
case fsByte:
47914808
case fsUnknown:
47924809
forceNamedAccess = true;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class HostThing {
2+
public var obj:Dynamic;
3+
public var fn:Void->Int;
4+
public var items:Array<Int>;
5+
6+
public function new() {}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function main() {
2+
new HostThing();
3+
var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]);
4+
for (jit in [false, true]) {
5+
cpp.cppia.Host.enableJit(jit);
6+
try {
7+
cpp.cppia.Host.runFile(script);
8+
} catch (e:Dynamic) {
9+
Sys.println("error: " + e);
10+
}
11+
}
12+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Node {
2+
public var value:Int;
3+
public var next:Node;
4+
public var label:Dynamic;
5+
6+
public function new(value:Int)
7+
this.value = value;
8+
}
9+
10+
class Script {
11+
static function main() {
12+
var host = new HostThing();
13+
var head:Node = null;
14+
for (i in 0...20000) {
15+
var n = new Node(i);
16+
n.next = head;
17+
n.label = {index: i};
18+
head = n;
19+
if (i % 5000 == 0) {
20+
host.obj = new Node(-i);
21+
var captured = i;
22+
host.fn = () -> captured;
23+
host.items = [i, i + 1];
24+
cpp.vm.Gc.run(false);
25+
}
26+
}
27+
cpp.vm.Gc.run(false);
28+
cpp.vm.Gc.run(true);
29+
var count = 0;
30+
var labelsOk = true;
31+
var n = head;
32+
while (n != null) {
33+
if (n.label.index != n.value)
34+
labelsOk = false;
35+
count++;
36+
n = n.next;
37+
}
38+
var hostOk = (host.obj : Node).value == -15000 && host.fn() == 15000 && host.items[1] == 15001;
39+
Sys.println('script fields: ${count == 20000 && labelsOk}, host fields: $hostOk');
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--main Script
2+
-cppia bin/script.cppia
3+
--next
4+
--main Main
5+
-cpp bin
6+
-D scriptable
7+
-D HXCPP_GC_GENERATIONAL
8+
--dce no
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
script fields: true, host fields: true
2+
script fields: true, host fields: true

0 commit comments

Comments
 (0)