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 |
Code
#pragma once
#include <cassert>
#include <utility>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <bit>
#if defined(_MSC_VER) && defined(__AVX512BW__)
#define MEMSET_DECOMP_MSVC_AVX512BW_IMPLIES_BMI2
#endif
#if defined(__AVX512BW__) && (defined(__BMI2__) || defined(MEMSET_DECOMP_MSVC_AVX512BW_IMPLIES_BMI2))
#include <immintrin.h>
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#define MEMSET_DECOMP_INLINE [[msvc::forceinline]] inline
#elif defined(__GNUC__) || defined(__clang__)
#define MEMSET_DECOMP_INLINE __attribute__((always_inline)) inline
#else
#define MEMSET_DECOMP_INLINE inline
#endif
namespace memset_decomp_ns {
#if defined(__AVX512F__)
inline constexpr std::uint64_t register_width{ 64 };
#elif defined(__AVX2__)
inline constexpr std::uint64_t register_width{ 32 };
#else
inline constexpr std::uint64_t register_width{ 16 };
#endif
inline constexpr std::uint64_t small_set_limit{ 16 };
inline constexpr std::uint64_t smallest_pair_size{ std::bit_floor(small_set_limit + 1) };
inline constexpr std::uint64_t largest_pair_size{ 128 };
template<std::uint64_t max_bytes>
inline constexpr std::uint64_t top_pair_size{ std::bit_floor(max_bytes) < largest_pair_size ? std::bit_floor(max_bytes) : largest_pair_size };
template<std::uint64_t max_bytes> inline constexpr std::uint64_t pair_class_count{
top_pair_size<max_bytes> < smallest_pair_size ? 0 : static_cast<std::uint64_t>(std::bit_width(top_pair_size<max_bytes>) - std::bit_width(smallest_pair_size)) + 1
};
template<std::byte value, std::uint64_t size> struct pow2_set {
static_assert(std::has_single_bit(size), "Sorry, but you can only memset a power-of-2 size.");
MEMSET_DECOMP_INLINE static void impl(std::byte* dst) noexcept {
std::memset(dst, std::to_integer<int>(value), size);
}
};
template<std::byte value, std::uint64_t pair_size, std::uint64_t chunk_size, typename sequence_type> struct pair_set_impl;
template<std::byte value, std::uint64_t pair_size, std::uint64_t chunk_size, std::uint64_t... indices>
struct pair_set_impl<value, pair_size, chunk_size, std::integer_sequence<std::uint64_t, indices...>> {
MEMSET_DECOMP_INLINE static void impl(std::byte* dst, std::uint64_t byte_count) noexcept {
std::byte* tail = dst + byte_count - pair_size;
(pow2_set<value, chunk_size>::impl(dst + indices * chunk_size), ...);
(pow2_set<value, chunk_size>::impl(tail + indices * chunk_size), ...);
}
};
template<std::byte value, std::uint64_t pair_size> struct pair_set {
static_assert(std::has_single_bit(pair_size), "Sorry, but pairs must be a power-of-2 size.");
static constexpr std::uint64_t chunk_size{ pair_size < register_width ? pair_size : register_width };
static constexpr std::uint64_t chunk_count{ pair_size / chunk_size };
MEMSET_DECOMP_INLINE static void impl(std::byte* dst, std::uint64_t byte_count) noexcept {
pair_set_impl<value, pair_size, chunk_size, std::make_integer_sequence<std::uint64_t, chunk_count>>::impl(dst, byte_count);
}
};
template<std::byte value> struct small_set {
MEMSET_DECOMP_INLINE static void impl(std::byte* dst, std::uint64_t byte_count) noexcept {
if (byte_count >= 8) {
pow2_set<value, 8>::impl(dst);
pow2_set<value, 8>::impl(dst + byte_count - 8);
} else if (byte_count >= 4) {
pow2_set<value, 4>::impl(dst);
pow2_set<value, 4>::impl(dst + byte_count - 4);
} else if (byte_count >= 2) {
pow2_set<value, 2>::impl(dst);
pow2_set<value, 2>::impl(dst + byte_count - 2);
} else if (byte_count != 0) {
*dst = value;
}
}
};
template<std::byte value, typename sequence_type> struct pair_dispatcher;
template<std::byte value, std::uint64_t... class_indices> struct pair_dispatcher<value, std::integer_sequence<std::uint64_t, class_indices...>> {
template<std::uint64_t pair_size> MEMSET_DECOMP_INLINE static bool try_pair(std::byte* dst, std::uint64_t byte_count) noexcept {
if (byte_count <= pair_size * 2) {
pair_set<value, pair_size>::impl(dst, byte_count);
return true;
}
return false;
}
MEMSET_DECOMP_INLINE static void impl(std::byte* dst, std::uint64_t byte_count) noexcept {
if (byte_count <= small_set_limit) {
small_set<value>::impl(dst, byte_count);
} else if (!(try_pair<(smallest_pair_size << class_indices)>(dst, byte_count) || ...)) {
std::memset(dst, std::to_integer<int>(value), byte_count);
}
}
};
template<std::byte value, std::uint64_t max_bytes> using memset_dispatcher = pair_dispatcher<value, std::make_integer_sequence<std::uint64_t, pair_class_count<max_bytes>>>;
#if defined(__AVX512BW__) && (defined(__BMI2__) || defined(MEMSET_DECOMP_MSVC_AVX512BW_IMPLIES_BMI2))
inline constexpr std::uint64_t masked_width{ 64 };
MEMSET_DECOMP_INLINE __mmask64 length_mask(std::uint64_t byte_count) noexcept {
return _bzhi_u64(~0ull, static_cast<std::uint32_t>(byte_count));
}
template<std::byte value> struct masked_set {
MEMSET_DECOMP_INLINE static void impl(std::byte* dst, std::uint64_t byte_count) noexcept {
const __m512i fill{ _mm512_set1_epi8(static_cast<char>(value)) };
if (byte_count <= masked_width) {
_mm512_mask_storeu_epi8(dst, length_mask(byte_count), fill);
} else {
_mm512_storeu_si512(dst, fill);
_mm512_mask_storeu_epi8(dst + masked_width, length_mask(byte_count - masked_width), fill);
}
}
};
template<std::uint64_t max_bytes> MEMSET_DECOMP_INLINE bool fits_masked(std::uint64_t byte_count) noexcept {
return max_bytes <= masked_width * 2 || byte_count <= masked_width * 2;
}
template<std::byte value, std::uint64_t max_bytes> MEMSET_DECOMP_INLINE void memset_decomp_up_to(void* dst, std::uint64_t byte_count) noexcept {
assert(byte_count <= max_bytes);
if (fits_masked<max_bytes>(byte_count)) {
masked_set<value>::impl(static_cast<std::byte*>(dst), byte_count);
} else {
memset_dispatcher<value, max_bytes>::impl(static_cast<std::byte*>(dst), byte_count);
}
}
#else
template<std::byte value, std::uint64_t max_bytes> MEMSET_DECOMP_INLINE void memset_decomp_up_to(void* dst, std::uint64_t byte_count) noexcept {
assert(byte_count <= max_bytes);
memset_dispatcher<value, max_bytes>::impl(static_cast<std::byte*>(dst), byte_count);
}
#endif
template<std::byte value> MEMSET_DECOMP_INLINE void memset_decomp(void* dst, std::uint64_t byte_count) noexcept {
memset_decomp_up_to<value, std::numeric_limits<std::uint64_t>::max()>(dst, byte_count);
}
}
#undef MEMSET_DECOMP_MSVC_AVX512BW_IMPLIES_BMI2
#undef MEMSET_DECOMP_INLINE
11
u/jk-jeon 7h ago
Did you compare the resulting assembly with
std::memset? As far as I understand, doing a similar decomposition by size is howstd::memsetis implemented from the first place. What do you think is the core difference? Also, did you compare the code size of the functions?If it's a genuine improvement, how about contributing back to stdlib impls?
3
u/Kriemhilt 7h ago
Why are you specifically optimizing misaligned writes? Is that generally useful, or is it your specific use-case?
•
u/c0d3c 3h ago
It reminds me of the good old days of optimizing unaligned fills and copies in hand-rolled x86 assembler. That would have been compile time, in a fashion :-)
IIRC I did something more recently maybe 15 years ago with inline asm and found gains vs memcpy - which surprised me, of course GCC has come a long way since, but general purpose always has a cost.