Skip to content

Avoid unwinding the stack on hot path in method call lookups#15002

Merged
straight-shoota merged 1 commit intocrystal-lang:masterfrom
ggiraldez:avoid-unwind-call-lookup
Sep 16, 2024
Merged

Avoid unwinding the stack on hot path in method call lookups#15002
straight-shoota merged 1 commit intocrystal-lang:masterfrom
ggiraldez:avoid-unwind-call-lookup

Conversation

@ggiraldez
Copy link
Contributor

@ggiraldez ggiraldez commented Sep 14, 2024

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 special RetryLookupWithLiterals 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:

  • Full build (ie. GC_DONT_GC=1 make -B): from 15.0s (baseline) to 13.9s (with this patch)
  • Semantic analysis only (ie. 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.

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.
@straight-shoota straight-shoota added this to the 1.14.0 milestone Sep 15, 2024
@straight-shoota straight-shoota merged commit 5125f66 into crystal-lang:master Sep 16, 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)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants