On Sun, Feb 1, 2015 at 10:07 AM, Daiki Ueno <u...@gnu.org> wrote: > Eli Zaretskii <e...@gnu.org> writes: > >> Perhaps we could call _configthreadlocale to see if the value of >> setlocale can be cached? > > Yes, but then we would need to hook setlocale to invalidate the cache, > and currently the setlocale module is not listed in the dependencies of > localcharset. I'm wondering if it is worth adding the dependency for > this particular performance problem. Perhaps it might be easier to add > a new API to localcharset for single-threaded / single-locale consumer. >
Here's a simple solution. The caller of the new function would call locale_encoding themselves and save the result. Maybe somebody will have a better idea. If this is too specialized to go into gnulib, would it be easy for a project to use its own replacement functions instead of the ones from gnulib? *** wcwidth.c 2014-04-22 02:56:36.000000000 +0100 --- wcwidth.c-NEW 2015-02-01 14:53:41.000000000 +0000 *************** *** 27,37 **** #include "uniwidth.h" int ! wcwidth (wchar_t wc) ! #undef wcwidth { /* 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)) { /* We assume that in a UTF-8 locale, a wide character is the same as a --- 27,35 ---- #include "uniwidth.h" int ! wcwidth_given_encoding (wchar_t wc, const char *encoding) { /* In UTF-8 locales, use a Unicode aware width function. */ if (STREQ_OPT (encoding, "UTF-8", 'U', 'T', 'F', '-', '8', 0, 0, 0 ,0)) { /* We assume that in a UTF-8 locale, a wide character is the same as a *************** *** 48,50 **** --- 46,56 ---- #endif } } + + int + wcwidth (wchar_t wc) + #undef wcwidth + { + const char *encoding = locale_charset (); + return wcwidth_given_encoding (wc, encoding); + }