-
-
Notifications
You must be signed in to change notification settings - Fork 661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enum/Switch Fusion #4386
Comments
Yes, yes, yes! This bugs me quite a lot. Consider this: http://try.haxe.org/#e0EC4
That"s over an order of magnitude. I can live with having to optimize bottlenecks to not use enums, but if it can be fast out of the box, that"d be great. |
It looks nice, but I have no idea how to implement that. |
Could we somehow inline enum constructors similar to what we do with array and object declarations? |
Perhaps, but that"s not really the issue here. |
Could someone post a practical example of this which makes sense in the real world? |
@Simn how about this? function onKeyDown(key) {
if(key == "up") walk(Up);
if(key == "down") walk(Down);
if(key == "left") walk(Left);
if(key == "right") walk(Right);
}
function walk(direction:Direction) switch direction {
case Up: player.y --;
case Down: player.y ++;
case Left: player.x --;
case Right: player.x ++;
}
enum Direction {
Up; Down; Left; Right;
}
This example is more or less extracted and modified from here. See how |
That is not the same issue and already produces good output if you inline |
here is another example: import haxe.ds.Option;
abstract Maybe<T>(Null<T>) {
public function new (x:Null<T>) this = x;
@:analyzer(optimize) public inline function match ():haxe.ds.Option<T> {
return this != null ? Some(this) : None;
}
}
class Test {
@:analyzer(optimize) static function main () {
var m = new Maybe(null);
var x = switch (m.match()) {
case None: 0;
case Some(x): x;
}
trace(x);
}
} becomes var x;
var _g = m != null?haxe_ds_Option.Some(m):haxe_ds_Option.None;
switch(_g[1]) {
case 0:
x = _g[2];
break;
case 1:
x = 0;
break;
} instead of var x = (m != null ? m : 0); |
It would be nice to add the Enum/Switch Fusion feature, which eliminates intermediate
enum
andswitch
. It is similar to inlining, which eliminates intermediate function call, and brings zero-cost abstraction.Here is a sample code to show how
enum
andswitch
should be optimized.http://try.haxe.org/#2b610
The original code:
In that program, the
sqrt
function generatesOption<Float>
. The functiontrace_sqrt
usessqrt
and immediately decompose the enum withswitch
expression.trace_sqrt_fused
is a comparison case that doesn"t useOption<Float>
.The following is the compiled code (with Full DCE and Analyzer options):
trace_sqrt
uses intermediate data structures, so it looks less efficient thantrace_sqrt_fused
.The fused version would be acquired like the following steps.
The text was updated successfully, but these errors were encountered: