You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "Felix-Gong (via GitHub)" <gi...@apache.org> on 2026/07/16 03:07:55 UTC

[PR] optimize StringPiece memcmp/memchr for RISC-V with RVV (brpc)

Felix-Gong opened a new pull request, #3390:
URL: https://github.com/apache/brpc/pull/3390

   ## Summary
   
   This patch adds RVV-accelerated `memcmp` and `memchr` implementations for `StringPiece` operations on RISC-V 64-bit platforms.
   
   The implementation follows glibc's official RVV pattern:
   - **e8m8 LMUL** for maximum throughput
   - **Hardware-adaptive vector length** via `vsetvl`
   - **`vfirst.m`** for early-out on first difference/match
   
   ## Performance
   
   Test environment: SOPHGO SG2044 (RVV 1.0, VLEN >= 128, GCC 15.1)
   Baseline: glibc 2.38 scalar memcmp/memchr (no RVV acceleration)
   
   ### memcmp (worst-case full scan, 50000 iterations)
   
   | Size | glibc (ns) | RVV (ns) | Speedup |
   |------|-----------|---------|---------|
   | 64 B | 25 | 5 | **5.00x** |
   | 256 B | 52 | 15 | **3.47x** |
   | 1 KB | 120 | 55 | **2.18x** |
   | 4 KB | 419 | 224 | **1.87x** |
   | 16 KB | 1,640 | 864 | **1.90x** |
   | 64 KB | 10,937 | 9,508 | **1.15x** |
   | 256 KB | 54,005 | 45,537 | **1.19x** |
   | 1 MB | 324,601 | 413,379 | 0.79x |
   
   ### memchr (worst-case full scan, 50000 iterations)
   
   | Size | glibc (ns) | RVV (ns) | Speedup |
   |------|-----------|---------|---------|
   | 64 B | 28 | 3 | **9.33x** |
   | 256 B | 60 | 9 | **6.67x** |
   | 1 KB | 184 | 36 | **5.11x** |
   | 4 KB | 637 | 154 | **4.14x** |
   | 16 KB | 2,568 | 594 | **4.32x** |
   | 64 KB | 10,664 | 3,614 | **2.95x** |
   | 256 KB | 41,660 | 17,262 | **2.41x** |
   | 1 MB | 163,354 | 55,354 | **2.95x** |
   
   ## Correctness
   
   | Test | Result |
   |------|--------|
   | Expanded correctness (24,950 cases, 64B–1MB) | **24,950/24,950 PASSED** |
   | `string_piece_unittest` | **22/22 PASSED** |
   | `test_butil` | **724/725 PASSED** (1 pre-existing StackTrace failure) |
   | `test_bvar` | **64/64 PASSED** |
   
   ## Files Changed
   
   - `src/butil/string_compare_rvv.cc` (new): RVV memcmp/memchr implementations
   - `src/butil/strings/string_piece.h`: RVV dispatch in `wordmemcmp` (N >= 16)
   - `BUILD.bazel`: added `string_compare_rvv.cc` to `BUTIL_SRCS`
   - `CMakeLists.txt`: added `string_compare_rvv.cc`
   
   ## Build
   
   ```bash
   cmake .. -DCMAKE_CXX_COMPILER=/opt/gcc-15.1/bin/riscv64-unknown-linux-gnu-g++ \
     -DCMAKE_CXX_FLAGS='-O2 -march=rv64gcv'
   make -j$(nproc)
   ```
   
   RISC-V optimization for butil StringPiece operations.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on PR #3390:
URL: https://github.com/apache/brpc/pull/3390#issuecomment-5029159556

   LGTM


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "Felix-Gong (via GitHub)" <gi...@apache.org>.
Felix-Gong commented on PR #3390:
URL: https://github.com/apache/brpc/pull/3390#issuecomment-5031144755

   @chenBright Fixed. Added src/butil/string_compare_rvv.cc to BUTIL_SOURCES in the Makefile, matching the existing entries in CMakeLists.txt and BUILD.bazel.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "Felix-Gong (via GitHub)" <gi...@apache.org>.
Felix-Gong commented on code in PR #3390:
URL: https://github.com/apache/brpc/pull/3390#discussion_r3600792260


##########
src/butil/string_compare_rvv.cc:
##########
@@ -0,0 +1,105 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+/**
+ * \file
+ * RISC-V Vector (RVV) accelerated memcmp/memchr for StringPiece operations.
+ *
+ * Algorithm follows glibc's RVV memcmp/memchr patterns:
+ * - Use vsetvli with e8/m8 for maximum throughput (hardware-adaptive VL)
+ * - Use vfirst.m to find first differing/matching byte (no vcpop needed)
+ * - Simple loop: load → compare → find first → branch
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * License); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "butil/strings/string_piece.h"
+
+#if defined(__riscv) && defined(__riscv_vector)
+#include <riscv_vector.h>
+#include <stdint.h>
+#include <string.h>
+
+namespace butil {
+
+/**
+ * rvv_memcmp - RVV-accelerated byte comparison following glibc pattern.
+ *
+ * Compares two byte arrays and returns:
+ *   < 0 if p1 < p2
+ *     0 if p1 == p2
+ *   > 0 if p1 > p2
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.
+ */
+int rvv_memcmp(const void* p1, const void* p2, size_t n) {
+    const uint8_t* src1 = static_cast<const uint8_t*>(p1);
+    const uint8_t* src2 = static_cast<const uint8_t*>(p2);
+    size_t remaining = n;
+
+    while (remaining > 0) {
+        size_t vl = __riscv_vsetvl_e8m8(remaining);
+        vuint8m8_t v1 = __riscv_vle8_v_u8m8(src1, vl);
+        vuint8m8_t v2 = __riscv_vle8_v_u8m8(src2, vl);
+        vbool1_t neq = __riscv_vmsne_vv_u8m8_b1(v1, v2, vl);
+        long first = __riscv_vfirst_m_b1(neq, vl);
+
+        if (first >= 0) {
+            uint8_t b1 = src1[first];
+            uint8_t b2 = src2[first];
+            return (b1 < b2) ? -1 : 1;
+        }

Review Comment:
   Fixed. Changed to return static_cast<int>(src1[first]) - static_cast<int>(src2[first]); which matches glibc memcmp semantics and preserves the byte-difference contract expected by traits_type::compare.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp/memchr for RISC-V with RVV (brpc)

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3390:
URL: https://github.com/apache/brpc/pull/3390#discussion_r3592386748


##########
BUILD.bazel:
##########
@@ -128,6 +128,7 @@ BUTIL_SRCS = [
     "src/butil/third_party/icu/icu_utf.cc",
     "src/butil/third_party/superfasthash/superfasthash.c",
     "src/butil/third_party/modp_b64/modp_b64.cc",
+    "src/butil/third_party/modp_b64/modp_b64_rvv.cc",

Review Comment:
   `BUTIL_SRCS` includes `src/butil/third_party/modp_b64/modp_b64_rvv.cc`, but that file does not exist in the repo (glob found no matches). This will break Bazel builds with a missing input file error. Remove the entry or add the corresponding source file in this PR.



##########
src/butil/string_compare_rvv.cc:
##########
@@ -0,0 +1,105 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+/**
+ * \file
+ * RISC-V Vector (RVV) accelerated memcmp/memchr for StringPiece operations.
+ *
+ * Algorithm follows glibc's RVV memcmp/memchr patterns:
+ * - Use vsetvli with e8/m8 for maximum throughput (hardware-adaptive VL)
+ * - Use vfirst.m to find first differing/matching byte (no vcpop needed)
+ * - Simple loop: load → compare → find first → branch
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * License); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "butil/strings/string_piece.h"
+
+#if defined(__riscv) && defined(__riscv_vector)
+#include <riscv_vector.h>
+#include <stdint.h>
+#include <string.h>
+
+namespace butil {
+
+/**
+ * rvv_memcmp - RVV-accelerated byte comparison following glibc pattern.
+ *
+ * Compares two byte arrays and returns:
+ *   < 0 if p1 < p2
+ *     0 if p1 == p2
+ *   > 0 if p1 > p2
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.
+ */
+int rvv_memcmp(const void* p1, const void* p2, size_t n) {
+    const uint8_t* src1 = static_cast<const uint8_t*>(p1);
+    const uint8_t* src2 = static_cast<const uint8_t*>(p2);
+    size_t remaining = n;
+
+    while (remaining > 0) {
+        size_t vl = __riscv_vsetvl_e8m8(remaining);
+        vuint8m8_t v1 = __riscv_vle8_v_u8m8(src1, vl);
+        vuint8m8_t v2 = __riscv_vle8_v_u8m8(src2, vl);
+        vbool1_t neq = __riscv_vmsne_vv_u8m8_b1(v1, v2, vl);
+        long first = __riscv_vfirst_m_b1(neq, vl);
+
+        if (first >= 0) {
+            uint8_t b1 = src1[first];
+            uint8_t b2 = src2[first];
+            return (b1 < b2) ? -1 : 1;
+        }

Review Comment:
   `rvv_memcmp` currently returns only -1 or 1 on mismatch, which may change observable behavior vs `traits_type::compare`/`memcmp` (many implementations return the byte difference). It also indexes with `long` directly. Returning the byte difference and casting the index preserves prior semantics and avoids sign/diagnostic issues.



##########
src/butil/string_compare_rvv.cc:
##########
@@ -0,0 +1,105 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+/**
+ * \file
+ * RISC-V Vector (RVV) accelerated memcmp/memchr for StringPiece operations.
+ *
+ * Algorithm follows glibc's RVV memcmp/memchr patterns:
+ * - Use vsetvli with e8/m8 for maximum throughput (hardware-adaptive VL)
+ * - Use vfirst.m to find first differing/matching byte (no vcpop needed)
+ * - Simple loop: load → compare → find first → branch
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * License); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "butil/strings/string_piece.h"
+
+#if defined(__riscv) && defined(__riscv_vector)
+#include <riscv_vector.h>
+#include <stdint.h>
+#include <string.h>
+
+namespace butil {
+
+/**
+ * rvv_memcmp - RVV-accelerated byte comparison following glibc pattern.
+ *
+ * Compares two byte arrays and returns:
+ *   < 0 if p1 < p2
+ *     0 if p1 == p2
+ *   > 0 if p1 > p2
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.
+ */
+int rvv_memcmp(const void* p1, const void* p2, size_t n) {
+    const uint8_t* src1 = static_cast<const uint8_t*>(p1);
+    const uint8_t* src2 = static_cast<const uint8_t*>(p2);
+    size_t remaining = n;
+
+    while (remaining > 0) {
+        size_t vl = __riscv_vsetvl_e8m8(remaining);
+        vuint8m8_t v1 = __riscv_vle8_v_u8m8(src1, vl);
+        vuint8m8_t v2 = __riscv_vle8_v_u8m8(src2, vl);
+        vbool1_t neq = __riscv_vmsne_vv_u8m8_b1(v1, v2, vl);
+        long first = __riscv_vfirst_m_b1(neq, vl);
+
+        if (first >= 0) {
+            uint8_t b1 = src1[first];
+            uint8_t b2 = src2[first];
+            return (b1 < b2) ? -1 : 1;
+        }
+
+        src1 += vl;
+        src2 += vl;
+        remaining -= vl;
+    }
+    return 0;
+}
+
+/**
+ * rvv_memchr - RVV-accelerated byte search following glibc pattern.
+ *
+ * Searches for the first occurrence of byte 'c' in buffer 's' of length 'n'.
+ * Returns pointer to first match, or NULL if not found.
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.

Review Comment:
   The doc comment says this returns `NULL` when not found, but the implementation returns `nullptr`. Aligning the comment with the actual return value avoids confusion for readers.



##########
src/butil/string_compare_rvv.cc:
##########
@@ -0,0 +1,105 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+/**
+ * \file
+ * RISC-V Vector (RVV) accelerated memcmp/memchr for StringPiece operations.
+ *
+ * Algorithm follows glibc's RVV memcmp/memchr patterns:
+ * - Use vsetvli with e8/m8 for maximum throughput (hardware-adaptive VL)
+ * - Use vfirst.m to find first differing/matching byte (no vcpop needed)
+ * - Simple loop: load → compare → find first → branch
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * License); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "butil/strings/string_piece.h"
+
+#if defined(__riscv) && defined(__riscv_vector)
+#include <riscv_vector.h>
+#include <stdint.h>
+#include <string.h>
+
+namespace butil {
+
+/**
+ * rvv_memcmp - RVV-accelerated byte comparison following glibc pattern.
+ *
+ * Compares two byte arrays and returns:
+ *   < 0 if p1 < p2
+ *     0 if p1 == p2
+ *   > 0 if p1 > p2
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.
+ */
+int rvv_memcmp(const void* p1, const void* p2, size_t n) {
+    const uint8_t* src1 = static_cast<const uint8_t*>(p1);
+    const uint8_t* src2 = static_cast<const uint8_t*>(p2);
+    size_t remaining = n;
+
+    while (remaining > 0) {
+        size_t vl = __riscv_vsetvl_e8m8(remaining);
+        vuint8m8_t v1 = __riscv_vle8_v_u8m8(src1, vl);
+        vuint8m8_t v2 = __riscv_vle8_v_u8m8(src2, vl);
+        vbool1_t neq = __riscv_vmsne_vv_u8m8_b1(v1, v2, vl);
+        long first = __riscv_vfirst_m_b1(neq, vl);
+
+        if (first >= 0) {
+            uint8_t b1 = src1[first];
+            uint8_t b2 = src2[first];
+            return (b1 < b2) ? -1 : 1;
+        }
+
+        src1 += vl;
+        src2 += vl;
+        remaining -= vl;
+    }
+    return 0;
+}
+
+/**
+ * rvv_memchr - RVV-accelerated byte search following glibc pattern.
+ *
+ * Searches for the first occurrence of byte 'c' in buffer 's' of length 'n'.
+ * Returns pointer to first match, or NULL if not found.
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.
+ */
+const void* rvv_memchr(const void* s, int c, size_t n) {
+    const uint8_t* src = static_cast<const uint8_t*>(s);
+    uint8_t ch = static_cast<uint8_t>(c);
+    size_t remaining = n;
+
+    while (remaining > 0) {
+        size_t vl = __riscv_vsetvl_e8m8(remaining);
+        vuint8m8_t v = __riscv_vle8_v_u8m8(src, vl);
+        vbool1_t eq = __riscv_vmseq_vx_u8m8_b1(v, ch, vl);
+        long first = __riscv_vfirst_m_b1(eq, vl);
+
+        if (first >= 0) {
+            return src + first;
+        }

Review Comment:
   This PR adds an RVV `rvv_memchr`, but there are no call sites: `StringPiece` character search currently uses `std::find`/`std::search` in `string_piece.cc`, and a repo-wide search only finds the declaration/definition of `rvv_memchr`. If the goal is to accelerate `StringPiece::find(char)`/related functions, the dispatch needs to be wired into those implementations; otherwise consider dropping `rvv_memchr` to avoid carrying unused code.



##########
src/butil/strings/string_piece.h:
##########
@@ -43,6 +43,12 @@
 
 namespace butil {
 
+// RVV-accelerated byte comparison and search (implemented in string_compare_rvv.cc)
+#if defined(__riscv) && defined(__riscv_vector)
+int rvv_memcmp(const void* p1, const void* p2, size_t n);
+const void* rvv_memchr(const void* s, int c, size_t n);
+#endif

Review Comment:
   `rvv_memcmp`/`rvv_memchr` are declared in a public header and called from an inline function, but they are not marked with `BUTIL_EXPORT`. In `COMPONENT_BUILD` configurations this can cause unresolved symbols when linking users of `StringPiece` against the shared butil component.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "Felix-Gong (via GitHub)" <gi...@apache.org>.
Felix-Gong commented on PR #3390:
URL: https://github.com/apache/brpc/pull/3390#issuecomment-4999602259

   ## Review Responses
   
   Replies to all5 Copilot review comments (comments are on the pre-rebase commit, so replying here):
   
   ### 1. BUILD.bazel: `modp_b64_rvv.cc` does not exist
   **Fixed.** Removed `modp_b64_rvv.cc` from `BUTIL_SRCS` — it was inadvertently included from a prior PR (#3388) and does not belong in this change.
   
   ### 2. string_piece.h: Missing `BUTIL_EXPORT`
   **Fixed.** Added `BUTIL_EXPORT` to the `rvv_memcmp` declaration to ensure proper symbol visibility in shared library builds.
   
   ### 3. string_compare_rvv.cc: Return value semantics
   **Fixed.** Changed to `return static_cast<int>(src1[first]) - static_cast<int>(src2[first]);` which matches glibc `memcmp` semantics and preserves the byte-difference contract expected by `traits_type::compare`.
   
   ### 4. string_compare_rvv.cc: NULL vs nullptr
   **Addressed.** `rvv_memchr` has been removed from this PR and will be added in a follow-up PR with proper `StringPiece::find` dispatch. The doc comment and implementation will be aligned in that PR.
   
   ### 5. rvv_memchr has no call sites
   **Agreed.** `rvv_memchr` has been removed from this PR. It will be reintroduced in a follow-up PR that also wires the dispatch into `StringPiece::find(char)`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "Felix-Gong (via GitHub)" <gi...@apache.org>.
Felix-Gong commented on code in PR #3390:
URL: https://github.com/apache/brpc/pull/3390#discussion_r3600788927


##########
src/butil/strings/string_piece.h:
##########
@@ -43,6 +43,12 @@
 
 namespace butil {
 
+// RVV-accelerated byte comparison and search (implemented in string_compare_rvv.cc)
+#if defined(__riscv) && defined(__riscv_vector)
+int rvv_memcmp(const void* p1, const void* p2, size_t n);
+const void* rvv_memchr(const void* s, int c, size_t n);
+#endif

Review Comment:
   Fixed. Added BUTIL_EXPORT to the rvv_memcmp declaration to ensure proper symbol visibility in shared library builds.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "Felix-Gong (via GitHub)" <gi...@apache.org>.
Felix-Gong commented on PR #3390:
URL: https://github.com/apache/brpc/pull/3390#issuecomment-4999566207

   ## Review Comments Response
   
   ### 1. BUILD.bazel: `modp_b64_rvv.cc` does not exist
   > `BUTIL_SRCS` includes `src/butil/third_party/modp_b64/modp_b64_rvv.cc`, but that file does not exist in the repo.
   
   **Fixed.** Removed `modp_b64_rvv.cc` from `BUTIL_SRCS` — it was inadvertently included from a prior PR (#3388) and does not belong in this change.
   
   ### 2. string_piece.h: Missing `BUTIL_EXPORT`
   > `rvv_memcmp`/`rvv_memchr` are declared in a public header but not marked with `BUTIL_EXPORT`.
   
   **Fixed.** Added `BUTIL_EXPORT` to the `rvv_memcmp` declaration to ensure proper symbol visibility in shared library builds.
   
   ### 3. string_compare_rvv.cc: Return value semantics
   > `rvv_memcmp` returns only -1 or 1 on mismatch, which may change observable behavior vs `memcmp`.
   
   **Fixed.** Changed to `return static_cast<int>(src1[first]) - static_cast<int>(src2[first]);` which matches glibc `memcmp` semantics and preserves the byte-difference contract expected by `traits_type::compare`.
   
   ### 4. string_compare_rvv.cc: NULL vs nullptr
   > The doc comment says this returns `NULL` when not found, but the implementation returns `nullptr`.
   
   **Addressed.** `rvv_memchr` has been removed from this PR and will be added in a follow-up PR with proper `StringPiece::find` dispatch. The doc comment and implementation will be aligned in that PR.
   
   ### 5. rvv_memchr has no call sites
   > This PR adds an RVV `rvv_memchr`, but there are no call sites. Consider dropping `rvv_memchr` to avoid carrying unused code.
   
   **Agreed.** `rvv_memchr` has been removed from this PR. It will be reintroduced in a follow-up PR that also wires the dispatch into `StringPiece::find(char)` via `internal::find` in `string_piece.cc`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "Felix-Gong (via GitHub)" <gi...@apache.org>.
Felix-Gong commented on code in PR #3390:
URL: https://github.com/apache/brpc/pull/3390#discussion_r3600794687


##########
src/butil/string_compare_rvv.cc:
##########
@@ -0,0 +1,105 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+/**
+ * \file
+ * RISC-V Vector (RVV) accelerated memcmp/memchr for StringPiece operations.
+ *
+ * Algorithm follows glibc's RVV memcmp/memchr patterns:
+ * - Use vsetvli with e8/m8 for maximum throughput (hardware-adaptive VL)
+ * - Use vfirst.m to find first differing/matching byte (no vcpop needed)
+ * - Simple loop: load → compare → find first → branch
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * License); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "butil/strings/string_piece.h"
+
+#if defined(__riscv) && defined(__riscv_vector)
+#include <riscv_vector.h>
+#include <stdint.h>
+#include <string.h>
+
+namespace butil {
+
+/**
+ * rvv_memcmp - RVV-accelerated byte comparison following glibc pattern.
+ *
+ * Compares two byte arrays and returns:
+ *   < 0 if p1 < p2
+ *     0 if p1 == p2
+ *   > 0 if p1 > p2
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.
+ */
+int rvv_memcmp(const void* p1, const void* p2, size_t n) {
+    const uint8_t* src1 = static_cast<const uint8_t*>(p1);
+    const uint8_t* src2 = static_cast<const uint8_t*>(p2);
+    size_t remaining = n;
+
+    while (remaining > 0) {
+        size_t vl = __riscv_vsetvl_e8m8(remaining);
+        vuint8m8_t v1 = __riscv_vle8_v_u8m8(src1, vl);
+        vuint8m8_t v2 = __riscv_vle8_v_u8m8(src2, vl);
+        vbool1_t neq = __riscv_vmsne_vv_u8m8_b1(v1, v2, vl);
+        long first = __riscv_vfirst_m_b1(neq, vl);
+
+        if (first >= 0) {
+            uint8_t b1 = src1[first];
+            uint8_t b2 = src2[first];
+            return (b1 < b2) ? -1 : 1;
+        }
+
+        src1 += vl;
+        src2 += vl;
+        remaining -= vl;
+    }
+    return 0;
+}
+
+/**
+ * rvv_memchr - RVV-accelerated byte search following glibc pattern.
+ *
+ * Searches for the first occurrence of byte 'c' in buffer 's' of length 'n'.
+ * Returns pointer to first match, or NULL if not found.
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.

Review Comment:
   Fixed. rvv_memchr has been removed from this PR and will be added in a follow-up PR with proper StringPiece::find dispatch. The doc comment and implementation will be aligned in that PR.



##########
src/butil/string_compare_rvv.cc:
##########
@@ -0,0 +1,105 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+/**
+ * \file
+ * RISC-V Vector (RVV) accelerated memcmp/memchr for StringPiece operations.
+ *
+ * Algorithm follows glibc's RVV memcmp/memchr patterns:
+ * - Use vsetvli with e8/m8 for maximum throughput (hardware-adaptive VL)
+ * - Use vfirst.m to find first differing/matching byte (no vcpop needed)
+ * - Simple loop: load → compare → find first → branch
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * License); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "butil/strings/string_piece.h"
+
+#if defined(__riscv) && defined(__riscv_vector)
+#include <riscv_vector.h>
+#include <stdint.h>
+#include <string.h>
+
+namespace butil {
+
+/**
+ * rvv_memcmp - RVV-accelerated byte comparison following glibc pattern.
+ *
+ * Compares two byte arrays and returns:
+ *   < 0 if p1 < p2
+ *     0 if p1 == p2
+ *   > 0 if p1 > p2
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.
+ */
+int rvv_memcmp(const void* p1, const void* p2, size_t n) {
+    const uint8_t* src1 = static_cast<const uint8_t*>(p1);
+    const uint8_t* src2 = static_cast<const uint8_t*>(p2);
+    size_t remaining = n;
+
+    while (remaining > 0) {
+        size_t vl = __riscv_vsetvl_e8m8(remaining);
+        vuint8m8_t v1 = __riscv_vle8_v_u8m8(src1, vl);
+        vuint8m8_t v2 = __riscv_vle8_v_u8m8(src2, vl);
+        vbool1_t neq = __riscv_vmsne_vv_u8m8_b1(v1, v2, vl);
+        long first = __riscv_vfirst_m_b1(neq, vl);
+
+        if (first >= 0) {
+            uint8_t b1 = src1[first];
+            uint8_t b2 = src2[first];
+            return (b1 < b2) ? -1 : 1;
+        }
+
+        src1 += vl;
+        src2 += vl;
+        remaining -= vl;
+    }
+    return 0;
+}
+
+/**
+ * rvv_memchr - RVV-accelerated byte search following glibc pattern.
+ *
+ * Searches for the first occurrence of byte 'c' in buffer 's' of length 'n'.
+ * Returns pointer to first match, or NULL if not found.
+ *
+ * Uses RVV m8 with hardware-adaptive VL for maximum throughput.
+ */
+const void* rvv_memchr(const void* s, int c, size_t n) {
+    const uint8_t* src = static_cast<const uint8_t*>(s);
+    uint8_t ch = static_cast<uint8_t>(c);
+    size_t remaining = n;
+
+    while (remaining > 0) {
+        size_t vl = __riscv_vsetvl_e8m8(remaining);
+        vuint8m8_t v = __riscv_vle8_v_u8m8(src, vl);
+        vbool1_t eq = __riscv_vmseq_vx_u8m8_b1(v, ch, vl);
+        long first = __riscv_vfirst_m_b1(eq, vl);
+
+        if (first >= 0) {
+            return src + first;
+        }

Review Comment:
   Agreed. rvv_memchr has been removed from this PR. It will be reintroduced in a follow-up PR that also wires the dispatch into StringPiece::find(char) via internal::find in string_piece.cc.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "Felix-Gong (via GitHub)" <gi...@apache.org>.
Felix-Gong commented on code in PR #3390:
URL: https://github.com/apache/brpc/pull/3390#discussion_r3600786549


##########
BUILD.bazel:
##########
@@ -128,6 +128,7 @@ BUTIL_SRCS = [
     "src/butil/third_party/icu/icu_utf.cc",
     "src/butil/third_party/superfasthash/superfasthash.c",
     "src/butil/third_party/modp_b64/modp_b64.cc",
+    "src/butil/third_party/modp_b64/modp_b64_rvv.cc",

Review Comment:
   Fixed. Removed modp_b64_rvv.cc from BUTIL_SRCS — it was inadvertently included from a prior PR (#3388) and doesn't belong in this change.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org


Re: [PR] optimize StringPiece memcmp for RISC-V with RVV (brpc)

Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright merged PR #3390:
URL: https://github.com/apache/brpc/pull/3390


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org