🏁 Data structures 🏁
This crate provides two data structures used in DeepCausality: ArrayGrid and SlidingWindow.
ArrayGrid abstracts over scalars, vectors, and low-dimensional matrices, similar to a tensor but limited to 1 to 4 dimensions. Every ArrayGrid is a static, fixed-size const generic array. Because all structural parameters are known at compile time, the compiler can lay the data out cache-aligned, which makes ArrayGrid faster than a tensor.
The sliding window over-allocates, trading memory for time by delaying the rewind when it reaches the end of the underlying storage. A sliding window of size N holds approximately C-1 elements without any array copy, where the capacity C is NxM with N the window size and M a multiple. The crate implements the window over a vector and over a const generic array; the const generic version is significantly faster.
Why?
- Zero cost abstraction.
- Zero unsafe.
- Zero macros.
- Zero external dependencies.
Performance:
ArrayGrid
Set value:
| Dimension | Safe Implementation | Unsafe Implementation | Improvement |
|---|---|---|---|
| 1D Grid | 604.71 ps | 271.38 ps | 55.1% |
| 2D Grid | 581.33 ps | 417.39 ps | 28.2% |
| 3D Grid | 862.16 ps | 577.04 ps | 33.0% |
| 4D Grid | 1.137 ns | 812.62 ps | 28.5% |
See the Performance section of the ArrayGrid document for details.
Sliding Window
Single Push:
| Implementation | Single Push Time | Notes |
|---|---|---|
| ArrayStorage | ~2.08ns | Optimized for continuous access patterns |
| VectorStorage | ~2.5ns | Good for dynamic sizing |
| UnsafeVectorStorage | ~2.3ns | Better performance than safe vector |
| UnsafeArrayStorage | ~1.9ns | Best performance for sequential and batch operations |
Sequential Operations:
| Implementation | Operation Time | Notes |
|---|---|---|
| UnsafeArrayStorage | ~550ps | Best cache utilization |
| ArrayStorage | ~605ps | Excellent cache locality |
| UnsafeVectorStorage | ~750ps | Good for mixed workloads |
| VectorStorage | ~850ps | Most predictable |
See the Performance section of the SlidingWindow document for details.
Install
Run:
Build configurations
| Configuration | Cargo features | Supported |
|---|---|---|
std, hosted |
default | ✓ |
no-std with a heap |
default-features = false, features = ["no-std", "alloc"] |
✓ |
no-std on core alone, no heap |
default-features = false, features = ["no-std"] |
✓ |
With no-std alone the crate needs only core and links into a program that defines no #[global_allocator]; alloc adds the parts that need a heap. VectorStorage, window_type::new_with_vector_storage and the vec() accessor need alloc.
CI builds every supported bare-metal configuration for thumbv7em-none-eabihf and links the core configuration without an allocator.
README_NO_STD.md describes the build configuration of every crate in the workspace.
Configuration
The crate builds without the standard library, on core alone.
default: Enablesstd.std: Uses the standard library (includesalloc).no-std: Builds oncorealone; no#[global_allocator]is required.ArrayGrid,ArrayStorageandSlidingWindowover it are available.alloc: Adds theVec-backed parts:VectorStorage,new_with_vector_storageand thevec()accessor. Select withdefault-features = false, features = ["no-std", "alloc"].
Window accessors report an empty or unfilled window as a &'static str message.
Docs
Usage
ArrayGrid:
-
SlidingWindow:
Prior Art
The project took inspiration from:
Contribution
Contributions are welcome, especially documentation, example code, and fixes. If unsure where to start, open an issue and ask.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in deep_causality by you, shall be licensed under the MIT licence, without any additional terms or conditions.
Licence
This project is licensed under the MIT license.
Security
For details about security, please read the security policy.