AgonConsole8 / agon-vdp

Official Firmware for the Agon Console8: ESP32 VDP
MIT License
43 stars 17 forks source link

Optimise "buffer adjust" command #134

Open stevesims opened 9 months ago

stevesims commented 9 months ago

the buffer adjust command is arguably rather slow, and the command would be more useful if it were optimised

probably the biggest performance inhibitor is the current "byte by byte" approach, which iterates thru data and has a "switch" statement within that to deal with the operation being performed. flipping that on its head so that the "switch" is removed could potentially lead to performance improvements

TurboVega commented 9 months ago

The idea of flipping the loop on its head is a good one, because the general rule of "do something once, rather than multiple times in a loop" applies. However, that fix is insufficient, because the get_byte() function constantly traverses the list of buffers in order to obtain a single byte of data, meaning that it does that same operation (finding the right buffer) in a loop, rather than once. Thus, get_byte() does not follow the above rule.

I would suggest either of two approaches:

(1) Move the adjustment operation down to the block level, and call it from the adjust() function, indicating the number of consecutive bytes (within that block) to adjust. This implies either passing the operation, and having a switch statement at the block level (outside any loop), or having a separate function for each operation at the block level (with a loop in each function).

(2) Provide a function (if not already) that gets a pointer to the data in a block, and a function (if not already) that gets the size of the block. In the adjust() loop, get those two values (block and size), and use those in a specific loop (in the adjust() function) to that ONE operation on consecutive bytes.

Either approach implies having a separate loop for each operation, so that there is no test (switch or comparison) of the type of operation, inside the byte loop. In the adjust() function, there will still be a loop, but it should go through blocks, not bytes.

Hope that makes sense.