numinnex commented on code in PR #2549:
URL: https://github.com/apache/iggy/pull/2549#discussion_r2675318434


##########
core/common/src/collections/segmented_slab.rs:
##########
@@ -0,0 +1,601 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Concurrent segmented slab with structural sharing for RCU patterns.
+//!
+//! # Design Goals
+//!
+//! - O(1) access time
+//! - Structural sharing via Arc-wrapped segments (cheap clones)
+//! - Lock-free reads with copy-on-write modifications
+//! - Slab-assigned keys with slot reuse via free list
+//!
+//! # Architecture
+//!
+//! Data is divided into fixed-size segments, each backed by a `Slab<T>`.
+//! Keys are encoded as: `global_key = (segment_idx << SEGMENT_BITS) | 
local_key`
+//!
+//! Slab handles ID assignment internally:
+//! - `insert(value)` returns the assigned key
+//! - Removed slots are reused via Slab's free list
+//! - No external ID generation needed
+//!
+//! # Structural Sharing
+//!
+//! Each segment is wrapped in `Arc`. On modification:
+//! - `Arc::make_mut` clones only if segment is shared
+//! - Unmodified segments remain shared across snapshots
+//! - Clone cost is O(num_segments), not O(num_entries)
+
+use slab::Slab;
+use std::sync::Arc;
+
+/// Bits for local index within segment (2^10 = 1024 entries per segment)
+const SEGMENT_BITS: usize = 10;
+
+/// Mask to extract local index: key & SEGMENT_MASK = local_key
+const SEGMENT_MASK: usize = (1 << SEGMENT_BITS) - 1;
+
+/// Maximum entries per segment (must not exceed this to avoid key overlap)
+const SEGMENT_CAPACITY: usize = 1 << SEGMENT_BITS;
+
+/// Concurrent segmented slab with structural sharing.
+///
+/// # Performance Characteristics
+///
+/// | Operation | Time Complexity | Notes |
+/// |-----------|-----------------|-------|
+/// | get       | O(1)            | Direct segment + slab indexing |
+/// | insert    | O(1) amortized  | May clone segment if shared |
+/// | remove    | O(1) amortized  | May clone segment if shared |
+/// | clone     | O(num_segments) | Just Arc clones, not data copies |
+///
+/// # Key Encoding
+///
+/// Keys are `usize` where:
+/// - High bits `(key >> SEGMENT_BITS)` = segment index
+/// - Low bits `(key & SEGMENT_MASK)` = local slab key
+#[derive(Clone)]
+pub struct SegmentedSlab<T> {

Review Comment:
   use const generics for `SEGMENT_CAPACITY`, if the math for figuring out the 
`SEGMENT_BITS` is too hard, fallback to this formula - let (segment_index, 
index) = (key / segments.count()  ,key % segments.count() ). Alternatively if 
the math for bits is feasible you can create an `const: () = { assert 
SEGMENT_CAPACITY is power of 2}` block inside of the constructor to make sure 
that the capacity is always power of 2



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to