-
-
Notifications
You must be signed in to change notification settings - Fork 662
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
Inline Array.map/filter methods #5749
Inline Array.map/filter methods #5749
Conversation
Oops, C# fails at the moment, partly due to #5747 |
abfd40b
to
b242d69
Compare
What's the state here? |
Oh, I have to check. |
b242d69
to
25d9087
Compare
|
Up :) |
Rebased. C# tests still fail with an issue that looks like #5748 |
…ce unsafe versions fail, issue incoming)
59a575a
to
f0f8389
Compare
This PR is affected by 11186ef. Anonymous functions are not inlined anymore. |
This would need an update. |
Updated. Let's do this for 4.0 |
Blocked by #8654 |
Ready to go |
return ret; | ||
} | ||
|
||
public function filter(f:T->Bool):Array<T> { | ||
public inline function filter( f : T -> Bool ) : Array<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange spacing here.
Awesome! For the curious like me, the CPP and JS output of the OP example -- prior to this merge -- generates closures, something like: C++:
JS:
I suspect this won't affect node / chrome / V8 drastically, but CPP (and HL?) could see a nice gain. |
Actually it makes huge difference even on node: import haxe.Timer;
class Main {
public static function main():Void {
var a = [for(i in 0...0xFFF) i];
var b;
Timer.measure(() -> for(i in 0...0xFFF) b = a.map(i -> i));
// Timer.measure(() -> for(i in 0...0xFFFF) b = js.Syntax.code('{0}.map({1})', a, i -> i));
}
} The first line result (with this patch in effect)
The second line result (without this patch)
Edit:
|
Should I disable inlining for js? |
Though for a single |
Ok, I stand corrected! Many performance tweaks I've played with have little affect on V8 (presumably they've invested heavily in optimizations). I expected they'd be able to optimize away the inner function call, but I'm surprised not: https://jsperf.com/inlining-map-function shows 5X faster in my Chrome 73 browser: Interestingly, when I run basically the same test in node, I'm seeing it 26X faster:
I don't have this patch yet, but I'm interesting in CPP and HL. |
Ah, that's because we create new array like |
I wonder if creating an array with |
Checked. It can't. I've pushed array creation via |
As we discussed in Slack, it would be nice to inline array map and filter methods so we can avoid function calls and closure creation when we use anonymous functions.
With this, if we write
a.map(function(i) return i * 2)
, we get:C++
JS:
It's similar in other targets (also note the preallocation optimization for
map
). And it still works through Dynamic/structure typing, because actual Array objects have these methods as well (they are marked with@:runtime
for extern Array's so they really work).In Java I had to use
__get/__set
methods that do bounds checking instead of faster__unsafe_get/set
because of #5748. Let's change that when it's fixed.