This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Avoid locale function overhead
authorKarl Williamson <khw@cpan.org>
Sun, 1 Jun 2014 19:42:31 +0000 (13:42 -0600)
committerKarl Williamson <khw@cpan.org>
Thu, 5 Jun 2014 17:23:00 +0000 (11:23 -0600)
For the LC_NUMERIC locale category, one can swap between setlocale being
to "C" or to the underlying locale.  Perl keeps track of the state
using two boolean variables.  By testing these before calling the swap
functions, we can skip calling them if the current state is the one we
want to be in, and avoid the function call overhead, as the function
becomes a no-op if the current state is the desired one.

perl.h

diff --git a/perl.h b/perl.h
index bd36ef8..140569e 100644 (file)
--- a/perl.h
+++ b/perl.h
@@ -5363,11 +5363,12 @@ typedef struct am_table_short AMTS;
 
 /* The next two macros set unconditionally.  These should be rarely used, and
  * only after being sure that this is what is needed */
-#define SET_NUMERIC_STANDARD() \
-       set_numeric_standard();
+#define SET_NUMERIC_STANDARD()                                              \
+       STMT_START { if (! PL_numeric_standard) set_numeric_standard();     \
+                                                                 } STMT_END
 
-#define SET_NUMERIC_LOCAL() \
-       set_numeric_local();
+#define SET_NUMERIC_LOCAL()                                                 \
+       STMT_START { if (! PL_numeric_local) set_numeric_local(); } STMT_END
 
 /* The rest of these LC_NUMERIC macros toggle to one or the other state, with
  * the RESTORE_foo ones called to switch back, but only if need be */