Skip to main content

CodeCutter

CodeCutter is a Python library designed to preprocess Python functions and methods by performing compile-time optimizations. Inspired by the behavior of the C preprocessor, CodeCutter enhances execution performance by simplifying code logic before it runs, focusing on optimizing variables and control flow statements.

Features

  • Replace variables with constants or expressions: Automatically replaces variables with constant values or constant expressions when detected. This allows for faster execution as it reduces the need for variable lookups during runtime.
  • Simplify boolean if-clauses with constants: If a boolean expression contains constants, CodeCutter simplifies the expression by evaluating it during preprocessing. This can eliminate unnecessary branches in your code.
  • Remove dead if-clauses: If certain conditions in if statements are known to always evaluate to True or False, CodeCutter removes or short-circuits these branches. This only includes unreachable code due to constants, not always-true conditions.

Why CodeCutter?

Unlike regex-based or hacky import-time solutions, CodeCutter operates in a clean and structured way, leveraging Python’s internal structures to perform safe and reliable transformations. The library is built with extensibility in mind, allowing you to easily add custom preprocessing logic to match your specific optimization needs.

Documentation

A small documentation

Installation

You can install CodeCutter using pip:

pip3 install codecutter

Examples

Here's an example of how CodeCutter can improve your function before runtime.

Original bad function

def bad_function(
    iterations,
    feature_a_enabled,
    feature_b_enabled,
    feature_c_enabled,
):
    my_sum = 0

    for i in range(iterations):
        if feature_a_enabled:
            my_sum += 1

        if feature_b_enabled:
            my_sum += 2

        if feature_c_enabled:
            my_sum += 3

        my_sum += i

bad_function(10000, False, False, False)

The same function with preprocessing. Do note that the values of the constants are given as strings. This is so that the system can replace constants even with e.g. comparisons such as "value >= 5" or function calls. If you utilize variables outside of the function body (e.g. decorators), pass those to the preprocessor with variables kwarg.

from codecutter import preprocess

@preprocess(
    constants={
        "FEATURE_A_ENABLED": "False",
        "FEATURE_B_ENABLED": "False",
        "FEATURE_C_ENABLED": "False",
    }
)
def good_function(iterations):
    my_sum = 0

    for i in range(iterations):
        if FEATURE_A_ENABLED:
            my_sum += 1

        if FEATURE_B_ENABLED:
            my_sum += 2

        if FEATURE_C_ENABLED:
            my_sum += 3

        my_sum += i

good_function(10000)

The source code of the preprocessed function

def good_function(iterations):
    my_sum = 0
    for i in range(iterations):
        my_sum += i

Performance difference (best of 5 runs with %timeit)

  • Original: 267 µs ± 7.32 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
  • Preprocessed: 196 µs ± 5.57 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

Over 26% improvement!

Contributing & TODO

Preprocess expressions such as 1 + 2 + 3 * value * 4 -> 3 + 12 * value

Preprocess sequential binary opterations such as i += 1 + i += 2 -> i += 3

Detect deep attribute fetching in loops and replace (applies to non-functions too)

for i in range(1000):
    self.operations["myfunction"](i)

with

f = self.operations["myfunction"]
for i in range(1000):
    f(i)

Footnote

ChatGPT seems to excel at README's.

Metadata

Release files for codecutter 0.2.0

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

Source distribution (sdist)

Source distribution for codecutter 0.2.0
File Size Uploaded
codecutter-0.2.0.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for codecutter 0.2.0
File Interpreter ABI Platform
codecutter-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / codecutter-0.2.0.tar.gz

Download URL codecutter-0.2.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
278de2567ae76153bebc3d7f8dd50b99df4c9c8951372a0936e0de41b2660a47
BLAKE2b-256 checksum
How to use checksums
6f10c74c61a4b49eb97ec40fe2fb2f539919b5be01bf90d0bc245691d6c187ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.7 Linux/6.11.3-zen1-1-zen

Release files / codecutter-0.2.0-py3-none-any.whl

Download URL codecutter-0.2.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2c5d429572fce67390055079edfed6104198f3811e96111cd7c365d01524065
BLAKE2b-256 checksum
How to use checksums
1b47f35f5e86fee01393f71775bed2e40a8527b46a1b40410f5bbaaa4e876166
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.4 CPython/3.12.7 Linux/6.11.3-zen1-1-zen

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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