Add zmalloc_aligned() and fix SPMC queue buffer alignment#3504
Open
sarthakaggarwal97 wants to merge 4 commits intovalkey-io:unstablefrom
Open
Add zmalloc_aligned() and fix SPMC queue buffer alignment#3504sarthakaggarwal97 wants to merge 4 commits intovalkey-io:unstablefrom
sarthakaggarwal97 wants to merge 4 commits intovalkey-io:unstablefrom
Conversation
c9ee541 to
b3c0e26
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## unstable #3504 +/- ##
=========================================
Coverage 76.58% 76.59%
=========================================
Files 159 159
Lines 80019 80043 +24
=========================================
+ Hits 61283 61307 +24
Misses 18736 18736
🚀 New features to boost your workflow:
|
Contributor
Author
|
Macos Test should get stabilised by #3509 |
20e266c to
83bf327
Compare
madolson
reviewed
Apr 18, 2026
21aa1df to
57ab77a
Compare
Introduce zmalloc_aligned(alignment, size) for cache-line-aligned heap allocations with full Valkey memory tracking. On HAVE_MALLOC_SIZE platforms it wraps posix_memalign directly. On !HAVE_MALLOC_SIZE platforms it over-allocates and stores the raw pointer and a flag-tagged size before the aligned region so zfree() can transparently recover the original pointer. Use zmalloc_aligned() in spmcInit() to guarantee each spmcCell is cache-line aligned, fixing UBSan failures in sanitizer CI builds which force MALLOC=libc. Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
Replace the general-purpose zmalloc_aligned(alignment, size) with a simpler zmalloc_cache_aligned(size) that always aligns to CACHE_LINE_SIZE. This is the only alignment any caller needs, so drop: - The !HAVE_MALLOC_SIZE manual alignment path (MSB flag, raw pointer storage, modified zfree/zfree_with_size/zmalloc_size) - The alignment validation helper - The posix_memalign wrapper The result is ~70 fewer lines with the same practical effect. On !HAVE_MALLOC_SIZE platforms (none that Valkey ships on), it panics at startup rather than silently corrupting memory. Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
57ab77a to
cbd4fb2
Compare
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The SPMC queue from #3324 needs each
spmcCellto be cache-line aligned, but plainzmalloc()does not guarantee that in all build configurations.This change introduces
zmalloc_cache_aligned()and uses it for the SPMC queue buffer allocation inspmcInit().Failing CI: https://github.com/valkey-io/valkey/actions/runs/24374139344
Worked with Codex!