Archived

This topic is now archived and is closed to further replies.

Insane Optimization (Indirect Function call vs Direct Function call + Direct Jmp)

This topic is 5148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Everybody ''knows'' that calling a function via function pointer is slower than a direct call, and I was just wondering, would it be faster to use an assembly function something like the following than to call a function indirectly:
;Incoming: EAX = Place To Jump
IndirectCall PROC
         sub eax, relative_to
         mov DWORD PTR jmp_loc, eAx
         db 0E9h ;opcode for 32bit relative jump
jmp_loc dw ? ;where to jump to
relative_to: ;location that jump is relative to
ENDP IndirectCall
Assuming of course that code even works (not gonna bother testing it, but the idea is to make an indirect call into a direct one essentially)
I''m thinking that it definitely would not be faster since the processor can''t predict the branch since it changes by the time it gets to it.   

			
		

Share this post


Link to post
Share on other sites
I'm thinking that it definitely would not be faster since the processor can't predict the branch since it changes by the time it gets to it.

Branch prediction works with conditional jumps. Inconditional jumps are unaffected. The main slowdown in indirect calls is that a pipeline bubble forms until the jump target address is known. By computing/fetching it far enough in advance you will minimize that bubble. Your compiler might rearrange the instructions for you, or your CPU may actually do out-of-order execution.

At least, that's the kind of optimizations I would expect a compiler writer to think of.

A warning to hand-optimization afficionados : Make sure you know the specificities of the CPU you are working on. Fewer instructions is not always better.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on November 8, 2003 2:29:42 PM]

Share this post


Link to post
Share on other sites