columned 0.2.0

A single, contiguous, allocation for multiple arrays
Documentation

Columned

Crates.io Docs.rs MIT licensed

A single, contiguous, allocation for multiple arrays. Meant to allocate multiple arrays, that live the same lifetimes. This reduces multiple allocations, to a single one. This crate may facilitates the implementation of columnar data structures.

Example

use columned::{Allocate, with_allocation};

fn main() {
    let xs: Allocate<u64, _> = unsafe {
        Allocate::alloc(10, |xs| {
            for (i, x) in xs.iter_mut().enumerate() {
                x.write(i as u64);
            }
        })
    };
    let ys: Allocate<u64, _> = unsafe {
        Allocate::alloc(10, |ys| {
            for (i, y) in ys.iter_mut().enumerate() {
                y.write(i as u64);
            }
        })
    };
    let sums: Allocate<u64, _> = unsafe {
        Allocate::alloc(10, |sums| {
            for sum in sums.iter_mut() {
                sum.write(0);
            }
        })
    };

    with_allocation((xs, ys, sums), |(xs, ys, sums)| {
        for ((sum, x), y) in sums.iter_mut().zip(xs.iter()).zip(ys.iter()) {
            *sum = x + y;
        }

        for (i, sum) in sums.iter().enumerate() {
            assert_eq!(*sum, 2 * i as u64);
        }
    })
    .unwrap();
}

Working Principle

Columned manages a contiguous allocation of memory. Each Coulmn have a pointer to this contiguous allocation. The following figure illustrates the working principle.

       Columned
       +--------+--------+
       | 0x0123 |   ...  |
       +--------+--------+
        ptr
         |
         V
Heap   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
       |           0.1 |           3.2 |     5 |     7 |    20 |     6 |
       +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
         ^                               ^
         |                               |
        ptr      len                    ptr      len
       +--------+--------+             +--------+--------+
       | 0x0123 |      2 |             | 0x012b |      4 |
       +--------+--------+             +--------+--------+
       Column<f32>                     Column<u16>

This also means that the user has to ensure that Columned outlives the Columns that uses its managed memory.