How can I optimize this code? pAmmoOffset is pointer to byte array
*pAmmoOffset = 0x89;
*(pAmmoOffset + 1) = 0x70;
*(pAmmoOffset + 2) = 0x04;
How can I optimize this code? pAmmoOffset is pointer to byte array
*pAmmoOffset = 0x89;
*(pAmmoOffset + 1) = 0x70;
*(pAmmoOffset + 2) = 0x04;
You could try to pipeline 4 bytes at a time on a 32-bit platform. However, I wouldn't be surprised if attempting to do that manually is ultimately slower than what the compiler generated in the first place.
What you're doing is just about as simple as can be. It's doubtful that anything can be done to optimize this further, unless the code you've provided isn't quite the case and you're not writing compile-time constants into those addresses.
I see hardly any reason to optimize your code. But if you must you could try assigning blocks of values like this:
*(int*) pAmmoOffset = 0x08040201;
which is equivalent to:
*pAmmoOffset = 0x01;
*(pAmmoOffset + 1) = 0x02;
*(pAmmoOffset + 2) = 0x04;
*(pAmmoOffset + 3) = 0x08;
You could assign bigger blocks as well using int64 if you need.
*pAmmoOffset++ = 0x89;
*pAmmoOffset++ = 0x70;
*pAmmoOffst = 0x04;
Of course, this modifies the pointer.
Also, if the pointer is a global variable, the generated code will re-read it after each write. To get around this, copy it to a local variable and use it in the write statements.