Avoid unwinding the stack on hot path in method call lookups #15002
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 specialRetryLookupWithLiterals
exception 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.