-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
optimize state machine inside loop into computed goto #2162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Performance of computed goto highly depends on processor and the task. It is not always faster then the ordinary switch. Perhaps the compiler should be given a hint in form of designated tests. Depending on timing of these tests, it would then pick up the best performing implementation. This topic gets sometimes discussed on Forth forums, they found that the old truths are not absolute, as they once were. |
@PavelVozenilek |
Profile guided optimization is #237 |
I personally am not a fan of having to rely on the optimizer to do the right thing, and then trying to fool it into doing the right thing when it doesn't. While allowing the compiler to make this optimization is a good idea, I feel that that is not sufficient to cover the computed goto use case and some explicit mechanism should still be available. |
@shawnl: as I understand it, computed goto performance advantage depends on ability of the processor to predict which code path would be executed next (and start speculative execution of this path in advance). With one big switch such prediction is not feasible. In Forth interpreter code paths are few and short, and common patterns of code are frequent. The processor prediction could then make a big difference. I do not see how inlining could play a role here, but could be easily wrong. |
A good benchmark for this issue would be a |
Closing in favor of #8220 |
Related issues:
One remaining use case for computed goto is performance. This pattern:
Here the code relies on setting the state variable and a loop iteration. In C the code could directly goto another switch label. In Zig this is not available, and the code generated is worse than in C due to the optimizer's inability to detect this pattern.
Upstream bug, reported by @shawnl: https://bugs.llvm.org/show_bug.cgi?id=39043
Potential solutions include:
I don't consider this to be blocking a 1.0.0 release.
The text was updated successfully, but these errors were encountered: