From aed52151043561dbe9657abd07f1abfcd97df817 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Thu, 24 Aug 2023 17:53:23 +0930 Subject: [PATCH] Optimize handle_interrupt(Exception => ..) as a common case When interrupt behavior is configured for all possible exceptions using 'Exception', there's no need to iterate the pending exception's ancestors for hash lookups. More significantly, by storing the catch-all timing symbol directly in the mask stack, we can skip allocating the hash we would otherwise need. --- thread.c | 77 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 22 deletions(-) diff --git a/thread.c b/thread.c index 80a097f6f4..7654fc03a4 100644 --- a/thread.c +++ b/thread.c @@ -1891,6 +1891,23 @@ enum handle_interrupt_timing { INTERRUPT_NEVER }; +static enum handle_interrupt_timing +rb_threadptr_pending_interrupt_from_symbol(rb_thread_t *th, VALUE sym) +{ + if (sym == sym_immediate) { + return INTERRUPT_IMMEDIATE; + } + else if (sym == sym_on_blocking) { + return INTERRUPT_ON_BLOCKING; + } + else if (sym == sym_never) { + return INTERRUPT_NEVER; + } + else { + rb_raise(rb_eThreadError, "unknown mask signature"); + } +} + static enum handle_interrupt_timing rb_threadptr_pending_interrupt_check_mask(rb_thread_t *th, VALUE err) { @@ -1903,6 +1920,16 @@ rb_threadptr_pending_interrupt_check_mask(rb_thread_t *th, VALUE err) for (i=0; i