I wrote:
> The benchmarks clearly show the speedup, especially for the case of ASCII
> text (CPU time, measured on x86_64, with gcc 13, on an AMD Ryzen 7 CPU):
> 
>     mbiter  mbiterf
> a    0.926   0.230
> b    0.765   0.233
> c    1.706   1.125
> d    1.324   0.847
> e    1.580   0.889
> f   14.134  13.095
> g    8.409   8.244
> h    8.882   8.553
> i    4.110   4.000
> j    4.430   4.212

Similarly with clang 16 (on the same machine):

         gcc              clang
    mbiter  mbiterf  mbiter  mbiterf
a    0.926   0.230    1.425   0.227
b    0.765   0.233    1.442   0.227
c    1.706   1.125    2.761   1.084
d    1.324   0.847    2.565   0.833
e    1.580   0.889    2.582   0.878
f   14.134  13.095   13.278  12.431
g    8.409   8.244    9.343   7.898
h    8.882   8.553    9.766   8.341
i    4.110   4.000    4.316   4.025
j    4.430   4.212    4.501   4.254

Here the speedup in the ASCII case is even 6-fold,
because clang apparently does not optimize the state-based code as well as gcc,
but optimizes the struct-as-return-value code as well as (or slightly better
than) gcc.


2023-07-18  Bruno Haible  <[email protected]>

        mbiterf: Fix compiler warnings.
        * lib/mbiterf.h (mbiterf_next): Use C99 designated initializer syntax.
        * tests/bench-mbiterf.c (do_test): Use a 'const char *' pointer.

diff --git a/lib/mbiterf.h b/lib/mbiterf.h
index 0059c97fbd..f5a817dbed 100644
--- a/lib/mbiterf.h
+++ b/lib/mbiterf.h
@@ -120,7 +120,7 @@ mbiterf_next (struct mbif_state *ps, const char *iter, 
const char *endptr)
          their char code.  For the few other ones, this is the case as well,
          in all locale encodings that are in use.  The 32-bit wide character
          code is the same as well.  */
-      return (mbchar_t) { ptr: iter, bytes: 1, wc_valid: true, wc: *iter };
+      return (mbchar_t) { .ptr = iter, .bytes = 1, .wc_valid = true, .wc = 
*iter };
     }
   else
     {
@@ -140,7 +140,7 @@ mbiterf_next (struct mbif_state *ps, const char *iter, 
const char *endptr)
           ps->in_shift = false;
           #endif
           mbszero (&ps->state);
-          return (mbchar_t) { ptr: iter, bytes: 1, wc_valid: false };
+          return (mbchar_t) { .ptr = iter, .bytes = 1, .wc_valid = false };
         }
       else if (bytes == (size_t) -2)
         {
@@ -150,7 +150,7 @@ mbiterf_next (struct mbif_state *ps, const char *iter, 
const char *endptr)
           #endif
           /* Whether to reset ps->state or not is not important; the string end
              is reached anyway.  */
-          return (mbchar_t) { ptr: iter, bytes: endptr - iter, wc_valid: false 
};
+          return (mbchar_t) { .ptr = iter, .bytes = endptr - iter, .wc_valid = 
false };
         }
       else
         {
@@ -174,7 +174,7 @@ mbiterf_next (struct mbif_state *ps, const char *iter, 
const char *endptr)
           if (mbsinit (&ps->state))
             ps->in_shift = false;
           #endif
-          return (mbchar_t) { ptr: iter, bytes: bytes, wc_valid: true, wc: wc 
};
+          return (mbchar_t) { .ptr = iter, .bytes = bytes, .wc_valid = true, 
.wc = wc };
         }
     }
 }
diff --git a/tests/bench-mbiterf.c b/tests/bench-mbiterf.c
index 0949fb6752..bc3fbe4828 100644
--- a/tests/bench-mbiterf.c
+++ b/tests/bench-mbiterf.c
@@ -43,7 +43,7 @@ do_test (char test, int repeat, const char *locale_name, 
const char *text)
           unsigned long long sum = 0;
           const char *text_end = text + text_len;
           mbif_state_t state;
-          char *iter;
+          const char *iter;
           for (mbif_init (state), iter = text; mbif_avail (state, iter, 
text_end); )
             {
               mbchar_t cur = mbif_next (state, iter, text_end);




Reply via email to