Skip to content

Isolate I/O thread written client fields on a dedicated cache line#4176

Open
abokhalill wants to merge 3 commits into
valkey-io:unstablefrom
abokhalill:client-io-line-unstable
Open

Isolate I/O thread written client fields on a dedicated cache line#4176
abokhalill wants to merge 3 commits into
valkey-io:unstablefrom
abokhalill:client-io-line-unstable

Conversation

@abokhalill

Copy link
Copy Markdown

The reply append path explicitly supports appending while an I/O thread streams the same client, so during every in flight write job, the main thread updates net_output_bytes_curr_cmd while the owning I/O thread updates io_tracked_reply_len, nwritten and io_write_state a few bytes away on the same cache lines. Group every I/O thread written field on one _Alignas(CACHE_LINE_SIZE) line of its own, in the same spirit as #1179 did for used_memory_thread.

Signed off by: Yousef Ahmed yosefkhalil610@gmail.com

The reply append path explicitly supports appending while an I/O thread
streams the same client (io_write_state guards in _addReplyProtoToList),
so during every in-flight write job the main thread updates
net_output_bytes_curr_cmd while the owning I/O thread updates
io_tracked_reply_len, nwritten and io_write_state a few bytes away on
the same cache lines. Group every I/O-thread-written field on one
_Alignas(CACHE_LINE_SIZE) line of its own, in the same spirit as valkey-io#1179
did for used_memory_thread, and allocate clients cache line aligned so
the alignment holds. Costs 64 bytes per client (576 -> 640).

Signed-off-by: Yousef Ahmed <yosefkhalil610@gmail.com>
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 49cf1005-a760-46c1-88e6-ce354dbab3bb

📥 Commits

Reviewing files that changed from the base of the PR and between 3a10362 and 17d513f.

📒 Files selected for processing (1)
  • src/unit/test_networking.cpp
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/unit/test_networking.cpp

📝 Walkthrough

Walkthrough

The client structure now groups I/O-thread-owned fields into cache-line-aligned storage. Production and networking test client allocations use cache-line-aligned memory through updated allocation paths and a shared test helper.

Changes

Client cache alignment

Layer / File(s) Summary
Aligned client structure layout
src/server.h
The client layout reorganizes response and accounting fields, groups I/O-thread state and counters in an aligned region, moves io_last_written, and aligns client_list_node.
Aligned client allocation and test setup
src/networking.c, src/unit/test_networking.cpp
createClient() and networking tests use cache-aligned client allocation, with tests routed through a shared zero-initializing helper.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change of isolating I/O-thread-written client fields on a dedicated cache line.
Description check ✅ Passed The description is directly related to the cache-line layout change and the aligned test-client allocation updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/server.h`:
- Around line 1379-1395: Update the client allocations in the networking unit
tests at the referenced construction sites to use
zmalloc_cache_aligned(sizeof(client)) instead of zmalloc/zcalloc. Ensure every
allocation of client objects is cache-line aligned to satisfy the
_Alignas(CACHE_LINE_SIZE) fields in the client definition, while preserving
existing initialization behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 298c617d-649d-4c4a-ac15-36bc3bb5407a

📥 Commits

Reviewing files that changed from the base of the PR and between 5c1fc15 and d14ee6e.

📒 Files selected for processing (2)
  • src/networking.c
  • src/server.h

Comment thread src/server.h
client now carries _Alignas(CACHE_LINE_SIZE) fields, so the plain
zmalloc/zcalloc in the networking unit tests under-align it. Route all
test client allocations through one aligned, zeroing helper.

Signed-off-by: Yousef Ahmed <yosefkhalil610@gmail.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/unit/test_networking.cpp`:
- Around line 140-146: Separate allocation from initialization in
src/unit/test_networking.cpp: rename the cache-aligned helper at lines 140-146
to a distinct allocator symbol, then update the initializing createTestClient()
at lines 536-537 to call that allocator instead of recursively calling itself;
preserve the existing client initialization behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 426fa410-93f0-4eb2-9d46-7384e995a6a3

📥 Commits

Reviewing files that changed from the base of the PR and between d14ee6e and 3a10362.

📒 Files selected for processing (1)
  • src/unit/test_networking.cpp

Comment thread src/unit/test_networking.cpp
The aligned allocation helper collided with the existing initializing
createTestClient() and rewrote its allocation into a self-call. Name
the raw allocator allocTestClient() and have createTestClient() and
the previously raw allocation sites use it.

Signed-off-by: Yousef Ahmed <yosefkhalil610@gmail.com>
@abokhalill

Copy link
Copy Markdown
Author

hey @madolson, would appreciate you taking a look at this once you have a cycle

@abokhalill abokhalill removed their assignment Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant