Coder Social home page Coder Social logo

mmap-vec's Introduction

Rust memory mapped vector

CI Status codecov docs.rs Crates.io LICENSE MSRV dependency status

This crate contains implementation / helper to create data struct that are memory mapped.

Sometime, you have to deal with vector / data that cannot fit in memory. Moving them to disk and memory map them is a good way to deal with this problem.

How to use it ?

That is so simple !

use mmap_vec::MmapVec;

#[derive(Debug, PartialEq, Clone, Copy)]
struct Row {
    id: usize,
    age: u8,
}

let row1 = Row { id: 42, age: 18 };
let row2 = Row { id: 894, age: 99 };

// Create a memory mapped vec ๐Ÿ˜Ž
let mut v = MmapVec::<Row>::new();

// Push can trigger new mmap segment creation, so it can fail.
v.push(row1).unwrap();
v.push(row2).unwrap();

// Check the content
assert_eq!(v[0], row1);
assert_eq!(&v[..], &[row1, row2]);

// Pop content
assert_eq!(v.pop(), Some(row2));
assert_eq!(v.pop(), Some(row1));

Check the unit tests for more example.

How it works ?

The main idea here is to provide a basic struct Segment.

This struct provides constant size memory mapped array of type T. Wrapping Segment into a new struct MmapVec that handle segment growth / shrink does the trick.

Where does the segment are store on disk ?

For now data are stored in .cache (if using 'cache-dirs' feature) or /tmp under a dedicated folder.

UUID V4 are generated in order to avoid collision when creating segment.

โฏ ls /tmp/mmap-vec-rs -1
/tmp/mmap-vec-rs/00d977bf-b556-475e-8de5-d35e7baaa39d.seg
/tmp/mmap-vec-rs/6cb81228-9cf3-4918-a3ef-863907b32830.seg
/tmp/mmap-vec-rs/8a86eeaa-1fa8-4535-9e23-6c59e0c9c376.seg
/tmp/mmap-vec-rs/de62bde3-6524-4c4b-b514-24f6a44d6323.seg

Does segment creation is configurable ?

Yes ! Check out test_custom_segment_creator::test_custom_segment_builder for example.

Since segment creation are manage through a trait. You are free to configure it the way you want.

Does this work on Windows ?

Nope. I am not targeting this OS and would like to keep this crate as simple as possible.

I also would like to reduce dependencies as much as possible.

โฏ cargo tree
mmap-vec v0.1.1
โ”œโ”€โ”€ libc v0.2.147
โ”œโ”€โ”€ uuid v1.4.1
|   โ””โ”€โ”€ getrandom v0.2.10
|       โ”œโ”€โ”€ cfg-if v1.0.0
|       โ””โ”€โ”€ libc v0.2.147
# Optional using 'cache-dir' feature
โ”œโ”€โ”€ dirs v5.0.1
โ”‚   โ””โ”€โ”€ dirs-sys v0.4.1
โ”‚       โ”œโ”€โ”€ libc v0.2.147
โ”‚       โ””โ”€โ”€ option-ext v0.2.0
[dev-dependencies]
โ””โ”€โ”€ glob v0.3.1

Is this crate production ready ?

Yes ๐Ÿ˜ ! Since v0.1.1. But feature are a little bit limited for now ...

Github PR to help on this are welcomed !

Prefetching API is not fully stable for now and may change in the future.

Ideas / new features ?

  • Implement custom std::alloc::Allocator to use with std::vec::Vec

License: MIT

mmap-vec's People

Contributors

arthurlm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.