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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to