Skip to main content

High-performance Python memory pool library for pre-allocated object pooling

Project description

fastalloc

Python Version PyPI Version License CI Status Coverage

A high-performance Python memory pool library providing pre-allocated object pools for frequently created and destroyed objects. Reduce allocation overhead, minimize garbage collection pressure, and eliminate memory fragmentation.

Features

  • Multiple Pool Types: Fixed-size, growing, thread-safe, thread-local, and async pools
  • High Performance: < 50ns object acquisition, 80%+ reduction in GC cycles
  • Thread Safety: Built-in thread-safe and thread-local pool variants
  • Async Support: First-class async/await integration
  • Type Safe: Full type hints with mypy strict mode support
  • Easy to Use: Context managers, decorators, and builder patterns
  • Statistics: Built-in performance monitoring and reporting
  • Production Ready: Comprehensive tests (95%+ coverage), benchmarks, and documentation

Installation

pip install fastalloc

Quick Start

Basic Usage

from fastalloc import Pool

pool = Pool(MyObject, capacity=1000)

with pool.allocate() as obj:
    obj.do_something()
    result = obj.get_result()

Builder Pattern

from fastalloc import Pool, GrowthStrategy

pool = (Pool.builder()
    .type(Entity)
    .capacity(10_000)
    .max_capacity(100_000)
    .growth_strategy(GrowthStrategy.LINEAR, increment=1000)
    .pre_initialize(True)
    .reset_method('reset')
    .enable_statistics(True)
    .build())

Thread Safety

from fastalloc import ThreadSafePool, ThreadLocalPool

# shared pool with locking
pool = ThreadSafePool(MyObject, capacity=1000)

# per-thread pools without locking
thread_local_pool = ThreadLocalPool(MyObject, capacity=100)

Async Support

import asyncio
from fastalloc import AsyncPool

async def main():
    pool = AsyncPool(Connection, capacity=100)
    async with pool.allocate() as conn:
        await conn.fetch_data()
        result = await conn.process()

asyncio.run(main())

Decorator Pattern

from fastalloc import pooled

@pooled(capacity=1000, thread_safe=True)
class Worker:
    def process(self, data):
        pass

with Worker.pool.allocate() as worker:
    worker.process(data)

Use Cases

  • Game Development: Entity systems, particles, physics objects
  • Data Processing: ETL pipelines, streaming transforms, hot-path temporaries
  • Web Servers: Request/response objects, connection pools, worker reuse
  • Scientific Computing: Simulation particles, graph nodes, computational wrappers
  • Real-Time Systems: Audio/video processing, robotics loops
  • Machine Learning: Training loop temporaries, batch processing

Performance

fastalloc delivers significant performance improvements over standard object creation:

  • Object Acquisition: < 50ns (vs ~200ns for standard allocation)
  • GC Reduction: 80%+ fewer collection cycles
  • Memory Overhead: < 10% for pools over 1000 objects
  • Thread-Safe: < 200ns with moderate contention

See benchmarks for detailed results.

Documentation

Requirements

  • Python 3.8+
  • No external dependencies for core functionality
  • Optional: numpy for array-backed pools

Development

# Clone repository
git clone https://gitlab.com/TIVisionOSS/python/fastalloc.git
cd fastalloc

# Install development dependencies
pip install -r requirements-dev.txt

# Run tests
./scripts/test_all.sh

# Run benchmarks
./scripts/benchmark.sh

# Type check
./scripts/type_check.sh

# Lint
./scripts/lint.sh

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

This project is dual-licensed under MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE for details.

Author

Eshan Roy eshanized@proton.me

Organization: Tonmoy Infrastructure & Vision

Repository

https://gitlab.com/TIVisionOSS/python/fastalloc

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

fastalloc-0.1.1.tar.gz (63.3 kB view details)

Uploaded Source

Built Distribution

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

fastalloc-0.1.1-py3-none-any.whl (41.9 kB view details)

Uploaded Python 3

File details

Details for the file fastalloc-0.1.1.tar.gz.

File metadata

  • Download URL: fastalloc-0.1.1.tar.gz
  • Upload date:
  • Size: 63.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for fastalloc-0.1.1.tar.gz
Algorithm Hash digest
SHA256 31ef2c0e911bfc90f38dfdd5f3e8ec18add0542026b2d07bd0915fc2cf3a6a11
MD5 398fab14051da6d95111ba181de3fc49
BLAKE2b-256 5802cd6c5f74f397b53cee58aa80f90c40c257cc6bb90ac2813e40d8c47025d1

See more details on using hashes here.

File details

Details for the file fastalloc-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fastalloc-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 41.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for fastalloc-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df436fb8bbc6a3805daac1bfdf4ac9a0c26fcff1d41810bf8ba5ea5e169d39bf
MD5 0551b2590ef4d4733e725735b1e9da67
BLAKE2b-256 122e5628a9b081b26ca2d7d3b27f3bedfa786216170cd53f5cd7e8375120ee7b

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