The question of caching usefulness was raised. It obviously depends on how much math/etc you are doing, but, even in the case of a simple bit of math, on a small number of LEDs, I see sufficient improvements to make it seem worthwhile, so more math, more LEDs, it would only improve.
var cache = array(pixelCount)
var usecache = 0
for (i = 0; i < pixelCount; i++){
cache[i] = array(3) // to ensure it's a "array within an array" cache,
// a simple array lookup might be even faster, but this allows for multiple cached values
}
export function render2D(index,x,y) {
if (usecache == 0) {
a = hypot(x-.5,y-.5)
cache[index][0] = a
if (index == pixelCount - 1) { usecache = 1} // change from 1 to 0 to disable using cache
} else {
a = cache[index][0]
}
hsv(a, 1, 1)
}
Cache enabled v3, 256 leds 16x16 matrix: 100 fps
Cache disabled same setup: 86 fps
Cache disabled, and array line cache[index][0] = a
commented out too: 91.5 FPS
So, by removing the array write and not using the cache, we are still slower than the cached version, but we still have logic in there, and variables.
So now, let’s do the minimal version:
export function render2D(index,x,y) {
hsv(hypot(x-.5,y-.5), 1, 1)
}
and that’s 103.5 FPS. Hmm…
FPS golfing is called for, I think. I suspect caching makes sense in non-trivial cases, where lots of math or other logic happens. But removing variables, and doing it as directly as possible COULD be faster, or at least equivalent. But that’s just one built in function (hypot)… what if we add 5 or 6 math operations?
export function render2D(index,x,y) {
hsv(hypot(x-.5,y-.5), sin(x+y), cos(x-y)*(x+y))
}
that’s 90.7 FPS
var cache = array(pixelCount)
var usecache = 0
for (i = 0; i < pixelCount; i++){
cache[i] = array(3) // to ensure it's a "array within an array" cache,
// a simple array lookup might be even faster, but this allows for multiple cached values
}
export function render2D(index,x,y) {
if (usecache == 0) {
a = hypot(x-.5,y-.5)
b = sin(x+y)
c = cos(x-y)*(x+y)
cache[index][0] = a
cache[index][1] = b
cache[index][2] = c
if (index == pixelCount - 1) { usecache = 1} // change from 1 to 0 to disable using cache
} else {
a = cache[index][0]
b = cache[index][1]
c = cache[index][2]
}
hsv(a, b, c)
}
putting that into a caching version, where we cache a,b,c: 87.5 FPS
Closing to parity, but still slightly slower.
Timing the reading of an array of an array, comparing it to various math timing, might show us that there is a lower bound where we should do the math rather than store it and read it back… TBD.