10 unstable releases (3 breaking)

0.7.0 Nov 26, 2024
0.6.1 Nov 25, 2024
0.5.0 Nov 21, 2024
0.4.7 Jan 14, 2024
0.4.4 Jul 7, 2023

#820 in Data structures

Download history 2/week @ 2024-08-30 13/week @ 2024-09-20 2/week @ 2024-09-27 1/week @ 2024-10-04 1/week @ 2024-11-01 1/week @ 2024-11-08 167/week @ 2024-11-15 595/week @ 2024-11-22 56/week @ 2024-11-29 9/week @ 2024-12-06

827 downloads per month
Used in deep_causality

MIT license

150KB
2K SLoC

๐Ÿ Data structures ๐Ÿ

Crates.io Docs.rs MIT licensed Audit Clippy Tests

High performance SlidingWindow datastructures used in DeepCausality and elsewhere.

RingBuffer is a high-performance, lock-free data structure implementation inspired by the LMAX Disruptor pattern. The RingBuffer supports the following configurations:

  • Single producer / single consumer
  • Single producer / muliple consumer
  • Multi producer / single consumer
  • Multi producer / multi consumer

ArrayGrid is an abstraction over scalars, vectors, and low dimensional matrices similar to a tensor. In contrast to a tensor, an ArrayGrid is limited to low dimensions (1 to 4), only allowing a scalar, vector, or matrix type. Still, all of them are represented as a static fixed-size const generic array. Fixed-sized arrays allow for several compiler optimizations, including a cache-aligned data layout and the removal of runtime array boundary checks because all structural parameters are known upfront, providing a significant performance boost over tensors.

The sliding window implementation over-allocates to trade space (memory) for time complexity by delaying the rewind operation when hitting the end of the underlying data structure. Specifically, a sliding window of size N can hold, without any array copy, approximately C-1 elements, where C is the total capacity defined as NxM with N as the window size and M as a multiple. This crate has two implementations, one over vector and the second over a const generic array. The const generic implementation is significantly faster than the vector-based version.

๐Ÿค” Why?

  1. Zero dependencies.
  2. Zero cost abstraction.
  3. Zero unsafe by default. Unsafe implementations are available through the unsafe feature flag.

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%

More details on performance can be found in the Performance section of the ArrayGrid document.

RingBuffer: Single Producer/Consumer Performance

Batch Size Throughput Latency
1 220.47 Melem/s 4.54 ms
10 1.65 Gelem/s 604.88 ยตs
50 1.67 Gelem/s 597.67 ยตs
100 1.68 Gelem/s 596.12 ยตs

More details on performance can be found in the Performance section of the RingBuffer document.

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

More details on performance can be found in the Performance section of the SlidingWindow document.

๐Ÿš€ Install

Just run:

cargo add dcl_data_structures

๐Ÿ“š Docs

โญ Usage

ArrayGrid:

SlidingWindow:

๐Ÿ™ Prior Art

The project took inspiration from:

๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป Contribution

Contributions are welcomed especially related to documentation, example code, and fixes. If unsure where to start, just 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.

๐Ÿ’ป Author

  • Marvin Hansen.
  • Github GPG key ID: 369D5A0B210D39BC
  • GPG Fingerprint: 4B18 F7B2 04B9 7A72 967E 663E 369D 5A0B 210D 39BC

No runtime deps