Search code examples
c++performanceif-statementhotpatching

Adding N count of bytes at the end of a function for hotpatching


Is it possible to add N count of bytes at the end of the function?

My simple idea is to add the following code:

_asm {
    NOP
    NOP
    NOP
    NOP
    NOP
}

Are there any other ways to do it? (with code, compiler or other methods)

I need it for the hotpatching the function. I have a function that has some IF statements, the function is called 10 times a second or more often. So, in order to increase performance I need to make less checks like "do I need to execute that code?". The boolean in IF statement is not changed so often (i'd say very rarely). I also want to achieve that if I don't need to execute some code, I don't need to check for that.


Solution

  • You could write the function with a one point return and add in NOPs before the return statement. Although this is platform dependent.

    Another method is to place garbage code before the return statement and jump around the garbage code using a label and a goto.

    Be aware of compiler and linker optimizations that may remove unused code.