-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Writeback counter cell #6190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Writeback counter cell #6190
Conversation
0955c6e
to
6550930
Compare
160a859
to
6c99f68
Compare
9e8650b
to
e354ec0
Compare
6c99f68
to
05c03ce
Compare
e354ec0
to
dd7a5c4
Compare
Co-authored-by: Tim Visée <tim+github@visee.me>
05c03ce
to
0524ba5
Compare
📝 WalkthroughWalkthroughThe changes introduce a new mechanism for accumulating and updating hardware counters. A new structure, ✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
lib/common/common/src/counter/counter_cell.rs (1)
74-98
: Well-documented performance optimization for hot-pathsThe documentation thoroughly explains the purpose, benefits, and use cases for
WritebackCounterCell
. This is a valuable addition that provides clear guidance on when to use (and when not to use) this optimization.One suggestion: consider adding examples in the documentation showing typical usage patterns to guide developers unfamiliar with this pattern.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (10)
lib/common/common/src/counter/counter_cell.rs
(1 hunks)lib/common/common/src/counter/hardware_counter.rs
(2 hunks)lib/segment/src/index/field_index/full_text_index/mutable_inverted_index.rs
(2 hunks)lib/segment/src/index/field_index/geo_index/mutable_geo_index.rs
(2 hunks)lib/segment/src/index/field_index/map_index/mod.rs
(1 hunks)lib/segment/src/index/field_index/map_index/mutable_map_index.rs
(2 hunks)lib/segment/src/index/field_index/mmap_point_to_values.rs
(2 hunks)lib/segment/src/index/field_index/numeric_index/immutable_numeric_index.rs
(2 hunks)lib/segment/src/index/field_index/numeric_index/mmap_numeric_index.rs
(2 hunks)lib/segment/src/index/field_index/numeric_index/mutable_numeric_index.rs
(1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
lib/segment/src/index/field_index/numeric_index/immutable_numeric_index.rs (1)
src/common/metrics.rs (1)
counter
(513-522)
⏰ Context from checks skipped due to timeout of 90000ms (13)
- GitHub Check: test-shard-snapshot-api-s3-minio
- GitHub Check: Basic TLS/HTTPS tests
- GitHub Check: test-snapshot-operations-s3-minio
- GitHub Check: test-low-resources
- GitHub Check: test-consistency
- GitHub Check: test-consensus-compose
- GitHub Check: test (macos-latest)
- GitHub Check: test
- GitHub Check: test (windows-latest)
- GitHub Check: test-consensus
- GitHub Check: test
- GitHub Check: test (ubuntu-latest)
- GitHub Check: test
🔇 Additional comments (30)
lib/common/common/src/counter/hardware_counter.rs (3)
213-216
: Good addition of write-back counter to testsThe write-back counter is now properly tested in the test_hw_counter_drain method. This ensures that when a write-back counter is used and dropped, its increments are properly accumulated in the original counter.
219-219
: Correctly updated assertion for cumulative counter valueGood adjustment of the expected value from 1 to 2, accounting for both the direct increment (line 211) and the write-back counter increment (line 215).
237-240
: Appropriate write-back counter usage in accumulator testEffective demonstration of using write-back counters alongside direct counter increments. This change properly tests the accumulation of both increment methods to ensure they work together correctly.
lib/segment/src/index/field_index/numeric_index/mmap_numeric_index.rs (2)
6-6
: Good addition of required importCorrectly added the import for HwMeasurementIteratorExt which provides the measure_hw_with_cell method used in the updated code.
258-260
: Improved hardware counter measurement approachReplaced the previous inspect-based counter incrementing pattern with a more efficient and cleaner measure_hw_with_cell approach. This change improves code readability and maintainability while following the new counter pattern.
lib/segment/src/index/field_index/mmap_point_to_values.rs (2)
287-295
: Enhanced hardware counter handling with write-back patternGood update to use the write-back counter pattern. The parameter rename from
hw_acc
tohw_counter
improves naming consistency, and using a write-back counter throughwrite_back_counter()
follows the new efficient counter pattern.
303-303
: Correctly updated increment to use write-back counterUpdated the increment call to use the new write-back counter mechanism, ensuring hardware measurements are properly accumulated.
lib/segment/src/index/field_index/numeric_index/immutable_numeric_index.rs (2)
7-7
: Added required HwMeasurementIteratorExt importCorrectly imported the trait that provides the measure_hw_with_cell method used in the updated code.
225-227
: Improved hardware counter measurement in immutable indexReplaced the previous hardware counter incrementing approach with the cleaner measure_hw_with_cell pattern. This change provides a more consistent and efficient way to measure hardware usage across the codebase.
lib/segment/src/index/field_index/geo_index/mutable_geo_index.rs (3)
329-331
: Good use of write-back counter pattern.The implementation correctly obtains a write-back counter from the hardware counter cell, which will accumulate increments locally and write back to the original counter when dropped.
337-337
: Properly measuring geo hash size with write-back counter.The code now directly measures each geo hash's size with the write-back counter, which is more efficient than accumulating counts in a local variable.
351-351
: Efficiently measuring total point offset size.The implementation correctly calculates the total memory size for all point offsets at once, which is an efficient approach.
lib/segment/src/index/field_index/numeric_index/mutable_numeric_index.rs (2)
265-268
: Good use of write-back counter pattern.The implementation correctly obtains a write-back counter from the hardware counter cell, which accumulates increments locally and writes back to the original counter when dropped.
272-273
: Correctly measuring key and index size in each iteration.The code now directly increments the write-back counter with both the key and index sizes in each loop iteration, eliminating the need for a local counter variable.
lib/segment/src/index/field_index/full_text_index/mutable_inverted_index.rs (4)
86-88
: Good use of write-back counter pattern.The implementation correctly obtains a write-back counter from the hardware counter cell, which will accumulate increments locally and write back to the original counter when dropped.
94-95
: Accurately measuring Document overhead.The code now uses the write-back counter to track the memory overhead of expanding the
point_to_docs
vector, with a clear comment explaining that it's only measuring the Document's overhead.
105-106
: Properly measuring postings expansion size.The write-back counter correctly measures the size difference when the postings vector needs to be expanded.
118-118
: Accurately tracking point ID size.The implementation correctly measures the size of each point ID with the write-back counter.
lib/segment/src/index/field_index/map_index/mod.rs (3)
506-508
: Good use of write-back counter pattern.The implementation correctly obtains a write-back counter from the hardware counter cell, accumulating increments locally and writing back to the original counter when dropped.
515-515
: Correctly measuring unique value size.The write-back counter now tracks the size of each unique value added to the index, which is more direct than using a local counter variable.
518-518
: Properly measuring point ID size.The implementation correctly measures the size of each point ID with the write-back counter.
lib/common/common/src/counter/counter_cell.rs (4)
66-72
: New method provides performance-optimized countingThe
write_back_counter
method creates a newWritebackCounterCell
that accumulates increments and writes back to the original counter only when dropped. This is a good design for optimizing performance in hot-paths and loops.
100-105
: Drop implementation ensures proper counter updatesThe
drop
implementation correctly writes the accumulated count back to the original cell, which is the core mechanism that makes this optimization work without requiring manual reconciliation.
107-117
: Simple and efficient WritebackCounterCell implementationThe implementation is clean and focused. The
incr_delta
method performs a simple addition without any cell access, which is exactly what's needed for the performance optimization.
119-134
: Test validates core functionality but can be expandedThe test correctly verifies that the counter value is written back on drop. However, based on an earlier review comment, adding an additional assertion after incrementing but before dropping would better demonstrate the behavior.
Consider enhancing the test to include additional assertions and test cases:
#[test] fn test_write_back_counter() { let cell = CounterCell::new(); { let mut wb_counter = cell.write_back_counter(); wb_counter.incr_delta(4); assert_eq!(cell.get(), 0); + // Multiple increments + wb_counter.incr_delta(2); + assert_eq!(cell.get(), 0); } assert_eq!(cell.get(), 6); + + // Test nested scopes + { + let mut wb_counter1 = cell.write_back_counter(); + wb_counter1.incr_delta(10); + { + let mut wb_counter2 = cell.write_back_counter(); + wb_counter2.incr_delta(4); + assert_eq!(cell.get(), 6); + } + assert_eq!(cell.get(), 10); + } + assert_eq!(cell.get(), 20); }lib/segment/src/index/field_index/map_index/mutable_map_index.rs (5)
61-64
: Adopting optimized counter for write operationsThe change replaces direct counter increments with a write-back counter for better performance in this hot path. Good optimization for write-intensive operations.
71-71
: Performance-optimized counter increment replaces direct callThe direct increment to the write-back counter eliminates cell access overhead during the loop, which is a good optimization for performance-critical code.
138-141
: Adopting optimized counter for read operationsUsing the write-back counter for read operations matches the pattern used in write operations, maintaining consistency and providing the same performance benefits.
142-143
: Improved code documentationThe added comment clarifies the purpose of the overhead increment. This helps maintain code clarity while transitioning to the write-back counter pattern.
151-151
: Consistent use of write-back counterThe counter increment inside the loop uses the write-back counter consistently with the rest of the changes, providing performance benefits for iterative operations.
* Add WritebackCounterCell * Replace custom counting with WritebackCounterCell * Update lib/common/common/src/counter/counter_cell.rs Co-authored-by: Tim Visée <tim+github@visee.me> --------- Co-authored-by: Tim Visée <tim+github@visee.me>
Depends on #6137
Introduces a new type that counts inside a
usize
and writes the results into aCounterCell
on drop.This reduces potential errors when manually counting inside a hotpath and applying measurements afterwards.
This PR can be reviewed on per commit basis.