Felix-Gong commented on code in PR #3360:
URL: https://github.com/apache/brpc/pull/3360#discussion_r3490609015


##########
src/butil/third_party/snappy/snappy.cc:
##########
@@ -97,9 +97,33 @@ static const uint32_t kMaximumTagLength = 5;  // 
COPY_4_BYTE_OFFSET plus the act
 // or memmove().
 static inline void IncrementalCopy(const char* src, char* op, ssize_t len) {
     assert(len > 0);
+#if defined(__riscv) && __riscv_xlen == 64
+    // RISC-V optimized: use 8-byte copies when possible
+    if (len >= 8 && (op - src >= 8 || src - op >= 8)) {
+        // Non-overlapping or safe overlap: copy 8 bytes at a time
+        do {
+            uint64_t tmp;
+            __asm__ volatile(
+                "ld %0, %1\n\t"
+                "sd %0, %2\n\t"
+                : "=&r"(tmp)
+                : "m"(*(const uint64_t*)src), "m"(*(uint64_t*)op)
+                : "memory");
+            src += 8;
+            op += 8;
+            len -= 8;
+        } while (len >= 8);
+    }

Review Comment:
   Fixed. Added runtime alignment check with `((uintptr_t)src | (uintptr_t)op) 
& 7) == 0`. Only uses `ld`/`sd` when both pointers are 8-byte aligned, 
otherwise falls back to byte-by-byte copy.



##########
src/butil/third_party/snappy/snappy-stubs-internal.h:
##########
@@ -164,6 +164,16 @@ inline void UNALIGNED_STORE64(void *p, uint64_t v) {
 // This can be more efficient than UNALIGNED_LOAD64 + UNALIGNED_STORE64
 // on some platforms, in particular ARM.
 inline void UnalignedCopy64(const void *src, void *dst) {
+#if defined(__riscv) && __riscv_xlen == 64
+    // RISC-V optimized: single ld/sd pair for 8-byte copy
+    uint64_t tmp;
+    __asm__ volatile(
+        "ld %0, %1\n\t"
+        "sd %0, %2\n\t"
+        : "=&r"(tmp)
+        : "m"(*(const uint64_t*)src), "m"(*(uint64_t*)dst)
+        : "memory");
+#else

Review Comment:
   Fixed. Added alignment check `(((uintptr_t)src | (uintptr_t)dst) & 7) == 0` 
before using `ld`/`sd`. Falls back to `memcpy` for unaligned addresses.



-- 
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