Skip to main content

📦 Stash - Memory Optimisation Class-Decorator

License: MIT Python Version PyPI - 0.1.0


What is Stash?

Stash is a 'slots'-based Python class-decorator developed to assist Python developers in significantly reducing memory overhead when initiating individual classes. It dynamically creates a new optimised class with necessary dunder-methods behind the scene (init, repr, eq), and also adds user-specified methods to ensure custom functionality. The application also supports immutability through 'Freeze' parameter, disabling user's ability to set attribute values post-initialisation for better memory efficiency.

❓ Why use Stash?

Python's default class instances store attributes in a __dict__, which provides general flexibility but at the cost of significant memory overhead - Especially when constructing large numbers of objects. Stash helps to eliminate this fact by replacing this traditional memory storage with __slots__ resulting in:

  • ⚡ Lower Memory Usage: Substantially reduces per-instance memory overhead.
  • 🚀 Faster Attribute Access: Slots-based storage supports faster lookup than ordinary __dict__.
  • 🔐 Optional Immutability: Prevents unintended mutations by freezing attributes post-init.
  • 🧼 Clean API: Less boilerplate code as Stash automatically generates __init__, __repr__, __eq__.
  • 🧩 Selective Method Preservation: Use @conserve to explicitly preserve methods - only keep what's essential.
  • 🧠 Better for Large Datasets: Ideal for performance-critical application like data science, simulations, and high-volume APIs.

Stash is designed to help you write lean, efficient and Pythonic code

📋 Key Features

  • 🎁 Simple-to-use Python class decorator for memory optimisation.
  • 🛠️ Dynamically creates slots-based classes to minimize memory overhead.
  • ⚗️ Adds essential dunder methods (init, repr, eq).
  • 🔄 Allows user to specify class-methods to preserve and inherit.
  • ❄️ Supports attribute Freeze-functionality (_frozen) for increased memory efficiency.
  • 📊 Retains original class metadata for analysis, debugging and introspection.

🧠 Installation

Install Stash using your preferred Package Manager.

pip install stash-memory
# or
poetry add stash-memory
# or
conda install stash-memory

#️⃣ Quickstart Example

from Stash import Stash

@Stash
class Example:
    name: str
    age: int
    is_single: bool

example1 = Example(
    name="Tony Stark",
    age=34,
    is_single=False
)

print(example1.name)        # Tony Stark
print(example1.age)         # 34
print(example1.is_single)   # False

NOTE: An __init__ method is automatically created similarly to @dataclass

🧊 Freeze mechanic

Stash also supports immutability by disabling modifications to instance attributes after initialization. By setting freeze=True, individual instances become read-only. Any attempts to modify or change attributes raises an AttributeError.

from Stash import Stash

@Stash(freeze=True)
class Example:
    name: str
    age: int
    is_villain: bool

example1 = Example(
    name="Otto Octavius",
    age=52, 
    is_villain=True
)

example1.name = "Norman Osborn"     # Raises an AttributeError, as values are immutable.

🔒 Preserve Methods

Utilise the @conserve decorator to explicitly mark methods for preservation in the new slots-based class. Individual methods NOT marked will not be present in new class.

from Stash import Stash, conserve

@Stash
class Example:
    name: str
    age: int
    is_superhero: bool

    # Method is preserved
    @conserve
    def smash(self):
        print(f"{self.name} SMASH!")

    # Method is NOT preserved, due to lack of @conserve-decorator
    def surrender(self):     
        print(f"{self.name} surrenders!")

example1 = Example(
    name="Bruce Banner",
    age=39,
    is_superhero=True
)

example1.smash()                # Successfully runs and prints message.

try:                            # Raises AttributeError: Instance has no atribute 'surrender'
    example1.surrender()
except AttributeError as e:
    print(f"Method missing {e}")

📈 Benchmark Results

To validate Stash's memory optimisation, I performed a benchmark test utilising a @Stash decorated class against a standard Python class using tracemalloc.

🧪 Test Setup

  • Test: Instantiating 10,000 objects with 3 attributes.
  • Compared:
    • ✅ RegularClass - Traditional class utilising __dict__ for attribute storage.
    • ✅ StashClass - Optimised class using @Stash
  • Tool: tracemalloc, sys.getsizeof, pympler.asizeof for memory tracking.

💡 Results Summary

Measurement RegularClass StashClass Notes
Total memory used (Tracemalloc) 1.5 MiB 1.5 MiB Snapshot difference for 10,000 instances
Avg. memory per instance 154 B 155 B Similar values, but granularity might affect final result
Shallow object size (sys.getsizeof) 48 B 65 B Memory size of instance container only
Deep object size (pympler.asizeof) 360 B 168 B Includes all attributes: Stash saves >50% memory

⚙️ Advanced Usage

  • Inheritance: Preserved methods marked with @conserve are correctly inherited.
  • Caching: Stash automatically caches generated classes for improved performance.
  • Interning: String attributes are automatically interned to ensure memory efficiency.

🧪 Testing

Stash provides a full testing suite covering preservation, immutability, caching and general performance metrics. Run tests via:

pytest

📜 Licensing

This project is licensed under the MIT License - see the LICENSE section for further details.

👥 Collaboration

If you have any suggestions for improvements to the codebase, future app extensions or you simply wish to help expand Stash's functionality further, please do not hesitate to reach out at: HysingerDev@gmail.com

Metadata

Release files for stash-memory 0.0.1

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

Source distribution (sdist)

Source distribution for stash-memory 0.0.1
File Size Uploaded Provenance
stash_memory-0.0.1.tar.gz 13.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for stash-memory 0.0.1
File Interpreter ABI Platform
stash_memory-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 26.2 kB

Release files / stash_memory-0.0.1.tar.gz

Download URL stash_memory-0.0.1.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
46b193f8ade73a03de502459ed21aea31c41376b92f1e17a800c494ded5f96d4
BLAKE2b-256 checksum
How to use checksums
1c205136873b3164af33e6650ef03f40bedee96c7c173dca2886b3bc8d8afe50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / stash_memory-0.0.1-py3-none-any.whl

Download URL stash_memory-0.0.1-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdc0f570b36f6c7fae0fc735c545fc41f4d08dfa26f2110c6111c62107c6769f
BLAKE2b-256 checksum
How to use checksums
1cc49ca424f80e5f76b0368e65f0a9514e188eb85b6c1a26a9b88dd30d84504b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for stash-memory 0.0.1

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.0.1 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