Skip to content

Fix false sharing between the main thread and io-threads in spscQueue - #5761

Open
zhulipeng wants to merge 1 commit into
valkey-io:unstablefrom
zhulipeng:perf/spsc-producer-line
Open

zhulipeng wants to merge 1 commit into
valkey-io:unstablefrom
zhulipeng:perf/spsc-producer-line

Conversation

@zhulipeng

Copy link
Copy Markdown
Member

Description

In spscQueue, the producer-private tail_local and head_cache share a cache line with tail, which the io-thread polls in spscDequeueBatch() while its queue is empty. So every enqueue from the main thread (e.g. tryOffloadFreeArgvToIOThreads(), once per command) has to take the line back from the io-thread first, even for the batched commit = false enqueues.

This patch moves tail_local and head_cache to their own cache line. No logic change.

perf c2c (io-threads 16, get-10B-pipeline-100), the producer cache line before this patch:

  Offset  LclHitm  Store  Symbol                          Source:Line
     0x0  100.00%   0.00%  spscDequeueBatch                queues.c:269   (io-thread, reads tail)
     0x8    0.00% 100.00%  tryOffloadFreeArgvToIOThreads   queues.c:251   (main, writes tail_local)

On the main thread, tryOffloadFreeArgvToIOThreads drops from 21.7% to 1.9% of cycles.

Performance Boost

Didn't run a full list, just picked the benchmarks hitting tryOffloadFreeArgvToIOThreads with the main thread as the bottleneck (pipelined).

Benchmark (io-threads 16) Performance Boost
memtier_benchmark-1Mkeys-string-get-10B-pipeline-100 58%
memtier_benchmark-1Mkeys-string-get-10B-pipeline-10 23%
memtier_benchmark-1Mkeys-string-incr-pipeline-10 22%
memtier_benchmark-1Mkeys-string-setex-100B-pipeline-10 15%

Test Env

  • Intel Xeon 6960P, Ubuntu 26.04; server and client on the same socket, different NUMA nodes
  • Server: 16 cores, port 9001, bind * -::*, protected-mode no, save "", io-threads 16
  • Median of 3 interleaved runs (first run dropped)

spscQueue placed tail_local and head_cache on the same cache line as
tail. The consumer (an I/O thread) reads tail on every poll while its
queue is empty, so the line is usually shared with the I/O thread's
core when the main thread comes to enqueue. spscIsFull() and
spscEnqueue() read and write tail_local, so every batched enqueue,
including the uncommitted ones in tryOffloadFreeArgvToIOThreads(),
first had to take the line back. That defeats the point of batching
the tail publication.

Move tail_local and head_cache to their own cache line. The consumer
still only touches head, tail_cache, tail and the buffer, and the
producer now only touches the shared line when it commits. The struct
grows by one cache line per queue.

Signed-off-by: Lipeng Zhu <lipeng.zhu@intel.com>
Co-authored-by: Wangyang Guo <wangyang.guo@intel.com>
@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The queue layout now places the shared, consumer-visible tail and the producer’s private tail_local write index on separate cache lines. The fields and public function declarations remain unchanged.

Changes: Queue field layout

Layer / File(s) Summary
Separate queue tail indices
src/queues.h
The tail comment identifies it as published to the consumer. tail_local is identified as the producer’s private write index and is separated onto another cache line.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Suggested reviewers: akashkgit

Merge Risk: Minimal · up to 2d0a7

This change only rearranges fields in the queue struct to reduce cache-line contention between the main thread and I/O threads. It does not change behavior, and no merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: fixing false sharing between the main thread and io-threads in spscQueue. The raw summary says the shared tail moves to its own producer cache line and tail_local move…
Description check Passed The description explains the problem: tail_local and head_cache share a cache line with tail, which the io-thread polls. It describes the fix of moving tail_local and head_cache to their own cache lin…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: fixing false sharing between the main thread and io-threads in spscQueue. The raw summary says the shared tail moves to its own producer cache line and tail_local moves to a separate line, which matches the title's aim. The title is specific and concise.

Full details: Description check

Explanation

The description explains the problem: tail_local and head_cache share a cache line with tail, which the io-thread polls. It describes the fix of moving tail_local and head_cache to their own cache line, and it includes perf c2c data and benchmark results. This is directly related to the changeset.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ab1cd11d-5e48-4d6b-97a8-18098cfdd181
Commits

Reviewing files that changed from the base of the PR and between 82fdc03 and 2d0a762.

Files selected for processing: 1

  • src/queues.h

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@valkey-review-bot

Copy link
Copy Markdown
Contributor

Review state

Ready for human review.
No findings. Moving tail_local and head_cache to their own cache line matches how the queue is used: all producer calls on io_private_inbox come from the main thread, and spscDequeueBatch reads only head, tail_cache, tail and the buffer. I did not build the tree or reproduce the benchmark numbers.

Risk: Low. Struct layout change only in spscQueue, with no logic change. If it were wrong the cost is a perf regression on the io-threads free-argv offload path and 1 extra cache line per queue, not a correctness problem.

Open findings: none.

Backport: Not needed. Performance improvement to struct layout, not a bug fix. The same layout exists on 9.1 and 9.2 (src/queues.h:119) if maintainers want the speedup there.

Updated for 2d0a762 at 2026-12-05 11:15 UTC. The bot rewrites this comment on every review.

@codecov

codecov Bot commented Dec 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.78%. Comparing base (82fdc03) to head (2d0a762).

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #5761      +/-   ##
============================================
- Coverage     81.80%   81.78%   -0.02%     
============================================
  Files           202      202              
  Lines        106278   106278              
============================================
- Hits          86941    86921      -20     
- Misses        19337    19357      +20     

see 27 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@quanyeyang quanyeyang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and impressive. Keeping tail_local off the cache line the I/O thread polls makes sense. Thanks for including the perf c2c data! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants