Subject: bug#24160: [PATCH 1/2] sed: cache results of mbrtowc for speed

From: Norihiro Tanaka <[email-masked]>
Date: Mon, 19 Sep 2016 11:32:20 +0900

On Fri, 05 Aug 2016 22:51:16 +0900
Norihiro Tanaka <[email-masked]> wrote:

> Hi,
> 
> We can speeds up sed by caching result of result mbrtowc() for single
> byte characters.  It is effective especially in non-UTF8 multibyte
> locales which is expensive calculatation.
> 
> $ yes $(printf %040d 0) | head -1000000 >k
> 
> Before:
> 
> $ time -p env LC_ALL=ja_JP.eucjp sed/sed -ne /a.b/p k
> real 1.93
> user 1.61
> sys 0.27
> 
> After patching
> 
> $ time -p env LC_ALL=ja_JP.eucjp sed/sed -ne /a.b/p k
> real 0.46
> user 0.42
> sys 0.03
> 
> Thanks,
> Norihiro

I rewrote the patch as using localeinfo in gnulib.
From c1a9d70936756887c7cdf55b5b32826df72b9d52 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Sun, 18 Sep 2016 17:46:57 +0900
Subject: [PATCH] sed: use cache provided by localeinfo for mbrtowc and mbrlen

* sed/sed.h (MBRTOWC, MBRLEN): Use cache provided by localeinfo.
(MBRTOWC, MBRLEN): Use the cache.
---
 sed/sed.h |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/sed/sed.h b/sed/sed.h
index 083baae..9ebc815 100644
--- a/sed/sed.h
+++ b/sed/sed.h
@@ -247,8 +247,8 @@ extern bool is_utf8;
 extern bool sandbox;
 
 #define MBRTOWC(pwc, s, n, ps) \
-  (mb_cur_max == 1 ? \
-   (*(pwc) = btowc (*(unsigned char *) (s)), 1) : \
+  (localeinfo.sbclen[*(unsigned char *) (s)] == 1 ? \
+   (*(pwc) = localeinfo.sbctowc[*(unsigned char *) (s)], 1) : \
    mbrtowc ((pwc), (s), (n), (ps)))
 
 #define WCRTOMB(s, wc, ps) \
@@ -260,7 +260,8 @@ extern bool sandbox;
   (mb_cur_max == 1 ? 1 : mbsinit ((s)))
 
 #define MBRLEN(s, n, ps) \
-  (mb_cur_max == 1 ? 1 : mbrtowc (NULL, s, n, ps))
+  (localeinfo.sbclen[*(unsigned char *) (s)] == 1 ? \
+   1 : mbrtowc (NULL, s, n, ps))
 
 #define IS_MB_CHAR(ch, ps)                \
   (mb_cur_max == 1 ? 0 : is_mb_char (ch, ps))
-- 
1.7.1