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.0.tar.gz (63.1 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.0-py3-none-any.whl (41.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastalloc-0.1.0.tar.gz
  • Upload date:
  • Size: 63.1 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.0.tar.gz
Algorithm Hash digest
SHA256 a5c4b3fdeece3e99df7d800268cca1c96242ff4282370b3500f1053e0954d3d1
MD5 8ae8d1fe006e5364b0f5fc252f71e7d7
BLAKE2b-256 5e7546ca026b583abbd58095a794f1804360490b97c35b55ab1cd7e7607d02f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastalloc-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f0e696242ff5fa668fcb71835d0665778ebd6fba71e8c38cfa81be427274bc3
MD5 5a963f1115ce3cb7c3a7530fff62c922
BLAKE2b-256 8d583db2220edcdbc91aa9c723a0da0599fb57363a71e63e4253bcb87569c29d

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