Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[mypyc] Simplify comparison of tuple elements#19396

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@p-sawicki
Copy link
Collaborator

Got rid of unnecessary operations when comparing tuple elements returns a bit primitive.

Example code:

def f(x: tuple[float], y: tuple[float]) -> bool:    return x == y

IR before:

def f(x, y):    x, y :: tuple[float]    r0, r1 :: float    r2 :: bit    r3 :: object    r4 :: i32    r5 :: bit    r6, r7, r8 :: boolL0:    r0 = x[0]    r1 = y[0]    r2 = r0 == r1    r3 = box(bit, r2)    r4 = PyObject_IsTrue(r3)    r5 = r4 >= 0 :: signed    if not r5 goto L5 (error at f:2) else goto L1 :: boolL1:    r6 = truncate r4: i32 to builtins.bool    if not r6 goto L2 else goto L3 :: boolL2:    r7 = 0    goto L4L3:    r7 = 1L4:    return r7L5:    r8 = <error> :: bool    return r8

IR after:

def f(x, y):    x, y :: tuple[float]    r0, r1 :: float    r2 :: bit    r3 :: boolL0:    r0 = x[0]    r1 = y[0]    r2 = r0 == r1    if not r2 goto L1 else goto L2 :: boolL1:    r3 = 0    goto L3L2:    r3 = 1L3:    return r3

Tested using the following benchmark:

def f(x: tuple[float,float], y: tuple[float,float]) -> bool:    return x == ydef bench(n: int) -> None:    for x in range(n):        lhs = (float(x), float(x * 2))        rhs = (float(x), float(x * 3))        f(lhs, rhs)from time import timebench(1000)t0 = time()bench(50 * 1000 * 1000)print(time() - t0)

Execution time goes from ~315ms to ~150ms on my machine.

Copy link
Collaborator

@JukkaLJukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Nice perf win!

@JukkaLJukkaL merged commitf9fe331 intopython:masterJul 8, 2025
13 checks passed
@JukkaL
Copy link
Collaborator

I measured the impact to mypy self check runtime usingmisc/perf_compare.py -- this improved performance by 0.4%, which very good (measured 100 samples)!

@JukkaL
Copy link
Collaborator

this improved performance by 0.4%

I did another measurement and it appears to be random noise, unfortunately.

@p-sawickip-sawicki deleted the simplify-tuple-elem-compare branchJuly 8, 2025 16:09
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@JukkaLJukkaLJukkaL approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@p-sawicki@JukkaL

[8]ページ先頭

©2009-2025 Movatter.jp