ID:119963
 
Taday i was trying to figure out if the BYOND Compiler does inline functions.
I wrote a small test to check for inliningof functions in for loops.

#define CAKE1 ;a += 5;c -= b;d = a + b + c;
#define CAKE2 ;a = 0;b = 2;c = 1;d = 0;
#define CAKE3 ;a = 0;b = 2;c = 1;d = 0;


mob
var
a = 0
b = 2
c = 1
d = 0
proc
cake1()
a += 5
c -= b
d = a + b + c

cake2()
a = 0
b = 2
c = 1
d = 0

cake3()
a = 0
b = 2
c = 1
d = 0

verb
show_vars()
world << "[a] [b] [c] [d]"
test_proc(loops as num)
set background = TRUE
world << "test_proc started with [loops] iterations"
var/start = world.time

for(var/i = 0, i < loops, i++)
cake1()
cake2()
cake1()
cake3()
cake1()
cake2()
cake1()
cake3()

world << (world.time - start)

test_prec_macro(loops as num)
set background = TRUE
world << "test_prec_macro started with [loops] iterations"
var/start = world.time

for(var/i = 0, i < loops, i++)
CAKE1
CAKE2
CAKE1
CAKE3
CAKE1
CAKE2
CAKE1
CAKE3

world << (world.time - start)


The purpose of this was testing the overhead of calling functions against using macros, i other words inlining the code.

The test verbs show results when using values of at least 1000 000 loops, and as expected the macro version performs better.

The thing is maybe i did my macros wrong :-O, maybe you can tell me if that is the case ;-)

The strange thing is, that after running both versions one or two times, they seem to get faster, and then after a few time the get back to the old performance. (maybe due to my processor changing FSB Frequency)
Does anyone have some information what kind of optimization regarding function inlining the compiler can do?

A few assumptions so far:
- Functions are not inlned
- Functions are not inlned in for-loops
- for-loops are not inlined (if they contain fuctions????)

Possible reasons:
-BYOND allows to add and remove functions and verbs from/to objects, so the compiler may be unable to assure it is the same function called?

-I did my Macros wrong :-)
Functions are not inlined, to my knowledge and loops are not unrolled. I suspect this is more down to the fact the compiler is perhaps 10 years old now in parts, and is more a monolithic compiler than the kind they teach you of in compiler engineering classes, so semantic analysis phases are kind of awkward to implement.
That's what i feared, and that's why it is quite slow i guess. I guess it has to be done manually, if it is realy needed.
Maybe I'm missing the point, but I can't imagine a game where the overhead of a function call will be a problematic bottleneck. If high performance on computationally intensive tasks is necessary, BYOND was never the right tool for the job.
Most likely you are right, I just wanted to know if does some optimization.
Stephen001 wrote:
Functions are not inlined, to my knowledge and loops are not unrolled. I suspect this is more down to the fact the compiler is perhaps 10 years old now in parts, and is more a monolithic compiler than the kind they teach you of in compiler engineering classes, so semantic analysis phases are kind of awkward to implement.

In parts is a large mistake here, more like, it's changed in parts.
The compiler has been barely touched since Dan left, sadface.