This is an automated email from the ASF dual-hosted git repository.

nickva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb-jiffy.git

commit 0b795ab40ab123a3cd19ba53a09cdc567156f1f7
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Tue Apr 14 13:52:48 2026 -0400

    Implement SIMD vectorization
    
    Structure scan-ahead logic so Clang and GCC will auto-vectorize the code.
    For the auto-vectorizer we want a loop with a known bound and no early exit,
    which is a bit tricky, since the whole idea is to exit when we find a "stop"
    character. To get the best of both worlds we scan 32 bytes at a time, and 
then
    have an outer loop around that.
    
    The title and the .h file name is also a subtle joke, as we didn't actually
    write a single line of SIMD assembly. However, we've shown vectorization 
takes
    place (left some command lines in the file as proof) and we still keep the 
code
    fairly readable.
    
    To get an even better speed boost someone who knows they'll deploy on 
certain
    CPUs could choose to compile with -march=native or -march=core-avx2 or
    something like that. In that case the auto-vectorizing code may do an even
    better job and use wider AVX2 registers. Most Intel and AMD CPUs since 2013 
or
    so would support that.
---
 c_src/decoder.c    | 16 ++++----------
 c_src/encoder.c    | 17 +++------------
 c_src/jiffy_simd.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 26 deletions(-)

diff --git a/c_src/decoder.c b/c_src/decoder.c
index ae2d266..7ebaeb9 100644
--- a/c_src/decoder.c
+++ b/c_src/decoder.c
@@ -8,6 +8,7 @@
 #include "erl_nif.h"
 #include "ffc.h"
 #include "jiffy.h"
+#include "jiffy_simd.h"
 #include "jiffy_utf8.h"
 
 #define STACK_SIZE_INC 64
@@ -269,18 +270,9 @@ dec_string(Decoder* d, ERL_NIF_TERM* value)
                     return 0;
             }
         } else if(d->p[d->i] < 0x80) {
-            // Scan ahead plain ASCII as an optimization
-            const unsigned char* JIFFY_RESTRICT p = d->p;
-            size_t idx = d->i + 1;
-            const size_t len = d->len;
-            while(idx < len
-                    && p[idx] >= 0x20
-                    && p[idx] < 0x80
-                    && p[idx] != '\"'
-                    && p[idx] != '\\') {
-                idx++;
-            }
-            d->i = idx;
+            // Scan ahead plain ASCII as an optimization. The first
+            // byte has already been checked, so start at i+1.
+            d->i = jiffy_scan_string_body(d->p, d->len, d->i + 1);
         } else {
             ulen = utf8_validate(&(d->p[d->i]), d->len - d->i);
             if(ulen == 0) {
diff --git a/c_src/encoder.c b/c_src/encoder.c
index a2a0108..196ecf8 100644
--- a/c_src/encoder.c
+++ b/c_src/encoder.c
@@ -5,6 +5,7 @@
 #include <string.h>
 
 #include "jiffy.h"
+#include "jiffy_simd.h"
 #include "jiffy_utf8.h"
 #include "ryu/ryu.h"
 
@@ -408,13 +409,7 @@ enc_atom(Encoder* e, ERL_NIF_TERM val)
                     i++;
                 }
             } else {
-                while(i < size
-                        && data[i] >= 0x20
-                        && data[i] < 0x80
-                        && data[i] != '\"'
-                        && data[i] != '\\') {
-                    i++;
-                }
+                i = jiffy_scan_string_body(data, size, i);
             }
             size_t run = i - start;
             if(!enc_ensure(e, run)) {
@@ -497,13 +492,7 @@ enc_string(Encoder* e, ERL_NIF_TERM val)
                     i++;
                 }
             } else {
-                while(i < size
-                        && data[i] >= 0x20
-                        && data[i] < 0x80
-                        && data[i] != '\"'
-                        && data[i] != '\\') {
-                    i++;
-                }
+                i = jiffy_scan_string_body(data, size, i);
             }
             size_t run = i - start;
             if(!enc_ensure(e, run)) {
diff --git a/c_src/jiffy_simd.h b/c_src/jiffy_simd.h
new file mode 100644
index 0000000..44ad5d8
--- /dev/null
+++ b/c_src/jiffy_simd.h
@@ -0,0 +1,64 @@
+// This file is part of Jiffy released under the MIT license.
+// See the LICENSE file for more information.
+//
+// Structure the scan-ahead logic so it's both readable and so that Clang and
+// GCC will auto-vectorize the code. For the auto-vectorizer we want to have a
+// loop with a known bound and no early exit, which is a bit tricky, since the
+// whole idea is to exit when we find a "stop" character. To get the best of
+// both worlds, scan 32 bytes at a time and then have an outer loop around that
+//
+// We get auto-vectorization by default with Clang (MacOS 15+) and GCC (14+)
+//
+// Clang
+// ===
+// % cc -S -O3 -Rpass='.*vectoriz.*' -I "$ERL_INC" -I c_src  -c c_src/jiffy.c  
-o jiffy.s 2>&1 | grep -i -w 'vectorized' | grep simd
+// c_src/jiffy_simd.h:22:61: remark: Vectorized horizontal reduction with cost 
-112 and with tree size 3 [-Rpass=slp-vectorizer]
+// c_src/jiffy_simd.h:22:61: remark: Vectorized horizontal reduction with cost 
-87 and with tree size 3 [-Rpass=slp-vectorizer]
+//
+// GCC
+// ====
+// % gcc-14 -S -O3 -fopt-info-vec-all -I "$ERL_INC" -I c_src  -c c_src/jiffy.c 
-o jiffy.s 2>&1 | grep -i -w 'optimized'| grep simd
+// c_src/jiffy_simd.h:21:23: optimized: loop vectorized using 16 byte vectors
+// c_src/jiffy_simd.h:21:23: optimized: loop vectorized using 16 byte vectors
+// c_src/jiffy_simd.h:21:23: optimized: loop vectorized using 16 byte vectors
+//
+// If we know we'll deploy on the same architecture we're compiling on we can
+// use -march=native and get even better vectorization with 32 byte chunks at a
+// time
+//
+// GCC /w -march=native
+// ====
+// % gcc-14 -march=native -S -O3 -fopt-info-vec-all -I "$ERL_INC" -I c_src  -c 
c_src/jiffy.c -o jiffy.s 2>&1 | grep -i -w 'optimized'| grep simd
+// c_src/jiffy_simd.h:21:23: optimized: loop vectorized using 32 byte vectors
+// c_src/jiffy_simd.h:21:23: optimized: loop vectorized using 32 byte vectors
+// c_src/jiffy_simd.h:21:23: optimized: loop vectorized using 32 byte vectors
+
+#ifndef JIFFY_SIMD_H
+#define JIFFY_SIMD_H
+
+#define JIFFY_SIMD_BLOCK_SIZE 32
+
+static inline unsigned int
+jiffy_block_has_stop(const unsigned char* JIFFY_RESTRICT p)
+{
+    unsigned int bad = 0;
+    for (int i = 0; i < JIFFY_SIMD_BLOCK_SIZE; i++) {
+        unsigned char c = p[i];
+        bad |= (unsigned)(c < 0x20) | (unsigned)(c >= 0x80) | (unsigned)(c == 
'"') | (unsigned)(c == '\\');
+    }
+    return bad;
+}
+
+static inline size_t
+jiffy_scan_string_body(const unsigned char* JIFFY_RESTRICT p, size_t len, 
size_t i)
+{
+    while (i + JIFFY_SIMD_BLOCK_SIZE <= len && !jiffy_block_has_stop(p + i)) {
+        i += JIFFY_SIMD_BLOCK_SIZE;
+    }
+    while (i < len && p[i] >= 0x20 && p[i] < 0x80 && p[i] != '"' && p[i] != 
'\\') {
+        i++;
+    }
+    return i;
+}
+
+#endif

Reply via email to