Skip to content

Commit 8a0a4ee

Browse files
Skip the cppia safe-cast check when the static type already passes
Since #135, a safe cast to Int, Float, Bool or String boxes its value and checks its class, in the interpreter and through a native call in the JIT. Haxe emits such casts on values that are already statically that type, where the check cannot fail, so hot script code paid a box and a call per cast: the JIT Float array benchmark fell from 218M to 31M elements/s at that commit (found by bisecting the new cppia benchmarks), and JIT virtual calls from 66M to 45M calls/s. The check is now kept only when the value's static type could hold something else: a Float from a Float or a non-Bool Int, an Int from a non-Bool Int, a Bool from a Bool and a String from a String need none. Float arrays are back at 219M elements/s in the JIT (stock 220M) and 24M in the interpreter (stock 25.6M), and virtual calls at 71M. CppiaSafeCasts gains statically typed casts, which give the same results compiled, interpreted and jitted. Fixes #246 Part of #245
1 parent 1e97754 commit 8a0a4ee

5 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ The first libhxcpp version. It is based on hxcpp 4.3 (the changes listed under 4
8989
* Array<String>.sort boxes each String once instead of boxing both arguments on every comparison
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
92+
* 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
9293

9394
### Garbage collector
9495

‎src/hx/cppia/Cppia.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,15 @@ struct CastExpr : public CppiaDynamicExpr
12941294
}
12951295
nullValue = op==castInt || op==castFloat ? Dynamic(0).mPtr :
12961296
op==castBool ? Dynamic(false).mPtr : 0;
1297+
1298+
// A value whose static type already passes needs no check
1299+
ExprType from = value->getType();
1300+
bool isBool = from==etInt && value->isBoolInt();
1301+
if ( (op==castFloat && (from==etFloat || (from==etInt && !isBool))) ||
1302+
(op==castInt && from==etInt && !isBool) ||
1303+
(op==castBool && isBool) ||
1304+
(op==castString && from==etString) )
1305+
checked = false;
12971306
}
12981307

12991308
if (op==castDynamic || op==castInt || op==castBool || op==castFloat || op==castString )

‎test/regression/CppiaSafeCasts/Main.hx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,14 @@ class Compiled {
5454
i = -1;
5555
}
5656
Sys.println("typed " + i);
57+
var f:Float = 2.5;
58+
var n:Int = 3;
59+
var s:String = null;
60+
var b:Bool = true;
61+
attempt("typed 2.5 Float", () -> cast(f, Float));
62+
attempt("typed 3 Float", () -> cast(n, Float));
63+
attempt("typed 3 Int", () -> cast(n, Int));
64+
attempt("typed null String", () -> cast(s, String));
65+
attempt("typed true Bool", () -> cast(b, Bool));
5766
}
5867
}

‎test/regression/CppiaSafeCasts/Script.hx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ class Script {
4141
i = -1;
4242
}
4343
Sys.println("typed " + i);
44+
var f:Float = 2.5;
45+
var n:Int = 3;
46+
var s:String = null;
47+
var b:Bool = true;
48+
attempt("typed 2.5 Float", () -> cast(f, Float));
49+
attempt("typed 3 Float", () -> cast(n, Float));
50+
attempt("typed 3 Int", () -> cast(n, Int));
51+
attempt("typed null String", () -> cast(s, String));
52+
attempt("typed true Bool", () -> cast(b, Bool));
4453
}
4554

4655
static function main() {

‎test/regression/CppiaSafeCasts/stdout.txt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ null Float = 0
1313
null Bool = false
1414
caught Int 0
1515
typed -1
16+
typed 2.5 Float = 2.5
17+
typed 3 Float = 3
18+
typed 3 Int = 3
19+
typed null String = null
20+
typed true Bool = true
1621
abc Int throws
1722
1.5 Int throws
1823
7 String throws
@@ -28,6 +33,11 @@ null Float = 0
2833
null Bool = false
2934
caught Int 0
3035
typed -1
36+
typed 2.5 Float = 2.5
37+
typed 3 Float = 3
38+
typed 3 Int = 3
39+
typed null String = null
40+
typed true Bool = true
3141
abc Int throws
3242
1.5 Int throws
3343
7 String throws
@@ -43,3 +53,8 @@ null Float = 0
4353
null Bool = false
4454
caught Int 0
4555
typed -1
56+
typed 2.5 Float = 2.5
57+
typed 3 Float = 3
58+
typed 3 Int = 3
59+
typed null String = null
60+
typed true Bool = true

0 commit comments

Comments
 (0)