changeset 39701:4642c3a9e204

wchar-single: a new module to enable optimizations in wchar replacements * lib/mbrtowc.c (mbrtowc): Only check locale_charset() once if GNULIB_WCHAR_SINGLE is enabled. * lib/wcwidth.c (wcwidth): Likewise.
author Pádraig Brady <P@draigBrady.com>
date Sun, 20 May 2018 22:11:12 -0700
parents 997df1305be8
children c4c7af0652e0
files ChangeLog lib/mbrtowc.c lib/wcwidth.c modules/wchar modules/wchar-single
diffstat 5 files changed, 54 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sat Jun 23 14:43:56 2018 +0200
+++ b/ChangeLog	Sun May 20 22:11:12 2018 -0700
@@ -1,3 +1,10 @@
+2018-06-23  Pádraig Brady  <P@draigBrady.com>
+
+	wchar-single: a new module to enable optimizations in wchar replacements
+	* lib/mbrtowc.c (mbrtowc): Only check locale_charset() once if
+	GNULIB_WCHAR_SINGLE is enabled.
+	* lib/wcwidth.c (wcwidth): Likewise.
+
 2018-06-23  Bruno Haible  <bruno@clisp.org>
 
 	libc-config: Fix conflict with FreeBSD include files.
--- a/lib/mbrtowc.c	Sat Jun 23 14:43:56 2018 +0200
+++ b/lib/mbrtowc.c	Sun May 20 22:11:12 2018 -0700
@@ -138,9 +138,19 @@
         goto invalid;
       /* Here MB_CUR_MAX > 1 and 0 < m < 4.  */
       {
-        const char *encoding = locale_charset ();
+        static int utf8_charset = -1;
+        static const char *encoding;
 
-        if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0, 0))
+# if GNULIB_WCHAR_SINGLE
+        if (utf8_charset == -1)
+# endif
+          {
+            encoding = locale_charset ();
+            utf8_charset = STREQ_OPT (encoding,
+                                      "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0 ,0);
+          }
+
+        if (utf8_charset)
           {
             /* Cf. unistr/u8-mblen.c.  */
             unsigned char c = (unsigned char) p[0];
--- a/lib/wcwidth.c	Sat Jun 23 14:43:56 2018 +0200
+++ b/lib/wcwidth.c	Sun May 20 22:11:12 2018 -0700
@@ -30,9 +30,20 @@
 wcwidth (wchar_t wc)
 #undef wcwidth
 {
+  static int utf8_charset = -1;
+  static const char *encoding;
+
+#if GNULIB_WCHAR_SINGLE
+  if (utf8_charset == -1)
+#endif
+    {
+      encoding = locale_charset ();
+      utf8_charset = STREQ_OPT (encoding,
+                                "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0 ,0);
+    }
+
   /* In UTF-8 locales, use a Unicode aware width function.  */
-  const char *encoding = locale_charset ();
-  if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0 ,0))
+  if (utf8_charset)
     {
       /* We assume that in a UTF-8 locale, a wide character is the same as a
          Unicode character.  */
--- a/modules/wchar	Sat Jun 23 14:43:56 2018 +0200
+++ b/modules/wchar	Sun May 20 22:11:12 2018 -0700
@@ -1,5 +1,6 @@
 Description:
 A <wchar.h> that works around platform issues.
+Note also the wchar-single module.
 
 Files:
 lib/wchar.in.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/wchar-single	Sun May 20 22:11:12 2018 -0700
@@ -0,0 +1,21 @@
+Description:
+Enable more efficient wchar replacements, where we know
+the locale charset will not change between calls.
+
+Files:
+
+Depends-on:
+wchar
+
+configure.ac:
+gl_MODULE_INDICATOR([wchar-single])
+
+Makefile.am:
+
+Include:
+
+License:
+LGPLv2+
+
+Maintainer:
+all