From 03f24b8a082948e5b437394fa33d0af08d7b80b6 Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Thu, 4 Sep 2025 09:04:58 -0600 Subject: [PATCH] Make croak() always expand to Perl_croak_nocontext() Perl almost always opts for saving time over saving space. Hence, we have croak() that saves time at the expense of space, but needs thread context available; and croak_no_context() that doesn't need that, but takes extra time But, when we are about to die, time isn't that important. Even if we are doing eval after eval in a tight loop, the potential time savings of passing the thread context to Perl_croak is insignificant compared to the tear-down that follows. My claim then is that croak() never needed a thread context parameter to save a bit of time just before death. It is an optimization that isn't worth it. And having it do so required the invention of croak_nocontext(), and the extra cognitive load associated with two methods for the same task. This commit changes plain croak() to not use a thread context parameter. It and croak_nocontext() now behave identically. That means that going forward, people will likely choose croak() which requires less typing and occupies fewer columns on the screen, and they won't have to remember which form to use when. --- autodoc.pl | 1 - embed.fnc | 2 +- embed.h | 3 --- regen/embed.pl | 1 - util.c | 7 +++---- util.h | 9 +++++++++ 6 files changed, 13 insertions(+), 10 deletions(-) diff --git a/autodoc.pl b/autodoc.pl index e36df49b68..0853d2aa53 100644 --- a/autodoc.pl +++ b/autodoc.pl @@ -86,7 +86,6 @@ my $item_flags_re = qr/[dD fF mM nN oO pT uU Wx;]/xx; # This is a copy of the list in regen/embed.pl. my @have_compatibility_macros = qw( - croak deb die form diff --git a/embed.fnc b/embed.fnc index f1a1094103..32cf046308 100644 --- a/embed.fnc +++ b/embed.fnc @@ -922,7 +922,7 @@ px |void |create_eval_scope \ |NN SV **sp \ |U32 flags : croak()'s first parm can be NULL. Otherwise, mod_perl breaks. -Adfpr |void |croak |NULLOK const char *pat \ +AMdfpr |void |croak |NULLOK const char *pat \ |... Tfpr |void |croak_caller |NULLOK const char *pat \ |... diff --git a/embed.h b/embed.h index bef2e7b7fa..72aef7aa81 100644 --- a/embed.h +++ b/embed.h @@ -29,7 +29,6 @@ #if !defined(MULTIPLICITY) /* undefined symbols, point them back at the usual ones */ -# define Perl_croak_nocontext Perl_croak # define Perl_deb_nocontext Perl_deb # define Perl_die_nocontext Perl_die # define Perl_form_nocontext Perl_form @@ -78,7 +77,6 @@ # if defined(MULTIPLICITY) && !defined(PERL_NO_SHORT_NAMES) && \ !defined(PERL_WANT_VARARGS) -# define croak Perl_croak_nocontext # define deb Perl_deb_nocontext # define die Perl_die_nocontext # define form Perl_form_nocontext @@ -914,7 +912,6 @@ # endif /* defined(MULTIPLICITY) */ # if !defined(MULTIPLICITY) || defined(PERL_CORE) || \ defined(PERL_WANT_VARARGS) -# define croak(...) Perl_croak(aTHX_ __VA_ARGS__) # define deb(...) Perl_deb(aTHX_ __VA_ARGS__) # define die(...) Perl_die(aTHX_ __VA_ARGS__) # define form(...) Perl_form(aTHX_ __VA_ARGS__) diff --git a/regen/embed.pl b/regen/embed.pl index 706a181609..5fbce07114 100755 --- a/regen/embed.pl +++ b/regen/embed.pl @@ -41,7 +41,6 @@ BEGIN { # N.B. If you change this list, update the copy in autodoc.pl. This is likely # to never happen, so not worth coding automatic synchronization. my @have_compatibility_macros = qw( - croak deb die form diff --git a/util.c b/util.c index 6712433f35..982a318011 100644 --- a/util.c +++ b/util.c @@ -1912,10 +1912,9 @@ error message from arguments. If you want to throw a non-string object, or build an error message in an SV yourself, it is preferable to use the C> function, which does not involve clobbering C. -The two forms differ only in that C does not take a thread -context (C) parameter. It is usually preferred as it takes up fewer -bytes of code than plain C, and time is rarely a critical resource -when you are about to throw an exception. +The reasons for the existence of C are no longer applicable. +croak() can now be used in all circumstances. C might be +useful when compiling with C. =cut */ diff --git a/util.h b/util.h index 4be411fbe3..c935d967e8 100644 --- a/util.h +++ b/util.h @@ -11,6 +11,15 @@ #ifndef PERL_UTIL_H_ #define PERL_UTIL_H_ +/* Calling Perl_croak_nocontext instead of plain Perl_croak is one less + * argument to pass under threads, so each instance takes up fewer bytes (but + * the nocontext function has to derive the thread context itself, taking more + * time). We trade time for less space here, because time is rarely a + * critical resource when you are about to throw an exception. */ +#define croak(...) Perl_croak_nocontext(__VA_ARGS__) +#ifndef MULTIPLICITY +# define Perl_croak_nocontext Perl_croak +#endif #ifdef VMS # define PERL_FILE_IS_ABSOLUTE(f) \