Like wee_alloc, but smaller since I used skinnier letters in the name.

Overview

lol_alloc

A laughably simple wasm global_allocator.

Like wee_alloc, but smaller since I used skinnier letters in the name.

lol_alloc is a experimental wasm global_allocator.

I wrote lol_alloc to learn about allocators (I hadn't written one before) and because wee_alloc seems unmaintained and has a leak. After looking at wee_alloc's implementation (which I failed to understand or fix), I wanted to find out how hard it really is to make a wasm global_allocator, and it seemed like providing one could be useful to the rust wasm community.

Soundness

Use of Pointers

Soundness of the pointer manipulation in this library is currently unclear. Since wasm32::memory_grow does not return a pointer there is no "original pointer" so the Strict Provenance rules can not be followed. Attempting to determine if this library's use of pointes at least meets the requirements for being dereferenceable when it dereferences them is similarly challenging as that is defined as:

dereferenceable: the memory range of the given size starting at the pointer must all be within the bounds of a single allocated object.

The definition of "allocated object" is not clear here. If the growable wasm heap counts as a single allocated object, then all these allocators are likely ok (in this aspect at least). However if each call to wasm32::memory_grow is considered to create a new allocated object, then the free list coalescing in FreeListAllocator in unsound and could result in undefined behavior.

Thread Safety

LeakingAllocator and FreeListAllocator are not thread-safe and use unsound implementations of Sync to allow their use as the global allocator.

Multithreading is possible in wasm these days: applications have multiple threads should not use these allocators.

Making thread safe versions of them is possible, but none of the allocators in this library are intended for high performance: if for some reason you have multithreaded wasm, and don't care about performance, it should be possible to modify these allocators to meet your needs, but consider just using Rust's built in allocator instead (it is thread safe, and fast).

Status

Not production ready.

Current a few allocators are provided with minimal testing. If you use it, please report any bugs. If it actually works for you, also let me know (you can post an issue with your report).

Currently only FailAllocator and LeakingPageAllocator are thread-safe safe.

Sizes of allocators include overhead from example:

  • FailAllocator: 195 bytes: errors on allocations. Operations are O(1),
  • LeakingPageAllocator: 230 bytes: Allocates pages for each allocation. Operations are O(1).
  • LeakingAllocator: 356 bytes: Bump pointer allocator, growing the heap as needed and does not reuse/free memory. Operations are O(1).
  • FreeListAllocator: 656 bytes: Free list based allocator. Operations (both allocation and freeing) are O(length of free list), but it does coalesce adjacent free list nodes.

LeakingAllocator should have no allocating space overhead other than for alignment.

FreeListAllocator rounds allocations up to at least 2 words in size, but otherwise should use all the space. Even gaps from high alignment allocations end up in its free list for use by smaller allocations.

Builtin rust allocator is 5053 bytes in this same test. If you can afford the extra code size, use it: its a much better allocator.

Supports only wasm32: other targets may build, but the allocators will not work on them (except: FailAllocator, it errors on all platforms just fine).

Usage

You can replace the global_allocator in wasm32 with FreeListAllocator builds using:

#[cfg(target_arch = "wasm32")]
use lol_alloc::FreeListAllocator;

#[cfg(target_arch = "wasm32")]
#[global_allocator]
static ALLOCATOR: FreeListAllocator = FreeListAllocator::new();

Testing

There are some normal rust unit tests (run with cargo run test), which use a test implementation of MemoryGrower.

There are also some wasm-pack tests (run with wasm-pack test --node lol_alloc)

Size testing:

wasm-pack build --release example && ls -l example/pkg/lol_alloc_example_bg.wasm
You might also like...
It's like pixelfix, but quix.
It's like pixelfix, but quix.

pixelquix It's like pixelfix, but quix. Written in Rust for low-level speed Uses rayon for work-stealing parallelism Based on Jump Flood for impressiv

Like HashSet but retaining INSERTION order and without `Hash` requirement on the Element type.

identified_vec A collection of unique identifiable elements which retains insertion order, inspired by Pointfree's Swift Identified Collections. Simil

🧠 A command-line utility for switching git branches more easily. Switch branches interactively or use a fuzzy search to find that long-forgotten branch name.
🧠 A command-line utility for switching git branches more easily. Switch branches interactively or use a fuzzy search to find that long-forgotten branch name.

git-smart-checkout A git command extension for switching git branches more efficiently. About Interactively switch branches or fuzzy search for that f

Experimental Rust UI library for Audulus. "rui" is a temporary name.

Experimental Rust UI library for Audulus. "rui" is a temporary name.

A truly zero-dependency crate providing quick, easy, reliable, and scalable access to the name "jordin"

jordin Finally! A truly zero-dependency crate providing quick, easy, reliable, and scalable access to the name "jordin". Additionally, this one-of-a-k

Give me your APK, I will give you framework name

APK-FiD Give me your APK, I will give you framework name Usage ❯ apk-fid -h APK-FiD 1.0 Petruknisme [email protected] Give me your APK, I will giv

Run a program with a modified process name.

tagrun Run a program with a modified process name. You can add a tag for a process with tagrun, and use the tag to locate the process with ps, pgrep o

A basic text editor, written in Rust (hence the name).

rut A basic text editor, written in Rust (hence the name). Why, though? I just wanted a basic TUI text editor (like Nano) that could: Be used with all

lace up GFA files using panSN path name sub-ranges to coordinate the lacing

gfalace gfalace is a Rust-based tool designed to process a collection of GFA (Graphical Fragment Assembly) files. It aims to interpret the path names

Comments
  • Error with simd_wasm64 feature

    Error with simd_wasm64 feature

    I tried using this in my wasm project and I get the following error:

    error[E0658]: use of unstable library feature 'simd_wasm64'
      --> /home/simon/.cargo/git/checkouts/lol_alloc-22b2a73873fe6f0a/ecf5399/lol_alloc/src/lib.rs:1:1
       |
    1  | / #![no_std]
    2  | |
    3  | | #[cfg(test)]
    4  | | #[macro_use]
    ...  |
    35 | |     fn memory_grow(&self, delta: PageCount) -> PageCount {
    36 | |         PageCount(core::arch::wasm::memory_grow(0, delta.0))
       | |_______________________________________________^
       |
       = note: see issue #90599 <https://github.com/rust-lang/rust/issues/90599> for more information
       = help: add `#![feature(simd_wasm64)]` to the crate attributes to enable
    
    opened by Titaniumtown 6
Owner
Craig
Craig
Lightweight alternative Discord client with a smaller footprint and some fancy extensible features.

Dorion Dorion is an alternative Discord client aimed and lower-spec or storage-sensitive PCs that supports themes, plugins, and more! Table of Content

SpikeHD 20 Jan 2, 2023
A bit like tee, a bit like script, but all with a fake tty. Lets you remote control and watch a process

teetty teetty is a wrapper binary to execute a command in a pty while providing remote control facilities. This allows logging the stdout of a process

Armin Ronacher 259 Jan 3, 2023
du + rust = dust. Like du but more intuitive.

Dust du + rust = dust. Like du but more intuitive. Why Because I want an easy way to see where my disk is being used. Demo Install Cargo cargo install

andy.boot 5.4k Jan 4, 2023
Like grep, but uses tree-sitter grammars to search

tree-grepper Works like grep, but uses tree-sitter to search for structure instead of strings. Installing This isn't available packaged anywhere. That

Brian Hicks 219 Dec 25, 2022
Zenith - sort of like top or htop but with zoom-able charts, CPU, GPU, network, and disk usage

Zenith - sort of like top or htop but with zoom-able charts, CPU, GPU, network, and disk usage

Benjamin Vaisvil 1.6k Jan 4, 2023
A command-line shell like fish, but POSIX compatible.

A command-line shell like fish, but POSIX compatible.

Seiya Nuta 813 Dec 29, 2022
Like a cell, but make lifetimes dynamic instead of ownership

LendingCell is a mutable container that allows you to get an owned reference to the same object. When the owned reference is dropped, ownership return

Kalle Samuels 19 Dec 15, 2022
Like Lua, but in Rust, and different

AirScript It's like Lua, but in Rust, and different. Introduction AirScript is a dynamically typed, interpreted language inspired by Lua and written i

David Delassus 5 Jun 28, 2022
`0x` - like `xxd` but colorful!

0x - like xxd but colorful! Note: for technical reasons, this library is listed as ohx on crates.io. 0x is a simple utility similar to xxd with a few

Miguel Young 57 Jun 12, 2023
It's like monkeytype but from your terminal

DoggyType It's like monkeytype but from ur terminal (ik very unoriginal lmao) Preview doggytype-preview.mp4 Requirements: Rust Build: git clone https:

Nirlep 6 Aug 29, 2023