Last active
August 4, 2021 04:19
-
-
Save kfischer-okarin/76de9f2c683a54bc5f056f23978250d7 to your computer and use it in GitHub Desktop.
DR: Performance test array splat/instance var access
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyTile | |
def initialize(x, y, r, g, b) | |
@x = x * 16 | |
@y = y * 16 | |
@w = 16 | |
@h = 16 | |
@path = 'tile' | |
@a = 255 | |
@r = r | |
@g = g | |
@b = b | |
end | |
def draw(ffi_draw) | |
ffi_draw.draw_sprite_4 @x, @y, @w, @h, @path, nil, @a, @r, @g, @b, | |
nil, nil, nil, nil, | |
nil, nil, | |
nil, nil, | |
nil, nil, nil, nil, | |
nil | |
end | |
end | |
class InstanceVarAccessStrategy | |
def self.build_element(x, y, r, g, b) | |
MyTile.new(x, y, r, g, b) | |
end | |
def self.draw(ffi_draw, elements) | |
index = 0 | |
len = elements.size | |
while index < len | |
elements[index].draw(ffi_draw) | |
index += 1 | |
end | |
end | |
end | |
class ArraySplatStrategy | |
def self.build_element(x, y, r, g, b) | |
[x * 16, y * 16, 16, 16, 'tile', 255, r, g, b] | |
end | |
def self.draw(ffi_draw, elements) | |
index = 0 | |
len = elements.size | |
while index < len | |
x, y, w, h, path, a, r, g, b = elements[index] | |
ffi_draw.draw_sprite_4 x, y, w, h, path, nil, a, r, g, b, | |
nil, nil, nil, nil, | |
nil, nil, | |
nil, nil, | |
nil, nil, nil, nil, | |
nil | |
index += 1 | |
end | |
end | |
end | |
class MyTileMapRenderer | |
def initialize(strategy) | |
build_tile | |
@strategy = strategy | |
@elements = 80.times.flat_map { |x| | |
43.times.map { |y| | |
@strategy.build_element(x, y, rand * 255, rand * 255, rand * 255) | |
} | |
} | |
end | |
def draw_override(ffi_draw) | |
start = Time.now.to_f | |
@strategy.draw(ffi_draw, @elements) | |
$last_frame_time = ((Time.now.to_f - start) * 1000) | |
end | |
def primitive_marker | |
:sprite | |
end | |
def build_tile | |
$args.outputs[:tile].width = 16 | |
$args.outputs[:tile].height = 16 | |
$args.outputs[:tile].solids << [0, 0, 16, 16, 255, 255, 255] | |
end | |
end | |
def tick(args) | |
# Change the commented out tile map renderer to try out the other strategy | |
$tilemap_renderer = MyTileMapRenderer.new(InstanceVarAccessStrategy) if args.tick_count.zero? | |
# $tilemap_renderer = MyTileMapRenderer.new(ArraySplatStrategy) if args.tick_count.zero? | |
args.outputs.primitives << $tilemap_renderer | |
args.outputs.primitives << [0, 720, $gtk.current_framerate.to_i.to_s].label | |
args.outputs.primitives << [200, 720, "#{$last_frame_time}ms"].label | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment