Tilemap using a BitmapData to store tile info is actually slow


(Abel Toy) #1

So, FlashPunk’s Tilemap uses a BitmapData to store tile info instead of a 2D Array / Vector. According to this website, which did some tests on multiple tile storage implementations, BitmapData is actually slower: http://petervandernoord.nl/blog/?p=216

So, my suggestion would be to change _map:BitmapData to a one-dimensional vector, using calculations to access the tiles.


(Abel Toy) #2

Actually, according to my own hardcore tests, just using a 2D Vector is faster than a one-dimensional one.


(Ultima2876) #3

It’s such a small difference that I don’t see that it’s worth it. On my computer (i5 dual core, 1.8ghz) I got the following results for their 500x500 grid test:

Bitmap: 14ms Bitmap32: 15ms Vector1D: 10ms Vector2D: 16ms

Decreasing code readability to shave off 4ms for such a huge operation doesn’t seem worth it at all to me. How often are we even going to be changing more than a handful of tiles in a frame anyway? Let alone 500x500=250,000 tiles. That kind of volume of tile-changing will only be done in loading screens, at which point if 4ms is going to make a significant % impact on your loading time then it’s probably fast enough anyway :grin:

The tiles are ultimately drawn to a Canvas and rendered that way so there would be no performance difference in rendering, which is the main bottleneck we have. Shaving a couple of milliseconds off of loading times doesn’t seem worth it to me!

Another advantage of using a bitmapdata instead of vectors is that it’s much much easier and faster to load the “map” bitmap onto the GPU and use a shader for tilemaps. This isn’t an issue now but might be when FlashPunk eventually goes Stage3D.


(Abel Toy) #4

Yeah. I also realized Tilemap has a bunch of functions dependant on map being a BitmapData, such as a flood fill.

Still, I’m making a version of Tilemap that only renders the tiles visible on screen. Making a large Tilemap, currently creates a huge BitmapData which isn’t really necessary and consumes too much memory. My version of it contains the full map information but only the portion visible on screen is rendered on the render bitmap data.


(Ultima2876) #5

See, now that’s a good idea - those kinds of optimisations are great and I’m fully in favour of adding that kind of thing. I mean, why doesn’t flashpunk cull rendering for stuff that’s offscreen anyway? That kind of stuff is the kind of optimisation that really counts, rather than using descending for loops or switching data containers to shave off milliseconds of loading time! :slight_smile:


(Abel Toy) #6

Yeah, I was just investigating what was the best data container I could use for my Tilemap, and I decided to post my findings here :stuck_out_tongue:


(Ultima2876) #7

Makes sense - it’s all worth discussion and consideration!