]> git.proxmox.com Git - wasi-libc.git/commitdiff
Implement BULK_MEMORY_THRESHOLD
authorCheng Shao <astrohavoc@gmail.com>
Wed, 18 May 2022 19:19:27 +0000 (19:19 +0000)
committerDan Gohman <dev@sunfishcode.online>
Fri, 20 May 2022 20:39:43 +0000 (13:39 -0700)
Makefile
libc-top-half/musl/src/string/memcpy.c
libc-top-half/musl/src/string/memmove.c
libc-top-half/musl/src/string/memset.c

index 44d55a5f0261ff1f91768e7fcaee37b72f628bc9..4341980f4faa483ac2ae5c92934e36a4f816c20c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -22,6 +22,13 @@ BUILD_LIBC_TOP_HALF ?= yes
 # The directory where we're store intermediate artifacts.
 OBJDIR ?= $(CURDIR)/build
 
+# When the length is no larger than this threshold, we consider the
+# overhead of bulk memory opcodes to outweigh the performance benefit,
+# and fall back to the original musl implementation. See
+# https://github.com/WebAssembly/wasi-libc/pull/263 for relevant
+# discussion
+BULK_MEMORY_THRESHOLD ?= 32
+
 # Variables from this point on are not meant to be overridable via the
 # make command-line.
 
@@ -394,6 +401,9 @@ $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS): CFLAGS += \
 $(BULK_MEMORY_OBJS): CFLAGS += \
         -mbulk-memory
 
+$(BULK_MEMORY_OBJS): CFLAGS += \
+        -DBULK_MEMORY_THRESHOLD=$(BULK_MEMORY_THRESHOLD)
+
 $(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
            -D_WASI_EMULATED_SIGNAL
 
index 18cf47447e9bcfc85134a9e6e6b962af59a9a696..3cc7e28f3bd38f866dfbfa8404ab47a04c11f679 100644 (file)
@@ -5,8 +5,9 @@
 void *memcpy(void *restrict dest, const void *restrict src, size_t n)
 {
 #if defined(__wasm_bulk_memory__)
-       return __builtin_memcpy(dest, src, n);
-#else
+       if (n > BULK_MEMORY_THRESHOLD)
+         return __builtin_memcpy(dest, src, n);
+#endif
        unsigned char *d = dest;
        const unsigned char *s = src;
 
@@ -124,5 +125,4 @@ void *memcpy(void *restrict dest, const void *restrict src, size_t n)
 
        for (; n; n--) *d++ = *s++;
        return dest;
-#endif // __wasm_bulk_memory__
 }
index 24a7739406585a8d9fac731ac572790715c5f971..7376a520bbfcab411646b4d49903a127a242b4f0 100644 (file)
@@ -9,8 +9,9 @@ typedef __attribute__((__may_alias__)) size_t WT;
 void *memmove(void *dest, const void *src, size_t n)
 {
 #if defined(__wasm_bulk_memory__)
-       return __builtin_memmove(dest, src, n);
-#else
+       if (n > BULK_MEMORY_THRESHOLD)
+               return __builtin_memmove(dest, src, n);
+#endif
        char *d = dest;
        const char *s = src;
 
@@ -42,5 +43,4 @@ void *memmove(void *dest, const void *src, size_t n)
        }
 
        return dest;
-#endif // __wasm_bulk_memory__
 }
index 643695677afce8aeb1b0c17b95218cdf138d7f8a..f64c9cf5ae96a7a021990276807d58825effd19a 100644 (file)
@@ -4,8 +4,9 @@
 void *memset(void *dest, int c, size_t n)
 {
 #if defined(__wasm_bulk_memory__)
-       return __builtin_memset(dest, c, n);
-#else
+       if (n > BULK_MEMORY_THRESHOLD)
+               return __builtin_memset(dest, c, n);
+#endif
        unsigned char *s = dest;
        size_t k;
 
@@ -90,5 +91,4 @@ void *memset(void *dest, int c, size_t n)
 #endif
 
        return dest;
-#endif // __wasm_bulk_memory__
 }