From 089e3d32de67fc4c68fc48c9fba06e37e825c1ac Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Tue, 28 Mar 2023 19:12:01 -0600 Subject: [PATCH] locale.c: use variable to avoid extra comparisons The mnemonic name also makes it clearer. Instead of comparing two variables multiple times, set a boolean once with the result, and use that. --- locale.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/locale.c b/locale.c index 6cc9835e32b5..b10ec837a92d 100644 --- a/locale.c +++ b/locale.c @@ -1853,11 +1853,21 @@ S_bool_setlocale_2008_i(pTHX_ locale_t new_obj; - /* We created a (never changing) object at start-up for LC_ALL being in the - * C locale. If this call is to switch to LC_ALL=>C, simply use that - * object. But in fact, we already have switched to it just above, in - * preparation for the general case. Since we're already there, no need to - * do further switching. */ + /* These two objects are special: + * LC_GLOBAL_LOCALE because it is undefined behavior to call + * newlocale() with it as a parameter. + * PL_C_locale_obj because newlocale() generally destroys its locale + * object parameter when it succeeds; and we don't + * want that happening to this immutable object. + * Copies will be made for them to use instead if we get so far as to call + * newlocale(). */ + bool entry_obj_is_special = ( entry_obj == LC_GLOBAL_LOCALE + || entry_obj == PL_C_locale_obj); + + /* PL_C_locale_obj is LC_ALL set to the C locale. If this call is to + * switch to LC_ALL=>C, simply use that object. But in fact, we already + * have switched to it just above, in preparation for the general case. + * Since we're already there, no need to do further switching. */ if (mask == LC_ALL_MASK && isNAME_C_OR_POSIX(new_locale)) { DEBUG_Lv(PerlIO_printf(Perl_debug_log, "(%" LINE_Tf "):" " bool_setlocale_2008_i will stay" @@ -1865,21 +1875,15 @@ S_bool_setlocale_2008_i(pTHX_ new_obj = PL_C_locale_obj; /* And free the old object if it isn't a special one */ - if (entry_obj != LC_GLOBAL_LOCALE && entry_obj != PL_C_locale_obj) { + if (! entry_obj_is_special) { freelocale(entry_obj); } } else { /* Here is the general case, not to LC_ALL=>C */ locale_t basis_obj = entry_obj; - /* Specially handle two objects */ - if (basis_obj == LC_GLOBAL_LOCALE || basis_obj == PL_C_locale_obj) { + if (entry_obj_is_special) { - /* For these two objects, we make duplicates to hand to newlocale() - * below. For LC_GLOBAL_LOCALE, this is because newlocale() - * doesn't necessarily accept it as input (the results are - * undefined). For PL_C_locale_obj, it is so that it never gets - * modified, as otherwise newlocale() is free to do so */ basis_obj = duplocale(basis_obj); if (! basis_obj) { locale_panic_(Perl_form(aTHX_ "(%" LINE_Tf "): duplocale failed",