Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(performance): add __slots__ to Result class #41

Conversation

acostapazo
Copy link
Contributor

@acostapazo acostapazo commented Oct 23, 2022

Benchmark code:

from __future__ import annotations

from timeit import timeit

from meiga import Error, Failure, Result, Success


class NoSuchKey(Error):
    ...


class TypeMismatch(Error):
    ...


def string_from_key(
    dictionary: dict, key: str
) -> Result[str, NoSuchKey | TypeMismatch]:
    if key not in dictionary.keys():
        return Failure(NoSuchKey())

    value = dictionary[key]
    if not isinstance(value, str):
        return Failure(TypeMismatch())

    return Success(value)


dictionary = {
    "key1": "value",
    "key2": 2,
    "key3": "value",
    "key4": 2,
    "key5": "value",
    "key6": 2,
    "key7": "value",
    "key8": 2,
    "key9": "value",
    "key10": 2,
    "key11": "value",
    "key12": 2,
}

time_success = timeit(lambda: string_from_key(dictionary=dictionary, key="key1"))
print(f"time when success: {time_success}")

time_failure_no_such_key = timeit(
    lambda: string_from_key(dictionary=dictionary, key="invalid_key")
)
print(f"time when failure (no such key): {time_failure_no_such_key}")

time_failure_type_missmatch = timeit(
    lambda: string_from_key(dictionary=dictionary, key="key2")
)
print(f"time when failure (type missmatch): {time_failure_type_missmatch}")

Result without slots:

$ python benchmark/time_result.py
time when success: 0.5478533749992494
time when failure (no such key): 0.5760475420102011
time when failure (type missmatch): 0.6188615420251153

Result with slots:

$ python benchmark/time_result.py
time when success: 0.5113824579748325
time when failure (no such key): 0.550075833016308
time when failure (type missmatch): 0.5880016250011977

Improvements

Improvement when success: ~7% faster (x1.071318279412337)
Improvement when failure (no such key): ~5% faster (x1.047214779190496)
Improvement failure (type missmatch): ~5% faster (x1.052482707039891)

@acostapazo acostapazo linked an issue Oct 23, 2022 that may be closed by this pull request
3 tasks
@acostapazo acostapazo added this to the v1.8.0 milestone Oct 23, 2022
@acostapazo acostapazo merged commit 1b30de4 into main Oct 26, 2022
@acostapazo acostapazo deleted the 37-use-__slots__-for-faster-attribute-access-and-space-savings-in-memory branch October 26, 2022 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use __slots__ for faster attribute access and space savings in memory
3 participants