From 187f2cba4ec51486fdd5d034cd7eec8cabc8fe5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Gir=C3=A1ldez?= Date: Sat, 14 Sep 2024 13:05:25 -0400 Subject: [PATCH] Avoid unwinding the stack on hot path in method call lookups 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. --- src/compiler/crystal/semantic/call.cr | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/compiler/crystal/semantic/call.cr b/src/compiler/crystal/semantic/call.cr index f581ea79d577..e265829a919e 100644 --- a/src/compiler/crystal/semantic/call.cr +++ b/src/compiler/crystal/semantic/call.cr @@ -13,6 +13,11 @@ class Crystal::Call property? uses_with_scope = false class RetryLookupWithLiterals < ::Exception + @@dummy_call_stack = Exception::CallStack.new + + def initialize + self.callstack = @@dummy_call_stack + end end def program