gauauu wrote:Ugh. I think the important lesson here seems to be: cc65 isn't very efficient. If performance matters in the section of code you're working on, assembly is the answer.
Well, performance matters everywhere in the actual action gameplay. But I still wouldn't want to write all this in pure Assembly. That's why I use inline Assembly when necessary and C for the rest. (And a bunch of low level pure Assembly code files, mostly for general purpose functions or NES-hardware-related things.)
However, arrays and pointers are really the only stuff that I noticed that are turned into unnecessarily complicated code.
Pointers are always copied to ptr1, even if your pointer is in the zeropage itself.
And arrays, as we see now, produce bad code whenever either the index or the assigned value isn't an exact byte value, but something that gets promoted to int due to calculations and stuff.
But other than arrays and pointers, I have yet to see a situation where the generated Assembly code from C is so bad that I'd have to use Assembly to cut down the required ROM size and CPU time by a huge amount.
rainwarrior wrote:Basically what you're looking at is that the code generated by assigning a value to an array element generates a known pattern. As soon as you put some extra operations on the value to be assigned, more code ends up inside that doesn't match the pattern.
I'm just a bit confused why even the assigned value makes a difference in calculating the array itself.
The array index, o.k., this one makes sense: Having a byte index or an int index makes a huge difference in the processing. And calculations always get promoted to int, so yeah, I understand why array[byteIndex + 1]
has an issue over array[byteIndex]
But I just don't understand why the assigned value
makes a difference on how the array's offset is calculated.
Well, looks like I have to create a macro:
Code: Select all
/* Only use this if the calculated value is actually calculated at runtime.
Example: array[index] = variable + 1;
If you assign an unaltered variable or a constant value known at compile time:
array[index] = variable;
array[index] = 1;
you can access the array directly.
This macro is only there because the generated code gets large otherwise. */
#define SetArrayValueFromCalculatedValue(array, index, calculatedValue, tempVariable)\
(tempVariable) = (calculatedValue);\
(array)[index] = (tempVariable);\