Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

MSVC has the compiler option /hotpatch whick allows the linker option /functionpadmin which modifies the processing of the final binary in such a way that hotpatching should work for valid functions. You can specify the number of reserved bytes to allow for hotpatching. See the link for details.

In general, yes, although you'll need to write your function in assembly to do so.

On the other hand, it looks like what you're doing is micro-optimising your code rather than benchmarking it. BOOLs and conditionals in C++ are really, really fast, and the cost of patching opcodes on modern systems can cause really really surprisingly bad performance penalties (for example, the call to VirtualProtect to make the code writable is going to cost hundreds of thousands more than a single conditional, and you'll force pipeline stalls and cache misses by changing the function inline even if you're running on an embedded system).

So in summary, yes, what you're doing is possible. But unless you are doing this as an "out of interest" excercise or run in a very strange environment where performance of conditionals is critically important but you still write in C, then you probably want to just benchmark your code instead and find the real places where it's slow, instead of going to huge amount of pain and effort to patch things that aren't actually performance critical.

the function is called 10 times a second or more often. So, in order to increase performance

Is your function taking 50-100 milliseconds of time to complete? I mean, is there really a perf problem here? 10 times a second is nothing for simple and regular functions, but can be a lot for some computationally intensive stuff.

There's no universal way of forcing a compiler to do that, to reserve some space. You might be able to find a special way for a specific compiler, but a better approach would be to have multiple versions of the same code or constructing the code on the fly at run time.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top