Crate oxc_allocator

Source
Expand description

§⚓ Oxc Memory Allocator

Oxc uses a bump-based memory arena for faster AST allocations. This crate contains an Allocator for creating such arenas, as well as ports of memory management data types from std adapted to use this arena.

§No Drops

Objects allocated into Oxc memory arenas are never Dropped. Memory is released in bulk when the allocator is dropped, without dropping the individual objects in the arena.

Therefore, it would produce a memory leak if you allocated Drop types into the arena which own memory allocations outside the arena.

Static checks make this impossible to do. Allocator::alloc, Box::new_in, Vec::new_in, and all other methods which store data in the arena will refuse to compile if called with a Drop type.

§Examples

use oxc_allocator::{Allocator, Box};

struct Foo {
    pub a: i32
}

impl std::ops::Drop for Foo {
    fn drop(&mut self) {}
}

struct Bar {
    v: std::vec::Vec<u8>,
}

let allocator = Allocator::default();

// This will fail to compile because `Foo` implements `Drop`
let foo = Box::new_in(Foo { a: 0 }, &allocator);
// This will fail to compile because `Bar` contains a `std::vec::Vec`, and it implements `Drop`
let bar = Box::new_in(Bar { v: vec![1, 2, 3] }, &allocator);

Consumers of the oxc umbrella crate pass Allocator references to other tools.

use oxc::{allocator::Allocator, parser::Parser, span::SourceType};

let allocator = Allocator::default();
let parsed = Parser::new(&allocator, "let x = 1;", SourceType::default());
assert!(parsed.errors.is_empty());

Re-exports§

Modules§

  • A hash map without Drop, that uses FxHasher to hash keys, and stores data in arena allocator.
  • Arena String.

Structs§

  • Memory address of an AST node in arena.
  • A bump-allocated memory arena based on bumpalo.
  • A Box without Drop, which stores its data in the arena allocator.
  • A Vec without Drop, which stores its data in the arena allocator.

Traits§

  • A trait to explicitly clone an object into an arena allocator.
  • This trait works similarly to the standard library From trait, It comes with a similar implementation containing blanket implementation for IntoIn, reflective implementation and a bunch of primitive conversions from Rust types to their arena equivalent.
  • Trait for getting the memory address of an AST node.
  • This trait works similarly to the standard library Into trait. It is similar to FromIn is reflective, A FromIn implementation also implicitly implements IntoIn for the opposite type.