Skip to main content

High-performance shared memory allocation for Python

Project description

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

FreeListAllocator (Recommended for general use)

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shared_allocator-0.1.0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shared_allocator-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file shared_allocator-0.1.0.tar.gz.

File metadata

  • Download URL: shared_allocator-0.1.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.19

File hashes

Hashes for shared_allocator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 72c9eadf2596827c33397ae71a2aa794a0bc6574605e098f6022ea074073c9bf
MD5 69cad7fff0c987d82c064f0691c9859b
BLAKE2b-256 bfaf23388b127aac50a26ffa556e86e06ca7d06e8f547dc53bc16c74a8fd9a8e

See more details on using hashes here.

File details

Details for the file shared_allocator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for shared_allocator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4407821fc038155aec1edaa082d7fb78aefd2c67e698917df6b4ea77625540fb
MD5 c39ca90a95ce058ddeb0251b0b80aae4
BLAKE2b-256 36251899aa6fc994c85d5255b7986c4d00ce8125491026d0c6d1f1dc799b4ff1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page