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: cythonize inspect signature #10

Merged
merged 1 commit into from
Aug 7, 2024
Merged

feat: cythonize inspect signature #10

merged 1 commit into from
Aug 7, 2024

Conversation

kszucs
Copy link
Owner

@kszucs kszucs commented Aug 7, 2024

The objective is to have a more performant inspect.Signature implementation especially in regards of binding arguments to parameters. This is going to help to validate both function calls and object initializations.

Even without compilation the added Signature class outperforms the stdlib one:

---------------------------------------------------------------------------------- benchmark: 2 tests ---------------------------------------------------------------------------------
Name (time in us)        Min                Max              Mean            StdDev            Median               IQR            Outliers  OPS (Kops/s)            Rounds  Iterations
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_signature        1.1880 (1.0)      13.5914 (1.0)      1.3703 (1.0)      0.5457 (1.0)      1.2643 (1.0)      0.0413 (1.0)      751;1326      729.7579 (1.0)       20000          10
test_inspect          2.0654 (1.74)     16.7978 (1.24)     2.3383 (1.71)     0.6871 (1.26)     2.1622 (1.71)     0.0840 (2.03)     810;3676      427.6570 (0.59)      20000          10
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

After compilation:

--------------------------------------------------------------------------------------------- benchmark: 2 tests ---------------------------------------------------------------------------------------------
Name (time in ns)            Min                     Max                  Mean                StdDev                Median                 IQR            Outliers  OPS (Kops/s)            Rounds  Iterations
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_signature          346.5975 (1.0)        7,967.0979 (1.0)        377.4496 (1.0)        183.3671 (1.0)        363.8997 (1.0)        6.4989 (1.0)      109;1711    2,649.3602 (1.0)       20000          10
test_inspect          2,068.0018 (5.97)     177,226.7016 (22.24)    2,317.2059 (6.14)     1,517.5780 (8.28)     2,196.4021 (6.04)     103.6999 (15.96)    686;1291      431.5542 (0.16)      20000          10
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Benchmarked via

def func(x: int, y: str, *args: int, z: float = 3.14, **kwargs) -> float: ...


args = (1, "a", 2, 3, 4)
kwargs = dict(z=3.14, w=5, q=6)
expected = {"x": 1, "y": "a", "args": (2, 3, 4), "z": 3.14, "kwargs": {"w": 5, "q": 6}}


def test_inspect(benchmark):
    sig = InspectSignature.from_callable(func)
    r = benchmark.pedantic(
        sig.bind, args=args, kwargs=kwargs, iterations=ITS, rounds=20000
    )
    assert r.arguments == expected


def test_signature(benchmark):
    sig = Signature.from_callable(func)
    r = benchmark.pedantic(
        sig.bind, args=args, kwargs=kwargs, iterations=ITS, rounds=20000
    )
    assert r == expected

@kszucs kszucs force-pushed the signature branch 4 times, most recently from 3c1d0fe to 5ae6d8a Compare August 7, 2024 10:58
@cpcloud cpcloud merged commit 1bfcfa2 into main Aug 7, 2024
24 checks passed
@cpcloud cpcloud deleted the signature branch August 7, 2024 11:47
@kszucs kszucs restored the signature branch August 24, 2024 17:58
@kszucs kszucs deleted the signature branch August 24, 2024 17:58
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.

2 participants