HaxePunk / HaxePunk

Cross-platform desktop, mobile, and WebGL game engine, based on FlashPunk
MIT License
487 stars 125 forks source link

Replace anonymous functions #212

Closed azrafe7 closed 10 years ago

azrafe7 commented 10 years ago

@MattTuttle has already replaced the one for sorting layers, but there's a bunch of them in other sections of the engine code (Emitter, BitmapText, ...) that could possibly be called/recreated every frame. We should replace them with non-anoymous ones (and maybe inline them if suitable).

Also... I know it's a recent commit, but this https://github.com/HaxePunk/HaxePunk/blob/dev/com/haxepunk/debug/LayerList.hx#L158 should, in some way, make use of Scene.layerSort().

MattTuttle commented 10 years ago

The ones in Emitter and BitmapText shouldn't be recreated because the render functions are inlined. Perhaps I am mistaken but I believe they work that way on all targets.

Also, LayerList sorts in the opposite order of Scene.layerSort so I've added a layerSort function.

azrafe7 commented 10 years ago

:thumbsup:

Shouldn't those sort functions be inlined too?

So next step can probably be: "Avoid usage of Dynamic and Std.is()".

MattTuttle commented 10 years ago

Inlining the sort functions won't do any because they get called from the Array.sort function. It's not super fast but generally the layer list is small so it doesn't cause a performance hit.

As for avoiding Dynamic and Std.is() see #208.

azrafe7 commented 10 years ago

Oooh... Nice! You're a step ahead man!

Though I think that the anonymous function thing needs to be investigated more. I tested a simple demo with a GraphicList in Scout and got:

renderList(function (g:Graphic) ...)

dev

renderList(renderBlit, ...)

mod

MattTuttle commented 10 years ago

I'm surprised Flash doesn't inline the function like Neko/C++. Seems weird to have a function that just calls another function but I guess we can do that...

Also, thanks for making me aware of Adobe Scout. I've been wondering if there was a decent Flash profiler out there and I guess there is. :)

MattTuttle commented 10 years ago

I finally figured out why inlines aren't working in Flash. OpenFL disables them in debug mode... Check your debug.hxml generated file and you'll find -no-inline.

Build it for release and the memory issues should disappear.

scriptorum commented 10 years ago

Aha!