• Stars
    star
    499
  • Rank 86,332 (Top 2 %)
  • Language
    Rust
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

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

Repository Details

The arena, a fast but limited type of allocator

typed-arena

GitHub Actions Build Status

A fast (but limited) allocation arena for values of a single type.

Allocated objects are destroyed all at once, when the arena itself is destroyed. There is no deallocation of individual objects while the arena itself is still alive. The flipside is that allocation is fast: typically just a vector push.

There is also a method into_vec() to recover ownership of allocated objects when the arena is no longer required, instead of destroying everything.

Example

use typed_arena::Arena;

struct Monster {
    level: u32,
}

let monsters = Arena::new();

let goku = monsters.alloc(Monster { level: 9001 });
assert!(goku.level > 9000);

Safe Cycles

All allocated objects get the same lifetime, so you can safely create cycles between them. This can be useful for certain data structures, such as graphs and trees with parent pointers.

use std::cell::Cell;
use typed_arena::Arena;

struct CycleParticipant<'a> {
    other: Cell<Option<&'a CycleParticipant<'a>>>,
}

let arena = Arena::new();

let a = arena.alloc(CycleParticipant { other: Cell::new(None) });
let b = arena.alloc(CycleParticipant { other: Cell::new(None) });

a.other.set(Some(b));
b.other.set(Some(a));

Alternatives

Need to allocate many different types of values?

Use multiple arenas if you have only a couple different types or try bumpalo, which is a bump-allocation arena that can allocate heterogenous types of values.

Want allocation to return identifiers instead of references and dealing with references and lifetimes everywhere?

Check out id-arena or generational-arena.

Need to deallocate individual objects at a time?

Check out generational-arena for an arena-style crate or look for a more traditional allocator.

More Repositories

1

arcstr

Better reference counted strings for Rust
Rust
97
star
2

ubrustc

Unborrowed Rust Compiler (rustc without a borrowchecker)
Rust
88
star
3

equirect-to-cubemap-faces

Convert an equirectangular image (aka lat-long map) into cubemap faces you can feed to WebGL
JavaScript
28
star
4

dunjeon

small roguelike game in clojure
Clojure
27
star
5

pcg-random

PCG RNG for JavaScript
JavaScript
26
star
6

W

Adventure/Puzzle Game
Racket
24
star
7

startup

Life before `main()`
Rust
17
star
8

fast-srgb8

Fast conversion between linear float and 8-bit sRGB
Rust
15
star
9

quick-noise.js

Fast, public domain implementation of perlin noise
JavaScript
14
star
10

ld23

ludum dare 23
Clojure
12
star
11

Argh

3d game in clojurescript. unfinished. frustruating to develop.
Clojure
10
star
12

atomic_float

Floating point types which can be safely shared between threads
Rust
9
star
13

bad3d

a 3d physics engine in rust. not really a serious project.
Rust
8
star
14

cobb

Rust
7
star
15

rust-more-asserts

Small library providing additional assert and debug_assert macros for rust code.
Rust
6
star
16

radix-sorter

JavaScript typed array radix sort
JavaScript
6
star
17

handy

Rust handle library
Rust
6
star
18

nonzero_lit

Easy, safe, and fully zero-cost NonZero constants
Rust
5
star
19

thrid

Fast Thread Identifier (WIP)
Rust
5
star
20

rust-base16

Base16 (hex) encoding functions for rust
Rust
4
star
21

GRID

Visual Esoteric Programming Language
Clojure
4
star
22

unlambda-clj

Unlambda in Clojure!
Clojure
4
star
23

ulock-sys

Rust bindings for Darwin's not-exactly-public ulock API, which provides futex-like functionality
Rust
4
star
24

tearor

Rust library providing a *barely* thread-safe cell.
Rust
3
star
25

unlambda-rs

Unlambda interpreter in Rust, for some reason.
Rust
3
star
26

core_detect

A `no_std` version of the `std::is_x86_feature_detected!` macro.
Rust
3
star
27

index_vec

A Rust library offering newtyped indices and vecs that use those indices
Rust
3
star
28

ld33

my entry into ludum dare 33 (fall 2015), bloody tentacles
JavaScript
2
star
29

persistent-hashmap

A JavaScript port of Clojure's persistent hash-maps
JavaScript
2
star
30

static_locks

Rust
2
star
31

liblls

A small collection of utilities extracted from llvm's codebase.
C++
2
star
32

fish-cargo-completions

improved fish completions for cargo.
Shell
2
star
33

cpuid

Processure feature tests for x86/x86-64.
C++
2
star
34

Scream

A mediocre (generously) dialect of Lisp written in Ruby
Ruby
1
star
35

almost

A crate for comparing floating point numbers
Rust
1
star
36

libcont

Continuations for C
C
1
star
37

hex

Hexagonal Cellular Automata in ClojureScript
Clojure
1
star
38

dunjeon-cljs

ClojureScript port of dunjeon
Clojure
1
star
39

Nine

9: a spacey shootery game
Java
1
star
40

envparse

wip compile-time env parsing crate
Rust
1
star
41

mentat-places-test

Rust
1
star
42

threadidbench

Rust
1
star
43

js-shader-program

yet another webgl shader program abstraction
JavaScript
1
star
44

thin_str

Rust
1
star
45

GBC

generic breakout clone in html5+coffeescript (so i can learn both of those simultaneously)
CoffeeScript
1
star