Skip to main content

shared_allocator

High-performance shared memory allocation for Python multiprocessing.

Provides multiple allocator strategies optimized for different use cases:

  • BumpAllocator: Ultra-fast (2-3x faster), reset-based allocation
  • FreeListAllocator: General-purpose with individual deallocation support
  • SlabAllocator: Fixed-size slot allocation with bitmap tracking

Features

  • 🚀 High Performance: 2-7 million operations per second
  • 🔒 Process-Safe: Safe concurrent access from multiple processes
  • 💾 Zero-Copy: Uses POSIX shared memory for efficient IPC
  • 🎯 Multiple Strategies: Choose the right allocator for your use case

Installation

uv pip install -e .

Quick Start

from shared_allocator import FreeListAllocator

# Create allocator
allocator = FreeListAllocator(name="my_allocator", capacity=10*1024*1024, create=True)

# Allocate and write
offset = allocator.allocate(256)
allocator.write(offset, b"Hello from shared memory!")

# Read back
data = allocator.read(offset, 25)
print(data)  # b"Hello from shared memory!"

# Free when done
allocator.free(offset)

# Cleanup
allocator.close()
allocator.unlink()

BumpAllocator (Fastest for temporary allocations)

from shared_allocator import BumpAllocator

# Create allocator
allocator = BumpAllocator(name="fast_allocator", capacity=10*1024*1024, create=True)

# Allocate lots of temporary data
for i in range(1000):
    offset = allocator.allocate(128)
    allocator.write(offset, f"Record {i}".encode())

# Reset all at once (much faster than individual frees)
allocator.reset()

# Memory is available again
print(f"Available: {allocator.available_bytes()} bytes")

allocator.close()
allocator.unlink()

Performance Benchmarks

Based on pytest-benchmark results:

Operation BumpAllocator FreeListAllocator Winner
Write 7,122 Kops/s 4,216 Kops/s Bump (1.7x)
Read 5,958 Kops/s 5,016 Kops/s Bump (1.2x)
Sequential Alloc 2,927 Kops/s 1,040 Kops/s Bump (2.8x)
Alloc+Free N/A 1,227 Kops/s FreeList
Reuse Pattern N/A 596 Kops/s FreeList

Recommendation: Use BumpAllocator for 2-3x better performance when you can bulk-reset. Use FreeListAllocator when you need individual deallocation.

Use Cases

BumpAllocator

  • Request-scoped allocations (allocate during request, reset after)
  • Temporary buffers in data processing pipelines
  • Phase-based computations
  • Scenarios where all allocations have similar lifetimes

FreeListAllocator

  • Long-lived objects with mixed allocation/deallocation
  • Dynamic data structures
  • When you need fine-grained memory management
  • General-purpose shared memory allocation

Development

# Install dependencies
uv sync

# Run tests
pytest

# Run benchmarks
pytest tests/test_benchmarks.py --benchmark-only

# Run tests with coverage
pytest --cov=shared_allocator

# Run examples
python examples/basic_usage.py

Architecture

All allocators use POSIX shared memory with the following structure:

[METADATA] [ALLOCATOR-SPECIFIC DATA] [DATA POOL]
  • BumpAllocator: Simple offset counter, O(1) allocation
  • FreeListAllocator: Stack of free slot indices, O(1) alloc/free
  • SlabAllocator: Bitmap for slot tracking, O(n) allocation

Learned from shared_hashmap:

  • 8-byte alignment for atomic operations
  • Lazy atomic view loading for performance
  • Clean separation of metadata and data regions

License

MIT

Metadata

Release files for shared_allocator 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shared_allocator 0.1.0
File Size Uploaded Provenance Actions
shared_allocator-0.1.0.tar.gz 11.1 kB Without provenance Details for shared_allocator-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shared_allocator 0.1.0
File Interpreter ABI Platform Actions
shared_allocator-0.1.0-py3-none-any.whl Python 3 none any Details for shared_allocator-0.1.0-py3-none-any.whl

Total release size: 22.1 kB

Release files / shared_allocator-0.1.0.tar.gz

Download URL shared_allocator-0.1.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
72c9eadf2596827c33397ae71a2aa794a0bc6574605e098f6022ea074073c9bf
BLAKE2b-256 checksum
How to use checksums
bfaf23388b127aac50a26ffa556e86e06ca7d06e8f547dc53bc16c74a8fd9a8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.19

Release files / shared_allocator-0.1.0-py3-none-any.whl

Download URL shared_allocator-0.1.0-py3-none-any.whl
Size 11.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4407821fc038155aec1edaa082d7fb78aefd2c67e698917df6b4ea77625540fb
BLAKE2b-256 checksum
How to use checksums
36251899aa6fc994c85d5255b7986c4d00ce8125491026d0c6d1f1dc799b4ff1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.19

Security for shared_allocator 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page