flixel icon indicating copy to clipboard operation
flixel copied to clipboard

Add FlxBuffer to hide Vector<Float> as a Array<T>

Open Geokureli opened this issue 8 months ago • 14 comments

The Problem

Many rendering systems are littered with Array<Float>s meant to be used like so:

rects.push(rect.x);
rects.push(rect.y);
rects.push(rect.width);
rects.push(rect.height);

transforms.push(matrix.a);
transforms.push(matrix.b);
transforms.push(matrix.c);
transforms.push(matrix.d);
transforms.push(matrix.tx);
transforms.push(matrix.ty);

and

for (i in 0...list.length)
{
	final pos = j * 3;
	final charCode = Std.int(list[pos]);
	final x = list[pos + 1];
	final y = list[pos + 2];
	doSomething(charCode, x, y);
}

This can be both hard to read and susceptible to human error compared to using Array<FlxRect>, but not only does the current way greatly reduce objects that need to be garbage collected, many lime/open features specifically require Vector<Float> or Array<Float>, anyway

The Solution

FlxBuffer and FlxBufferArray, under the hood they actually an Array<Float> or Vector<Float>, but when writing code with them, you can easily deal with them as though they were an Array<{x:Float, y:Float}>. Truly the best of both worlds!

The above code, while compiling to nearly identical source code, will look like this:

rects.push(rect);
transforms.push(matrix);

and

for (item in list)
	doSomething(item.charCode, item.x, item.y);

To Do

  • [x] Manually verify the compiled js/cpp never instantiates anonymous structs in any of the utilizing code
  • [ ] Do benchmark tests, comparing the old code vs the new
    • [ ] Html5
    • [ ] hxcpp
    • [ ] hl
    • [ ] neko
    • [ ] flash
  • [ ] Compare general speed of openfl.Vector vs Array
    • [ ] Html5
    • [ ] hxcpp
    • [ ] hl
    • [ ] neko
    • [ ] flash
  • [ ] See if there's a way to automatically detect if changes cause items to be instantiated (might not be possible)

Geokureli avatar Jun 21 '24 19:06 Geokureli