• Stars
    star
    611
  • Rank 73,401 (Top 2 %)
  • Language
    Rust
  • License
    Mozilla Public Li...
  • Created about 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A safe arena allocator that allows deletion without suffering from the ABA problem by using generational indices.

generational-arena

A safe arena allocator that allows deletion without suffering from the ABA problem by using generational indices.

Inspired by Catherine West's closing keynote at RustConf 2018, where these ideas (and many more!) were presented in the context of an Entity-Component-System for games programming.

What? Why?

Imagine you are working with a graph and you want to add and delete individual nodes at a time, or you are writing a game and its world consists of many inter-referencing objects with dynamic lifetimes that depend on user input. These are situations where matching Rust's ownership and lifetime rules can get tricky.

It doesn't make sense to use shared ownership with interior mutability (i.e. Rc<RefCell<T>> or Arc<Mutex<T>>) nor borrowed references (ie &'a T or &'a mut T) for structures. The cycles rule out reference counted types, and the required shared mutability rules out borrows. Furthermore, lifetimes are dynamic and don't follow the borrowed-data-outlives-the-borrower discipline.

In these situations, it is tempting to store objects in a Vec<T> and have them reference each other via their indices. No more borrow checker or ownership problems! Often, this solution is good enough.

However, now we can't delete individual items from that Vec<T> when we no longer need them, because we end up either

  • messing up the indices of every element that follows the deleted one, or

  • suffering from the ABA problem. To elaborate further, if we tried to replace the Vec<T> with a Vec<Option<T>>, and delete an element by setting it to None, then we create the possibility for this buggy sequence:

    • obj1 references obj2 at index i

    • someone else deletes obj2 from index i, setting that element to None

    • a third thing allocates obj3, which ends up at index i, because the element at that index is None and therefore available for allocation

    • obj1 attempts to get obj2 at index i, but incorrectly is given obj3, when instead the get should fail.

By introducing a monotonically increasing generation counter to the collection, associating each element in the collection with the generation when it was inserted, and getting elements from the collection with the pair of index and the generation at the time when the element was inserted, then we can solve the aforementioned ABA problem. When indexing into the collection, if the index pair's generation does not match the generation of the element at that index, then the operation fails.

Features

  • Zero unsafe
  • Well tested, including quickchecks
  • no_std compatibility
  • All the trait implementations you expect: IntoIterator, FromIterator, Extend, etc...

Usage

First, add generational-arena to your Cargo.toml:

[dependencies]
generational-arena = "0.2"

Then, import the crate and use the generational_arena::Arena type!

extern crate generational_arena;
use generational_arena::Arena;

let mut arena = Arena::new();

// Insert some elements into the arena.
let rza = arena.insert("Robert Fitzgerald Diggs");
let gza = arena.insert("Gary Grice");
let bill = arena.insert("Bill Gates");

// Inserted elements can be accessed infallibly via indexing (and missing
// entries will panic).
assert_eq!(arena[rza], "Robert Fitzgerald Diggs");

// Alternatively, the `get` and `get_mut` methods provide fallible lookup.
if let Some(genius) = arena.get(gza) {
    println!("The gza gza genius: {}", genius);
}
if let Some(val) = arena.get_mut(bill) {
    *val = "Bill Gates doesn't belong in this set...";
}

// We can remove elements.
arena.remove(bill);

// Insert a new one.
let murray = arena.insert("Bill Murray");

// The arena does not contain `bill` anymore, but it does contain `murray`, even
// though they are almost certainly at the same index within the arena in
// practice. Ambiguities are resolved with an associated generation tag.
assert!(!arena.contains(bill));
assert!(arena.contains(murray));

// Iterate over everything inside the arena.
for (idx, value) in &arena {
    println!("{:?} is at {:?}", value, idx);
}

no_std

To enable no_std compatibility, disable the on-by-default "std" feature.

[dependencies]
generational-arena = { version = "0.2", default-features = false }

Serialization and Deserialization with serde

To enable serialization/deserialization support, enable the "serde" feature.

[dependencies]
generational-arena = { version = "0.2", features = ["serde"] }

More Repositories

1

dodrio

A fast, bump-allocated virtual DOM library for Rust and WebAssembly.
Rust
1,236
star
2

bumpalo

A fast bump allocation arena for Rust
Rust
1,018
star
3

wu.js

wu.js is a JavaScript library providing higher order functions for ES6 iterators.
JavaScript
864
star
4

state_machine_future

Easily create type-safe `Future`s from state machines — without the boilerplate.
Rust
322
star
5

github-api

Javascript bindings for the Github API.
JavaScript
170
star
6

glob-to-regexp

Convert a glob to a regular expression
JavaScript
145
star
7

operational-transformation

JavaScript implementation of Operational Transformation
JavaScript
141
star
8

oxischeme

A Scheme implementation, in Rust.
Rust
128
star
9

id-arena

A simple, id-based arena
Rust
88
star
10

bacon-rajan-cc

Rust
86
star
11

mach

A rust interface to the Mach 3.0 kernel that underlies OSX.
Rust
80
star
12

bindgen-tutorial-bzip2-sys

A tutorial/example crate for generating C/C++ bindings on-the-fly with libbindgen
Rust
75
star
13

inlinable_string

An owned, grow-able UTF-8 string that stores small strings inline and avoids heap-allocation.
Rust
69
star
14

intrusive_splay_tree

An intrusive splay tree implementation that is no-std compatible and free from allocation and moves
Rust
65
star
15

synth-loop-free-prog

Synthesis of Loop-free Programs in Rust
Rust
62
star
16

fart

fitzgen's art
Rust
61
star
17

peepmatic

A DSL and compiler for generating peephole optimizers for Cranelift
Rust
59
star
18

scrapmetal

Scrap Your Rust Boilerplate
Rust
55
star
19

operational-transformation-example

Example app using Operational Transformation
JavaScript
51
star
20

wasm-smith

A WebAssembly test case generator
44
star
21

source-map-mappings

Parse the `mappings` field in source maps
Rust
39
star
22

chronos

Avoids JavaScript timer congestion
JavaScript
38
star
23

wasm-nm

List the symbols within a wasm file
Rust
38
star
24

associative-cache

A generic, fixed-size, associative cache
Rust
35
star
25

fast-bernoulli

Efficient sampling with uniform probability
Rust
32
star
26

zoolander

Pure Python DSL for CSS.
Python
28
star
27

django-wysiwyg-forms

WYSIWYG form editor/creator django app
JavaScript
25
star
28

winliner

The WebAssembly Indirect Call Inliner
Rust
25
star
29

minisynth-rs

Program synthesis is possible in Rust
Rust
24
star
30

derive_is_enum_variant

Automatically derives `is_dog` and `is_cat` methods for `enum Pet { Dog, Cat }`.
Rust
22
star
31

tempest

Tempest jQuery Templating Plugin
JavaScript
22
star
32

pfds-js

Purely Functional Data Structures in JS
JavaScript
22
star
33

histo

Histograms with a configurable number of buckets, and a terminal-friendly Display.
Rust
21
star
34

one-page-wasm

Rust
21
star
35

shuffling-allocator

Rust
20
star
36

rpn-js

A reverse polish notation --> JavaScript compiler demoing source maps
JavaScript
19
star
37

bugzilla-todos

Bugzilla todo list of reviews, flag requests, and bugs to fix
JavaScript
19
star
38

dwprod

Rust
16
star
39

tryparenscript.com

The code behind TryParenScript.com
JavaScript
16
star
40

is_executable

Is there an executable file at the given path?
Rust
15
star
41

rent_to_own

A wrapper type for optionally giving up ownership of the underlying value.
Rust
12
star
42

noodles

Asynchronous, non-blocking, continuation-passing-style versions of the common higher order functions in `Array.prototype`.
JavaScript
12
star
43

rust-conf-2019

Flatulence, Crystals, and Happy Little Accidents
JavaScript
11
star
44

life

John Conway's Game of Life (in Erlang)
Erlang
10
star
45

peeking_take_while

Rust
9
star
46

bufrng

An RNG that generates "random" numbers copied from a given buffer
Rust
9
star
47

wasm-summit-2021

Stuff related to my Wasm Summit 2021 talk
Rust
9
star
48

geotoy

Polygons in Contact + Glium
Rust
9
star
49

preduce

A parallel, language agnostic, automatic test case reducer
Rust
8
star
50

erl-ot

Simple example of Operational Transformation in Erlang
Erlang
8
star
51

longest-increasing-subsequence

A crate for finding a longest increasing subsequence of some input
Rust
8
star
52

cl-pattern-matching

Erlang/Haskell style pattern matching macros for Common Lisp.
Common Lisp
7
star
53

parenscript-narwhal

Integrates ParenScript with Narwhal and provides a ParenScript REPL.
JavaScript
7
star
54

reform

A Narwhal module for working with HTML forms, similar to Django's forms.
JavaScript
7
star
55

ada-scheme

Following along with Peter Michaux's Scheme from Scratch series (http://peter.michaux.ca/articles/scheme-from-scratch-introduction) in Ada
6
star
56

wasm-debugging-capabilities

Collecting requirements for WebAssembly debugging capabilities
HTML
6
star
57

couchdb-io

Io library for working with CouchDB (not yet implemented)
Io
5
star
58

cool-rs

Rust
5
star
59

souper-ir

A library for manipulating Souper IR
Rust
4
star
60

canvas-tool

experimental canvas tool; WORK IN PROGRESS
JavaScript
4
star
61

code-size-benchmarks

Rust and WebAssembly code size micro benchmarks
Rust
4
star
62

dodrio-todomvc-wasm-instantiation

Rust
4
star
63

clife

Conway's life, in Rust.
Rust
3
star
64

class_views

Class based views for django
Python
3
star
65

Blimp

A very simple django blogging app for my site. Blog + Simple = Blimp?
Python
2
star
66

mxr

Search the Mozilla Cross Reference from the command line
Python
2
star
67

tokio-timeit-middleware

Rust
2
star
68

tasks

A demo app I used for my presentation on server side JS.
JavaScript
2
star
69

kahn

A build tool for AMD-style Common JS projects
JavaScript
2
star
70

boffo

Boffo
Erlang
2
star
71

pineapple

Not quite sure yet
JavaScript
2
star
72

mach_o_sys

Bindings to the OSX mach-o system library
Rust
2
star
73

dateformat

A port of Steven Levithan's dateFormat to CommonJS.
JavaScript
2
star
74

mcmc-maze-solver

Rust
2
star
75

protolith

Just for exploratory fun, I decided to see what it takes to write a prototypical object system in Lisp.
Common Lisp
2
star
76

strace.js

Trace JS native calls
JavaScript
2
star
77

mini-meta-git

Very simple, lightweight git repository authentication and management (because gitosis just gets in my way).
2
star
78

tron

An online implementation of Tron, but you play with code instead of keys. Built with Wu.js, WebSockets, and Node.
JavaScript
2
star
79

aossoa

A crate providing a macro to abstract and automate SoA vs AoS
Rust
1
star
80

powereddit

Make reddit better.
JavaScript
1
star
81

servo-trace-dump

JS and CSS for Servo's HTML timelines
HTML
1
star
82

reharm

Prolog
1
star
83

pybeasttree

Library for parsing/consuming BEAST tree files
Python
1
star
84

django-query-analyzer

Analyzer of django query
Python
1
star
85

rust-words

Learning Rust. Read words from stdin and then print each unique word out in order and how many times it was found.
Rust
1
star
86

breakpoint-rs

Set breakpoints with the `breakpoint!()` macro.
Rust
1
star
87

ocean-noise

Makes soothing ocean noises.
JavaScript
1
star
88

bender

Create robust user interfaces with finite state machines.
JavaScript
1
star
89

hippocampus

A Lisp dialect targetting JavaScript
JavaScript
1
star
90

magritte

My just-for-fun Lisp in progress
Common Lisp
1
star
91

loader

A smart, parallel client-side Javascript loader written in ParenScript
Common Lisp
1
star
92

learn-your-scales

Flashcard-style webapp for learning musical scales!
JavaScript
1
star
93

music-thang

JavaScript
1
star
94

what-the-ffi-python-example

Rust
1
star
95

bang-bang-con-west-2020

Writing Programs! That Write Other Programs!!
HTML
1
star
96

pancakes

Still a WIP
Rust
1
star
97

reader-submit-to-hn

Greasemonkey script to add a link for submitting articles to Hacker News from Google Reader.
JavaScript
1
star