Skip to content
dew-uffPublic

About

No description, website, or topics provided.

Resources

Contributing

Security policy

Stars

1 star

Watchers

5 watching

Forks

Repository files navigation

SpeeduPy

CI Code coverage Python versions License: MIT

SpeeduPy is a performance optimization tool developed in Python. It utilizes an advanced memoization and persistent caching approach applied to pure functions, drastically accelerating the execution time of scientific and computation-intensive scripts.


Key Features

  • Flexible Cache: Results persist in files, SQLite, MySQL, MongoDB (documents or GridFS) or Redis, with an optional Memcached layer in memory.
  • Extreme Performance: Implementation of ultra-fast hashing algorithms, ensuring instantaneous cache lookups and maximizing retrieval speed.
  • Execution Modes: Support for different optimization and sampling strategies.
  • Disposable Servers: The server backends can be tried with Docker, through compose.yaml.

How to Install

SpeeduPy can be installed from PyPI, or from a clone of the repository: in the Docker image, or directly on your host machine.

SpeeduPy runs on Python 3.11 to 3.15, on Linux, Windows and macOS. Other Python versions are not supported. The commands below use python3.11; replace it with the supported version you have.

From PyPI

SpeeduPy is not on PyPI yet: the first release there will be 2.0.0. From then on, this installs a release with the drivers of every server backend (replace X.Y.Z with the version; the quotes keep shells such as zsh from reading the brackets):

$ python3.11 -m pip install "speedupy[all]==X.Y.Z"

pip install speedupy==X.Y.Z brings only the file and sqlite storages, which need no server. Each server backend is an extra, which installs its driver:

Extra For
mysql -s mysql
mongo -s mongo, -s gridfs, -s mongo-file, -s mongo-gridfs
redis -s redis
memcached --mem-arch memcached-wt, --mem-arch memcached-wb
all every one of the above

A backend whose driver is not installed stops with a message that names its extra.

0. Clone or Download the Repository

Start by getting the SpeeduPy source code onto your local machine:

# Option 1: Clone using Git
$ git clone https://github.com/dew-uff/speedupy.git
$ cd speedupy

# Option 2: Download the ZIP and unpack it

Option 1: Using Dockerfile (Recommended for Isolation)

You can build and run the development image directly using the Dockerfile provided in the project root.

Known problem: docker build currently fails, because conda asks for the Anaconda Terms of Service (#150). Until it is fixed, use Option 2.

# 1. Build the Docker image
$ docker build -t speedupy-dev .

# 2. Run the image in the background (mapping the current project folder for code access)
#    (The exact run command might need adjustments based on port mapping and volumes)
$ docker run -d --name speedupy-container -v $(pwd):/app speedupy-dev

# 3. Start the container if it was stopped
$ docker start speedupy-container

# 4. Access the environment shell
$ docker exec -it speedupy-container /bin/bash

The image installs SpeeduPy from /app in editable mode, with every server driver, so the mounted folder is the code that runs.

Option 2: Host Machine (Manual Setup)

For direct host installation, install SpeeduPy from the clone, in the environment that will run your experiments (a virtual environment is a good place). pip installs the pinned dependencies of pyproject.toml with it.

# From the clone, with the drivers of every server backend
$ python3.11 -m pip install ".[all]"

pip install . installs only the file and sqlite storages; name the extras you need instead of all, as in ".[redis]" (see From PyPI). pip install -e ".[all]" installs it in editable mode instead, for working on SpeeduPy itself. The install adds the command speedupy-setup (see Experiment Setup).

The server storages and the Memcached memory architectures also need their server: install it with your system's package manager, or try it with Docker (Trying the server backends with Docker). The versions SpeeduPy is tested with are MySQL 9.0.1, MongoDB 6.0.16, Redis 7.4.0 and Memcached 1.6.18.

Hashes

-H/--hash chooses the hash that identifies each function call in the cache. The default is xxhash.

Hash Kind
xxhash Non-cryptographic (XXH3, 128 bits). The default, recommended.
md5 From the Python standard library.
a5hash Research-grade. Native library.
museair Research-grade. Native library.

Research-grade hashes: available and fully tested.

The native hashes (a5hash, museair) come prebuilt in src/speedupy/native_lib/ for Linux x86_64 and aarch64, Windows x64, and macOS x86_64 and arm64, so an install from a plain clone runs them with no compiler. On any other platform xxhash and md5 still work. To rebuild the binaries (a changed wrapper, source commit or platform), follow native/README.md; the design is docs/native-hashes.md.


How to Set Up and Use

This section guides you through preparing your Python scripts for acceleration and setting up the server environment they may need.

SpeeduPy must be installed in the Python environment that runs your experiment (How to Install); the experiment's directory holds only your scripts. Run the commands below from that directory. If you used SpeeduPy 1.x, remove the speedupy/ folder or link from the experiment's directory: a copy next to your script comes before the installed package on sys.path and would run instead of it.

1. Starting Database Servers

Skip this step if your experiment uses the file or sqlite storage: they need no server.

For a server storage or a Memcached memory architecture, start the server it needs: your own installation, or a disposable one (Trying the server backends with Docker).

Inside the container of the Dockerfile (Option 1), a shell script starts the servers installed there. It needs the container's conda environment and /app/dbs folder, so it does not run on a host machine. It does not create the data folders of the servers (#153):

# Starts all configured database servers
$ ./speedupy_dev_env.sh start all

# Stops them
$ ./speedupy_dev_env.sh stop all

2. Adapting Your Scripts

Mark the pure functions you want to accelerate with @deterministic, and import it from speedupy.speedupy. The main script needs a function named main: setup adds @initialize_speedupy to it, with its import.

from speedupy.speedupy import deterministic


@deterministic
def fibonacci(n):
    return n if n < 2 else fibonacci(n - 1) + fibonacci(n - 2)


def main():
    print(fibonacci(30))


if __name__ == "__main__":
    main()

3. Experiment Setup

This step analyzes your code, injects the decorators and creates the necessary metadata (like function call graphs). It rewrites your scripts. It must be run after any modification to your experiment's source code, or when setting up for the first time.

# Replace file_name.py with the name of your main experiment file
$ speedupy-setup file_name.py

# To prepare a server storage, name it with -s/--storage (default: file)
$ speedupy-setup -s redis file_name.py

python3.11 -m speedupy.setup_exp.setup runs the same setup, for an environment whose scripts folder is not on the PATH. Setup adds from speedupy.speedupy import ... to your scripts; the sys.path.append(...) line that SpeeduPy 1.x added is no longer needed and does no harm.

-s/--storage takes the same names as the execution option of the same name: sqlite, redis, mongo, gridfs, mongo-file, mongo-gridfs, file and mysql. Setup prepares only that storage and never touches the servers of the others, so it needs no server unless you choose one. On the first setup of a directory, it empties the server storage you choose (Redis, MongoDB) or recreates its database (MySQL), so do not point it at a server that holds data you need.

Run the experiment with the same storage you set up, passing -s to both commands: their defaults differ, file for setup and sqlite for execution (#146). The local SQLite file that setup always creates holds the function-call provenance whatever the storage.


Running and Optimizing Experiments

You can control the entire caching mechanism, from the hashing algorithm to the storage backend, using command-line arguments.

Execution Options

Short Arg Long Arg Description Accepted Values
-h --help Displays all available options.
-H --hash Hash that identifies a function call (see Hashes). Default: xxhash. xxhash, md5, a5hash, museair
-s --storage Persistence backend: files, SQLite, MySQL, Redis, or MongoDB (documents; GridFS; documents with large data in files or in GridFS). Default: sqlite. file, sqlite, mysql, redis, mongo, gridfs, mongo-file, mongo-gridfs
--exec-mode Execution mode: manual (default) caches the @deterministic functions, no-cache caches nothing. The other three modes are experimental (beta): see docs/experimental.md. manual, no-cache, accurate, probabilistic-counting, probabilistic-error
--retrieval-strategy How data is loaded to main memory: each call when needed, all of a function, or everything at the start. Default: eager. lazy, function, eager
--retrieval-exec-mode Whether loading blocks the experiment. Default: sequential. thread is beta: see docs/experimental.md. sequential, thread
--mem-arch Cache structure in main memory. The memcached-* values need a Memcached server (write-through, write-back). Default: 2-dict-fast. 0-dict, 1-dict, 2-dict, 2-dict-fast, memcached-wt, memcached-wb
--serializer Serializer that persists the data. Default: cloudpickle. pickle, cloudpickle
-mt --min-exec-time-to-memoize Minimum time, in seconds, a call must take to be cached. Default: 0.01.
--measure-time Prints the time the script took to execute.
--profile Prints counters of calls and cache hits at the end.

Usage Examples

1. Running Without Cache (Baseline Measurement):

# Measures the time baseline, ignoring all cache settings
$ python3.11 file_name.py --exec-mode no-cache

2. Running With Cache:

# Example: MD5 hashing and SQLite storage
$ speedupy-setup -s sqlite file_name.py
$ python3.11 file_name.py -H md5 -s sqlite

Trying the server backends with Docker

You can try the server backends without installing a server: compose.yaml starts disposable ones with Docker Compose. It is the file the tests of the server backends run against.

To use Profile Port on this machine
-s redis redis 6379
-s mongo, -s gridfs, -s mongo-file, -s mongo-gridfs mongo 27017
-s mysql mysql 3306
--mem-arch memcached-wt or memcached-wb memcached 11211

The profile all starts the four servers. From the experiment's directory, with SpeeduPy installed; path/to/speedupy is your clone:

# 1. Start one server; the command returns when it is healthy
$ docker compose -f path/to/speedupy/compose.yaml --profile redis up -d --wait

# 2. Set up the experiment and run it with that storage
$ speedupy-setup -s redis file_name.py
$ python3.11 file_name.py -s redis --profile

# 3. Remove the servers, and the cache with them
$ docker compose -f path/to/speedupy/compose.yaml --profile all down -v

These servers are for trying the backends:

  • They keep nothing. The data lives in memory or in the container, and down -v removes it. For real use, install a server.
  • They have no password (MySQL accepts root with an empty one) and listen on this machine's loopback only.
  • A port must be free. Compose refuses to start when a server already holds it. Setup empties the storage it prepares, so never point it at a server that holds data you need.
  • A script as small as the example above may run too fast to be cached: add -mt 0 to cache every call.
  • Setup does not empty Memcached (#84).

Running Tests

How to set up the development environment and run the tests, with or without database servers, is in CONTRIBUTING.md. The tests that need a server erase its data, and are not run by default.


About

No description, website, or topics provided.

Resources

Contributing

Security policy

Stars

1 star

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages