chore: use one static dispatch table for nightly tail calls - #55
chore: use one static dispatch table for nightly tail calls#55stevefan1999-personal wants to merge 1 commit into
Conversation
|
Thanks for the PR! I tested this on my machine, and the growth seems to happen only with fat LTO, at least on Linux x86_64. Changing only |
Latest nightly 1.100.0. I did further and go for a more optimized indirect threaded code design. Code should look like this: Notice the: Which is the computed goto that directly dispatch on the next opcode |
Well, you still have dedicated match branches which is another jump tables, but it can be disabled by gcc/clang to prevent jump branch table optimization. So I further improved it to have an explicitly shared opcode table, because that opcode match branch, far as I remember, is still duplicated, because you still need an index to the jump table, and the size shrinked ~18KB far as I remember. The idea is like: let next_op = match next_ip {
Opcode::Add => add,
...
};
become next_op(...);Far as I remember too, each handler will duplicate this pattern and the match branch. Now it becomes: const OPCODE: [usize; N] = [
&add,
...
];
become OPCODE[next_ip](...);So while it technically should be optimized as identical, I don't think this is the case today. So it is originally around 579KB with the |
|
I think my I'm also a little hesitant to add more unstable features and unsafe code for this. I might look into exposing the opcode safely through the instruction representation instead. |
I noticed that compiling with explicit TCO, each tail call will create the jump table for each handler independently, causing a simple Hello World example to go to 14MB. I immediately disable the inline(always) for the handler but I still noticed that each handler still have their own threaded jump table too. So I did it further and used a common one-time static opcode table for a full indirect threading implementation.
Benchmark-wise I did not see any significant regression, but the binary size did shrink from 14MB to 560KB on my system