r/cpp • u/RealTimeChris :upvote: • 14m ago
Beating std::memset by 1.2-2.3x or more on runtime lengths of 1–128 bytes
Hey everyone, so it seems as though significant performance gains can be made by decomposing std::memset into a collection of "compile-time known" sizes, when running it with runtime-only known lengths. We bench-marked the code that follows by performing 4096 memsets per iteration into a 64 KiB buffer, with misaligned destination offsets and lengths that are uniformly randomly distributed across [1, N].
Tbh the lengths themselves are sourced from a 64K-entry LCG table, where we vary our starting position on each iteration in order so as to prevent the branch predictor from learning the sequence, and pass the lengths through a volatile to obscure them from the compiler. Both implementations are given a compile-time known fill value of 0x5A.
We're measuring the aggregate number of bytes set per iteration divided by the elapsed time (in MB/s).
For sampling, we start with 60 iterations and repeatedly double the number of iterations per epoch until we reach either our 10-second time limit or our maximum iteration count. We retain the epoch that yielded the lowest total of the relative standard error and the shift in the mean between epochs, with our convergence thresholds being 10% and 5%, respectively.
As far as correctness goes, we're exhaustively testing every length from 0 through 300 across the various destination offsets, verifying both the integrity of the surrounding guard bytes and the equivalence of the resultant buffer contents to those produced by std::memset - let me know what you think about this. Cheers!:
### Linux-Clang (EPYC 9V45, AVX-512, Clang 24)
| Length | memset_decomp | std_memset | Speedup | memset_decomp_up_to | std_memset | Speedup |
|---|--:|--:|--:|--:|--:|--:|
| 1-15 | 8,005 | 4,397 | **1.82x** | 9,045 | 4,514 | **2.00x** |
| 1-16 | 8,482 | 4,769 | **1.78x** | 9,610 | 4,718 | **2.04x** |
| 1-17 | 9,387 | 4,949 | **1.90x** | 10,190 | 4,964 | **2.05x** |
| 1-31 | 16,629 | 8,918 | **1.86x** | 18,145 | 8,940 | **2.03x** |
| 1-32 | 17,182 | 9,221 | **1.86x** | 18,752 | 8,992 | **2.09x** |
| 1-33 | 17,141 | 9,526 | **1.80x** | 19,034 | 9,458 | **2.01x** |
| 1-63 | 33,019 | 17,982 | **1.84x** | 36,421 | 17,927 | **2.03x** |
| 1-64 | 32,142 | 16,880 | **1.90x** | 35,420 | 16,362 | **2.16x** |
| 1-65 | 31,467 | 15,548 | **2.02x** | 34,651 | 15,030 | **2.31x** |
| 1-127 | 16,425 | 11,027 | **1.49x** | 16,286 | 11,201 | **1.45x** |
| 1-128 | 16,098 | 11,067 | **1.45x** | 16,930 | 11,411 | **1.48x** |
| 1-129 | 17,296 | 11,109 | **1.56x** | 16,672 | 11,025 | **1.51x** |
### Linux-GCC (EPYC 9V45, AVX-512, GCC 16.0.1)
| Length | memset_decomp | std_memset | Speedup | memset_decomp_up_to | std_memset | Speedup |
|---|--:|--:|--:|--:|--:|--:|
| 1-15 | 8,133 | 4,490 | **1.81x** | 8,958 | 4,433 | **2.02x** |
| 1-16 | 8,428 | 4,659 | **1.81x** | 9,460 | 4,863 | **1.95x** |
| 1-17 | 8,738 | 4,901 | **1.78x** | 10,133 | 5,082 | **1.99x** |
| 1-31 | 16,117 | 8,838 | **1.82x** | 18,011 | 9,078 | **1.98x** |
| 1-32 | 16,276 | 8,473 | **1.92x** | 18,727 | 8,352 | **2.24x** |
| 1-33 | 16,324 | 9,775 | **1.67x** | 18,810 | 9,290 | **2.02x** |
| 1-63 | 30,706 | 18,042 | **1.70x** | 36,228 | 17,605 | **2.06x** |
| 1-64 | 31,428 | 16,136 | **1.95x** | 36,385 | 17,063 | **2.13x** |
| 1-65 | 28,891 | 15,474 | **1.87x** | 33,924 | 15,601 | **2.17x** |
| 1-127 | 15,166 | 11,936 | **1.27x** | 15,571 | 12,031 | **1.29x** |
| 1-128 | 15,209 | 11,879 | **1.28x** | 15,839 | 12,544 | **1.26x** |
| 1-129 | 14,992 | 12,066 | **1.24x** | 15,363 | 12,521 | **1.23x** |
### Windows-MSVC (Xeon Platinum 8573C, AVX-512, MSVC 19.52)
| Length | memset_decomp | std_memset | Speedup | memset_decomp_up_to | std_memset | Speedup |
|---|--:|--:|--:|--:|--:|--:|
| 1-15 | 3,988 | 631 | **6.32x** | 2,801 | 632 | **4.43x** |
| 1-16 | 4,389 | 675 | **6.50x** | 3,672 | 667 | **5.51x** |
| 1-17 | 4,499 | 678 | **6.63x** | 4,567 | 676 | **6.76x** |
| 1-31 | 7,720 | 1,403 | **5.50x** | 7,854 | 1,392 | **5.64x** |
| 1-32 | 8,079 | 1,492 | **5.41x** | 6,248 | 1,509 | **4.14x** |
| 1-33 | 8,246 | 1,496 | **5.51x** | 4,889 | 1,496 | **3.27x** |
| 1-63 | 13,371 | 2,270 | **5.89x** | 13,382 | 2,240 | **5.97x** |
| 1-64 | 13,538 | 2,400 | **5.64x** | 13,198 | 2,430 | **5.43x** |
| 1-65 | 13,681 | 2,435 | **5.62x** | 13,838 | 2,448 | **5.65x** |
| 1-127 | 8,890 | 4,273 | **2.08x** | 10,010 | 4,315 | **2.32x** |
| 1-128 | 9,038 | 4,235 | **2.13x** | 10,135 | 4,209 | **2.41x** |
| 1-129 | 9,108 | 4,456 | **2.04x** | 9,127 | 4,423 | **2.06x** |
### macOS-Clang (Apple M1 virtual, NEON, Clang 23.1.6)
| Length | memset_decomp | std_memset | Speedup | memset_decomp_up_to | std_memset | Speedup |
|---|--:|--:|--:|--:|--:|--:|
| 1-15 | 1,329 | 691 | **1.92x** | 1,466 | 685 | **2.14x** |
| 1-16 | 1,721 | 711 | **2.42x** | 1,697 | 612 | **2.77x** |
| 1-17 | 1,638 | 819 | **2.00x** | 1,457 | 785 | **1.86x** |
| 1-31 | 2,723 | 1,193 | **2.28x** | 2,586 | 1,137 | **2.28x** |
| 1-32 | 2,106 | 1,266 | **1.66x** | 2,306 | 1,118 | **2.06x** |
| 1-33 | 2,542 | 1,190 | **2.14x** | 2,373 | 1,262 | **1.88x** |
| 1-63 | 5,921 | 2,207 | **2.68x** | 5,316 | 1,978 | **2.69x** |
| 1-64 | 5,914 | 1,908 | **3.10x** | 6,372 | 1,693 | **3.76x** |
| 1-65 | 5,453 | 2,179 | **2.50x** | 6,336 | 2,258 | **2.81x** |
| 1-127 | 11,367 | 4,254 | **2.67x** | 13,020 | 4,957 | **2.63x** |
| 1-128 | 12,855 | 4,835 | **2.66x** | 11,700 | 3,081 | **3.80x** |
| 1-129 | 11,069 | 4,586 | **2.41x** | 12,823 | 5,115 | **2.51x** |
### macOS-GCC (Apple M1 virtual, NEON, GCC 16.2.0)
| Length | memset_decomp | std_memset | Speedup | memset_decomp_up_to | std_memset | Speedup |
|---|--:|--:|--:|--:|--:|--:|
| 1-15 | 1,355 | 732 | **1.85x** | 1,296 | 733 | **1.77x** |
| 1-16 | 874 | 700 | **1.25x** | 1,347 | 669 | **2.01x** |
| 1-17 | 1,682 | 651 | **2.59x** | 1,591 | 737 | **2.16x** |
| 1-31 | 2,511 | 1,266 | **1.98x** | 2,496 | 1,263 | **1.98x** |
| 1-32 | 2,346 | 1,166 | **2.01x** | 2,340 | 1,326 | **1.76x** |
| 1-33 | 2,846 | 1,165 | **2.44x** | 2,229 | 1,189 | **1.88x** |
| 1-63 | 6,503 | 2,322 | **2.80x** | 5,921 | 2,189 | **2.71x** |
| 1-64 | 5,553 | 2,369 | **2.34x** | 5,407 | 2,107 | **2.57x** |
| 1-65 | 5,404 | 2,165 | **2.50x** | 6,166 | 2,233 | **2.76x** |
| 1-127 | 13,762 | 5,243 | **2.62x** | 12,864 | 5,234 | **2.46x** |
| 1-128 | 13,142 | 5,299 | **2.48x** | 11,865 | 5,280 | **2.25x** |
| 1-129 | 11,738 | 4,722 | **2.49x** | 11,505 | 4,665 | **2.47x** |
Here is the code: