Giter VIP home page Giter VIP logo

generational-arena's Introduction

Generational Arena

This is an allocator inspired from:

Example

let {GenerationalArena} = require("generational-arena");
let a = new GenerationalArena();
let f = a.insert("foo")
let b = a.insert("bar")
a.contains(b) // returns true
a.get(b)      // returns "bar"
a.remove(f);  // returns "foo"
a.contains(f) // returns false
a.get(f)      // returns undefined
for(k of a.values()){
  console.log(k)     
}
// bar
for(k of a.indices()){
  console.log(k)     
}
// Index{...}
for(k of a){
  console.log(k)     
}
// {index:Index{...},value:"bar"}

Why?

This is a data structure that offers certain guarantees and trade offs.

  • every time an object is inserted into the arena, a completely unique index will be returned that will never be given again.
  • whenever an index returned from arena is removed, space is freed to be re-used
  • once an index is freed, attempting to use it to get a value from the arena will return undefined
  • an index can be converted to and from a 64-bit integer represented as a big integer
  • an arena can hold max 2^32-1 items ( as limited by a JavaScript array )
  • an arena can hold max 2^32-1 generations
  • a generation increases on successful item removal
  • memory isn't freed back to operating system, just freed to be reused

Why is something like this useful?

  • being able to ask and release for memory with indexes without having to worry about if someone will mistakenly reuse a dead index
  • an index convertible to 64-bit will be able to be sent across to a web assembly module
  • useful in writing ECS engines to reuse memory for component allocation

generational-arena's People

Contributors

richardanaya avatar

Stargazers

Fabio Dias Rollo avatar  avatar Pearce Keesling avatar Mike Luan avatar  avatar Haroldo de Oliveira Pinheiro avatar

Watchers

 avatar  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.