Avoid unwinding the stack on hot path in method call lookups#15002
Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom Sep 16, 2024
Merged
Avoid unwinding the stack on hot path in method call lookups#15002straight-shoota merged 1 commit intocrystal-lang:masterfrom
straight-shoota merged 1 commit intocrystal-lang:masterfrom
Conversation
The method lookup code uses exceptions to retry lookups using auto-casting. This is effectively using an exception for execution control, which is not what they are intended for. On `raise`, Crystal will try to unwind the call stack and save it to be able to report the original place where the exception was thrown, and this is a very expensive operation. To avoid that, we initialize the callstack of the special `RetryLookupWithLiterals` exception class always with the same fixed value.
ysbaddaden
approved these changes
Sep 15, 2024
straight-shoota
approved these changes
Sep 15, 2024
straight-shoota
added a commit
that referenced
this pull request
Sep 21, 2024
This is a follow-up on #15002 which explicitly assigns a dummy callstack to `RetryLookupWithLiterals` for performance reasons. `CallStack.empty` is intended to make this use case a bit more ergonomical. It doesn't require allocating a dummy instance with fake data. Instead, it's an explicitly empty callstack. This makes this mechanism easier to re-use in other places (ref #11658 (comment)).
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The method lookup code uses an exception to retry lookups using auto-casting. This is effectively using an exception for execution control, which is not what they are intended for (in Crystal). On
raise, Crystal will try to unwind the call stack and save it to be able to report the original place where the exception was thrown, and this is a very expensive operation. To avoid that, initialize the callstack of the specialRetryLookupWithLiteralsexception class always with the same fixed value.This cuts the compilation times for the compiler itself in about 1 second (both using a development and a release optimized compiler). More importantly, since the affected code path happens during semantic analysis, it also cuts that time but approximately 1 second, which should benefit development tools such as crystalline.
Some numbers from non-rigorous measurement, using a release build of the compiler building the compiler itself:
GC_DONT_GC=1 make -B): from 15.0s (baseline) to 13.9s (with this patch)GC_DONT_GC=1 make -B FLAGS="--no-codegen"): from 7.3s (baseline) to 6s (with this patch)Btw, I think a better solution would be to refactor the code so that we don't use exceptions for flow control. But that's a lot more work.