Files
Library/docs/roadmap/modules/Containers.md

156 lines
10 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Containers completion plan
Status: planning only; every feature task is **PLANNED**. Baseline:
`f1f9ba375b22abf1adcc7c550ec21ac20895c0a4`, inspected 2026-11-24.
## Existing structures and gaps
[The module](../../../src/Containers/module.cmake) declares no dependencies. It already
contains [fixed bitsets](../../../include/Void/Containers/Bitset.hpp),
[intervals](../../../include/Void/Containers/Interval.hpp),
[disjoint sets](../../../include/Void/Containers/DisjointSet.hpp),
[a directed graph](../../../include/Void/Containers/Graph.hpp),
[a trie](../../../include/Void/Containers/Trie.hpp),
[an indexed priority queue](../../../include/Void/Containers/PriorityQueue.hpp),
[LRU](../../../include/Void/Containers/LRU.hpp),
[TTL/policy cache](../../../include/Void/Containers/Cache.hpp),
[object pools](../../../include/Void/Containers/Pool.hpp),
[buffer recycling](../../../include/Void/Containers/BufferPool.hpp),
[rings](../../../include/Void/Containers/Ring.hpp), and
[debug rendering](../../../include/Void/Containers/Debug.hpp).
Namespaces follow header families (`Void::Ring`, `Void::Cache`, etc.); a unified
`Void::Containers` naming migration must not pretend they already share one namespace.
`SpscRing<T,N>` exposes N−1 usable entries. `MpscRing` reserves a head position before
construction and spins waiting for readiness. That is not a general wait-free guarantee:
a stalled producer can stall the consumer, and throwing construction needs a defined
reservation-repair contract. Buffer leases store a raw pool pointer; moving/destroying
the pool with outstanding leases needs a stronger lifetime rule. Cache already accepts
an injected steady-clock callable and supports LRU/LFU/FIFO with lazy TTL expiry.
ObjectPool and FixedPool return deleters tied to their pool owner. These are implementation
constraints to repair, not proof of production readiness.
[Existing standalone suites](../../../tests/standalone/Containers) cover each header;
[ring](../../../tests/standalone/Containers/Ring.cpp),
[pool](../../../tests/standalone/Containers/Pool.cpp),
[buffer-pool](../../../tests/standalone/Containers/BufferPool.cpp), and
[cache](../../../tests/standalone/Containers/Cache.cpp) are regression starting points.
Graph coverage marked PriorityQueue line 23 partial; the constructor was read directly.
## Target ownership and backend choice
Under [C-01/C-04/C-05](../ARCHITECTURE.md), [Memory](../subsystems/Memory.md) owns raw
allocation resources; Containers owns typed lifetime, handles and data structures.
[Algorithms](../subsystems/Algorithms.md) owns reusable graph/range algorithms and
imports no Containers. Containers graph convenience members may delegate downward.
Standard vector/map/unordered_map/list/heap algorithms remain implementation building
blocks. Do not build a general hash-table or tree replacement without a named workload
and benchmark gate. Native specializations address bounded admission, stable identities
and inline storage that current module users actually need.
## NEW types and selected algorithms
Illustrative NEW API in NEW `include/Void/Containers/StaticVector.hpp` and
NEW `include/Void/Containers/SlotMap.hpp`:
```cpp
namespace Void::Containers {
enum class InsertError { full, allocation_failed, generation_exhausted };
template<class T, std::size_t N> class StaticVector {
public:
template<class... A> std::expected<std::reference_wrapper<T>, InsertError>
try_emplace_back(A&&...);
std::span<T> values() noexcept;
};
struct SlotHandle { std::uint32_t slot; std::uint32_t generation; };
template<class T> class SlotMap {
public:
template<class... A> std::expected<SlotHandle, InsertError> emplace(A&&...);
T* get(SlotHandle) noexcept;
bool erase(SlotHandle);
};
}
```
StaticVector uses aligned uninitialized inline storage and constructs exactly `size()`
objects. Capacity exhaustion leaves it unchanged. Throwing constructors preserve prior
elements and propagate unless an explicit exception adapter is used. Moving an inline
container invalidates element pointers. This supports small request metadata and
dependency lists without forcing a heap allocation.
SlotMap starts with segmented slots (256 per block), a free-index list and generation
checks. It preserves addresses of other live elements on growth, useful for graph and
registry identities, but is not itself a service handle or ownership pin. Erase increments
generation; retire a slot permanently on generation exhaustion to prevent ABA reuse.
Default maximum: 65,536 live slots; include retired slots and block metadata in the byte
budget. No pointer remains valid after its object's erase or container destruction.
SmallVector is a later NEW inline-then-heap specialization (default inline capacity 8)
only after StaticVector and allocator exception safety pass. Growth defaults to at most
4,096 elements; allow an explicit larger bound. Contiguous relocation invalidates
references. Compare with `std::vector` plus reserve for request-header and configuration
workloads before making it an internal default.
Keep SPSC admission nonblocking and reject full. Require nonthrowing transfer types for
the NEW concurrent ring APIs and explicitly quiesced destruction. Replace the MPSC spin
path with per-slot sequence publication and a `try_pop` that returns empty/pending/closed
without waiting for a reserved but unpublished slot. Ordered consumption may return
pending behind a stalled producer; no unconditional lock-free progress claim is made.
Async owns waits, wakeups, cancellation and queue shutdown adapters. Rings own no threads.
Pool control blocks remain stationary; owning leases retain shared pool state through
their final return, while explicitly borrowed pools require lease-before-owner
destruction. Return paths allocate nothing and never throw; exhausted retention frees to
the upstream resource. Default buffer limits: 1 MiB/request, 16 MiB idle bytes and 64 MiB
total checked-out plus idle bytes. Size-class rounding is checked before allocation.
Object-pool admission defaults to 4,096 live objects with explicit constructor limits.
## Existing capability completion
Cache defaults stay explicit-capacity; the NEW convenience constructor selects 1,024
entries and a 16 MiB weight budget. Caller-supplied weight functions account for payloads;
no pretend exact RSS bound for arbitrary `T`. TTL uses Time's clock contract, with
zero-duration meaning immediate expiry. Eviction/expiry callbacks execute after mutation
and outside any adapter lock. LFU tie-breaking becomes deterministic by age and its
linear-scan cost is documented until workload evidence justifies a different structure.
Trie adds iterative traversal, prefix result limits (256 default), and a 4 KiB key limit.
Graph traversal takes explicit vertex/edge budgets (65,536/262,144 default). Priority
updates preserve heap/index consistency if key copying/allocation fails; comparison must
be stable and nonthrowing for its bounded fast path. Interval endpoints remain half-open;
closed-to-half-open conversion checks maximum-endpoint overflow. Bitset zero-size and
tail masking receive explicit tests. Debug formatting truncates at 256 entries/16 KiB.
## Ordered implementation tasks
| Task | Phase; dependencies | Scope and acceptance | Recommended role |
| --- | --- | --- | --- |
| M-CONTAINERS-01 | P0; current source | Document capacity, invalidation, thread and exception guarantees for every current structure; preserve deterministic fixtures. | sol_implementer |
| M-CONTAINERS-02 | P1; Memory bounded-resource gate; M-CONTAINERS-01 | Repair object/buffer lease lifetime and byte accounting; owner destruction with retained leases is safe and recycle allocates zero. | astra_implementer |
| M-CONTAINERS-03 | P1; M-CONTAINERS-01 | Implement nonthrowing ring publication and nonspinning MPSC poll semantics; delayed producer, wraparound and quiesced destruction tests pass under sanitizers/TSAN. | astra_implementer |
| M-CONTAINERS-04 | P2; M-CONTAINERS-02 | Add StaticVector and SlotMap; capacity failure, over-alignment, constructor exceptions and stale handles leave invariants intact. | sol_implementer |
| M-CONTAINERS-05 | P2; M-CONTAINERS-01; Algorithms graph gate; Time clock gate | Complete bounded graph/trie/priority/interval/bitset/cache APIs; deterministic fake-clock eviction and failure injection match reference models. | sol_implementer |
| M-CONTAINERS-06 | P3; M-CONTAINERS-04; measured request-metadata profile | Add bounded SmallVector and compare reserved std::vector; publish allocation, relocation and compile-cost evidence before adoption. | sol_implementer |
| M-CONTAINERS-07 | P3; M-CONTAINERS-02–06 | Ship compatibility aliases, independent installed consumers and workload report; no unexplained dependency or progress-guarantee regression. | sol_implementer |
## Validation and transition
CURRENT checks are the inspected suites, not new executions. PROPOSED model/property
tests replay randomized operations against standard containers, force allocation and
construction failure at each insertion step, and verify destructor counts. TSAN plus
scheduled producer pauses exercise concurrency; TSAN alone cannot establish progress.
Bounded polling tests must terminate without a producer resuming. Retained pool leases,
moved containers, generation wrap and zero/maximum interval endpoints get dedicated cases.
Measure mixed cache traces, bursty ring occupancy, pool churn, prefix scans and 0–32-item
metadata under [PERFORMANCE](../PERFORMANCE.md): p50/p95/p99, throughput, live/idle bytes,
allocations, peak RSS/CPU and build cost. A lock-free label is not an acceptance metric.
The [validation](../VALIDATION.md), [dependency](../DEPENDENCIES.md) and
[delivery](../DELIVERY.md) gates precede integration into Runtime or Net.
[Migration](../MIGRATION.md) preserves old namespaces through deprecated aliases while
new bounded constructors and poll results require explicit caller changes. Move raw
allocation implementation to Memory behind old pool wrappers only after lifetime
contracts are tested; do not create Memory→Containers. Rollback uses the old major and
adapters without persistent state conversion. Never interchange in-memory SlotHandle
values with serialized IDs. [Requirements](../REQUIREMENTS.md) define package scope.