Skip to content

Commit 39b0ad1

Browse files
committed
[perl #134172] Avoid multiple checks of IN_LC(LC_NUMERIC)
This adds new API macros STORE_LC_NUMERIC_SET_TO_NEEDED_IN and WITH_LC_NUMERIC_SET_TO_NEEDED_IN that accept a precalculated value for the hints checks of IN_LC(LC_NUMERIC).
1 parent a06a4d4 commit 39b0ad1

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

perl.h

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6411,7 +6411,15 @@ argument list, like this:
64116411
64126412
On threaded perls not operating with thread-safe functionality, this macro uses
64136413
a mutex to force a critical section. Therefore the matching RESTORE should be
6414-
close by, and guaranteed to be called.
6414+
close by, and guaranteed to be called; see L</WITH_LC_NUMERIC_SET_TO_NEEDED>
6415+
for a more contained way to ensure that.
6416+
6417+
=for apidoc Am|void|STORE_LC_NUMERIC_SET_TO_NEEDED_IN|bool in_lc_numeric
6418+
6419+
Same as L</STORE_LC_NUMERIC_SET_TO_NEEDED_IN> with in_lc_numeric provided
6420+
as the precalculated value of C<IN_LC(LC_NUMERIC)>. It is the caller's
6421+
responsibility to ensure that the status of C<PL_compiling> and C<PL_hints>
6422+
cannot have changed since the precalculation.
64156423
64166424
=for apidoc Am|void|RESTORE_LC_NUMERIC
64176425
@@ -6455,6 +6463,13 @@ is equivalent to:
64556463
#endif
64566464
}
64576465
6466+
=for apidoc Am|void|WITH_LC_NUMERIC_SET_TO_NEEDED_IN|bool in_lc_numeric
6467+
6468+
Same as L</WITH_LC_NUMERIC_SET_TO_NEEDED> with in_lc_numeric provided
6469+
as the precalculated value of C<IN_LC(LC_NUMERIC)>. It is the caller's
6470+
responsibility to ensure that the status of C<PL_compiling> and C<PL_hints>
6471+
cannot have changed since the precalculation.
6472+
64586473
=cut
64596474
64606475
*/
@@ -6482,12 +6497,13 @@ is equivalent to:
64826497
# define DECLARATION_FOR_LC_NUMERIC_MANIPULATION \
64836498
void (*_restore_LC_NUMERIC_function)(pTHX) = NULL
64846499

6485-
# define STORE_LC_NUMERIC_SET_TO_NEEDED() \
6500+
# define STORE_LC_NUMERIC_SET_TO_NEEDED_IN(in) \
64866501
STMT_START { \
6502+
bool _in_lc_numeric = (in); \
64876503
LC_NUMERIC_LOCK( \
6488-
( ( IN_LC(LC_NUMERIC) && _NOT_IN_NUMERIC_UNDERLYING) \
6489-
|| (! IN_LC(LC_NUMERIC) && _NOT_IN_NUMERIC_STANDARD)));\
6490-
if (IN_LC(LC_NUMERIC)) { \
6504+
( ( _in_lc_numeric && _NOT_IN_NUMERIC_UNDERLYING) \
6505+
|| (! _in_lc_numeric && _NOT_IN_NUMERIC_STANDARD))); \
6506+
if (_in_lc_numeric) { \
64916507
if (_NOT_IN_NUMERIC_UNDERLYING) { \
64926508
Perl_set_numeric_underlying(aTHX); \
64936509
_restore_LC_NUMERIC_function \
@@ -6503,6 +6519,9 @@ is equivalent to:
65036519
} \
65046520
} STMT_END
65056521

6522+
# define STORE_LC_NUMERIC_SET_TO_NEEDED() \
6523+
STORE_LC_NUMERIC_SET_TO_NEEDED_IN(IN_LC(LC_NUMERIC))
6524+
65066525
# define RESTORE_LC_NUMERIC() \
65076526
STMT_START { \
65086527
if (_restore_LC_NUMERIC_function) { \
@@ -6577,14 +6596,17 @@ is equivalent to:
65776596
__FILE__, __LINE__, PL_numeric_standard)); \
65786597
} STMT_END
65796598

6580-
# define WITH_LC_NUMERIC_SET_TO_NEEDED(block) \
6599+
# define WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric, block) \
65816600
STMT_START { \
65826601
DECLARATION_FOR_LC_NUMERIC_MANIPULATION; \
6583-
STORE_LC_NUMERIC_SET_TO_NEEDED(); \
6602+
STORE_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric); \
65846603
block; \
65856604
RESTORE_LC_NUMERIC(); \
65866605
} STMT_END;
65876606

6607+
# define WITH_LC_NUMERIC_SET_TO_NEEDED(block) \
6608+
WITH_LC_NUMERIC_SET_TO_NEEDED_IN(IN_LC(LC_NUMERIC), block)
6609+
65886610
#else /* !USE_LOCALE_NUMERIC */
65896611

65906612
# define SET_NUMERIC_STANDARD()
@@ -6593,10 +6615,13 @@ is equivalent to:
65936615
# define DECLARATION_FOR_LC_NUMERIC_MANIPULATION
65946616
# define STORE_LC_NUMERIC_SET_STANDARD()
65956617
# define STORE_LC_NUMERIC_FORCE_TO_UNDERLYING()
6618+
# define STORE_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric)
65966619
# define STORE_LC_NUMERIC_SET_TO_NEEDED()
65976620
# define RESTORE_LC_NUMERIC()
65986621
# define LOCK_LC_NUMERIC_STANDARD()
65996622
# define UNLOCK_LC_NUMERIC_STANDARD()
6623+
# define WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric, block) \
6624+
STMT_START { block; } STMT_END
66006625
# define WITH_LC_NUMERIC_SET_TO_NEEDED(block) \
66016626
STMT_START { block; } STMT_END
66026627

sv.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11784,7 +11784,7 @@ S_format_hexfp(pTHX_ char * const buf, const STRLEN bufsize, const char c,
1178411784
#else
1178511785
if (in_lc_numeric) {
1178611786
STRLEN n;
11787-
WITH_LC_NUMERIC_SET_TO_NEEDED({
11787+
WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
1178811788
const char* r = SvPV(PL_numeric_radix_sv, n);
1178911789
Copy(r, p, n, char);
1179011790
});
@@ -12978,7 +12978,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
1297812978
}
1297912979

1298012980
if (in_lc_numeric) {
12981-
WITH_LC_NUMERIC_SET_TO_NEEDED({
12981+
WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
1298212982
/* this can't wrap unless PL_numeric_radix_sv is a string
1298312983
* consuming virtually all the 32-bit or 64-bit address
1298412984
* space
@@ -13071,7 +13071,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
1307113071
&& !fill
1307213072
&& intsize != 'q'
1307313073
) {
13074-
WITH_LC_NUMERIC_SET_TO_NEEDED(
13074+
WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
1307513075
SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
1307613076
);
1307713077
elen = strlen(ebuf);
@@ -13174,7 +13174,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
1317413174
const char* qfmt = quadmath_format_single(ptr);
1317513175
if (!qfmt)
1317613176
Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", ptr);
13177-
WITH_LC_NUMERIC_SET_TO_NEEDED(
13177+
WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
1317813178
elen = quadmath_snprintf(PL_efloatbuf, PL_efloatsize,
1317913179
qfmt, nv);
1318013180
);
@@ -13187,13 +13187,13 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
1318713187
Safefree(qfmt);
1318813188
}
1318913189
#elif defined(HAS_LONG_DOUBLE)
13190-
WITH_LC_NUMERIC_SET_TO_NEEDED(
13190+
WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
1319113191
elen = ((intsize == 'q')
1319213192
? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
1319313193
: my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)fv))
1319413194
);
1319513195
#else
13196-
WITH_LC_NUMERIC_SET_TO_NEEDED(
13196+
WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
1319713197
elen = my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
1319813198
);
1319913199
#endif

0 commit comments

Comments
 (0)