David Brown said:
On 26/11/2012 14:01, BartC wrote:
Cache sizes depend on the processor, but an Intel i7 (as an example) has
32 KB level 1 instruction cache as far as I know. I am not saying that
this /is/ a problem in your code, but it certainly could be (and the risk
gets higher as you fill out the stub functions).
OK, I thought they were of the order of 1MB or something.
One advantage of a simple interpreter, is that the code *is* localised,
because usually a few dozen handlers are constantly being executed, no
matter how big the target program is. So maybe I'm already benefitting!
Yes, gcc re-orders your code (assuming, as always, you use appropriate
optimisation levels).
Depending on the version of gcc that you are using, it might also split
your code automatically into "hot" and "cold" code. "hot" code is things
like inner loops, while "cold" code is things like initialisation code.
gcc will re-arrange the order of the code to make "hot" code stick
together - this improves cache locality when running. You can also mark
functions as "hot" or "cold" using attributes, or automatically by using
profiling.
The problem with an instruction dispatcher like I have is that I can't see
how the compiler can guess which are the more heavily used functions; they
all have equal 'weight'.
The information needed for that isn't in the structure of the C source code,
but in the structure of the bytecode program, which is data that is read
into the C program.
So I will try and do something with those inlining attributes when I've
finished development (at the moment it's just a distraction, so those
functions are kept external. Then I will know if any increase/reduction is
speed is do with my code, rather than a different inlining strategy coming
into play).
gcc /can/ inline calls through function pointers (which is pretty
impressive, IMHO), but only when it can tell what the function will be.
It'll have it's work cut out here:
typedef void (*(*fnptr))(void);
do {
(**(fnptr)(pcptr))();
} while (1);
I wouldn't even know where to start! (pcptr[] is an array of mixed-use
integers, some of which will be function pointers, which is set up at
runtime.)