GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-134584: Eliminate redundant refcounting from `_CALL_TYPE_1` by tomasr8 · Pull Request #135818 · python/cpython

Conversation

@tomasr8

@tomasr8 tomasr8 commented Jun 22, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Depends on #135761

  • Issue: Eliminate redundant refcounting in the JIT #134584

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Jun 22, 2025
Eliminate redundant refcounting in the JIT #134584
Closed
35 tasks done
@tomasr8
tomasr8 force-pushed the jit-refcount-call-type branch from 19ed708 to 184d8f1 Compare June 22, 2025 15:52
tomasr8
tomasr8 commented Jun 22, 2025
View reviewed changes
Comment thread Python/bytecodes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner
Fidget-Spinner reviewed Jun 22, 2025
View reviewed changes

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks mostly good, just 2 comments that also explain the CI failure.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Python/optimizer_bytecodes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/bytecodes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@tomasr8

tomasr8 commented Jun 22, 2025

Copy link
Copy Markdown
Member Author

Thanks for the review! I'll continue working on it tomorrow (Monday) 🙂

Fidget-Spinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 23, 2025

Copy link
Copy Markdown
Member

I just merged #135761 in. Sorry but you'll have to merge the changes in, as I did not ultimately add _SWAP_FAST.

tomasr8 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tomasr8
Eliminate redundant refcounting from _CALL_TYPE_1
85ab405
@tomasr8
tomasr8 force-pushed the jit-refcount-call-type branch from 184d8f1 to 85ab405 Compare June 23, 2025 18:19
tomasr8 added 2 commits June 23, 2025 20:53
@tomasr8
Add a new uop
f927209
@tomasr8
Add news entry
bbf1c9d
@tomasr8
tomasr8 marked this pull request as ready for review June 23, 2025 18:54
@tomasr8
tomasr8 requested a review from markshannon as a code owner June 23, 2025 18:54
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 23, 2025
Fidget-Spinner
Fidget-Spinner reviewed Jun 23, 2025
View reviewed changes
Comment thread Python/bytecodes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

tomasr8 added 2 commits June 23, 2025 21:10
@tomasr8
Do not close callable stackref
81f9b44
@tomasr8
Update remove_unneeded_uops with new uops
6934f63
Fidget-Spinner
Fidget-Spinner approved these changes Jun 23, 2025
View reviewed changes

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yay, thank you!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tomasr8 reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Jun 23, 2025
@Fidget-Spinner

Fidget-Spinner commented Jun 23, 2025

Copy link
Copy Markdown
Member

I will do some microbenchmarking to see if this does anything. Give me a sec.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 23, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

On this script:

import pyperf

def func():
    x = 1
    for _ in range(120000000):
        type(x)

if __name__ == "__main__":
    runner = pyperf.Runner()
    runner.bench_func('micro', func)

Mean +- std dev: [no-opt] 918 ms +- 17 ms -> [opt] 897 ms +- 21 ms: 1.02x faster

So it's slightly faster. However, note that this optimization won't fully pay off till we get #135465 merged. So let's wait for now till then. Thanks!

Basically right now there's the extra cost of reading and writing to the stack due to the extra _POP_TOP. We're eliminating 1 reference count check in return. This seems to be barely worth it, but I crunched the numbers before and once the stack becomes registers, it becomes very worth it.

If you're interested, I benchmarked my own branch with this (higher is better):

CPython base JIT:
Number of long_calcul per second: 10.23 ± 0.13

CPython TOS caching:
Number of long_calcul per second: 9.60 ± 0.37

CPython decref binary op int elimination:
Number of long_calcul per second: 10.14 ± 0.07

CPython TOS + decref binary op int elimination:
Number of long_calcul per second: 11.45 ± 0.10

So we should expect to see an around ~10% speedup once TOS caching is merged, and we start doing this!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 23, 2025

Copy link
Copy Markdown
Member

@tomasr8 congrats!!! I decreased the loop count, rebased your PR on top of Mark's TOS caching PR, and did a micro bench with the script above.

TOS Caching vs TOS Caching + This PR
Mean +- std dev: [no-opt] 210 ms +- 3 ms -> [opt] 195 ms +- 1 ms: 1.08x faster

So 8% improvement if we have TOS caching enabled on type operations that load a local variable!

tomasr8 reacted with rocket emoji tomasr8 reacted with eyes emoji
All reactions
  • 🚀 1 reaction
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner Fidget-Spinner mentioned this pull request Jun 23, 2025
gh-134584: Eliminate redundant refcounting from _CALL_TUPLE_1 #135860
Merged
@tomasr8

tomasr8 commented Jun 23, 2025

Copy link
Copy Markdown
Member Author

Awesome stuff! And a pretty nice speedup for a relatively simple optimization too!
I can try and have a look at Mark's PR so that we can get it merged sooner :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

brandtbucher
brandtbucher reviewed Jul 1, 2025
View reviewed changes
Comment thread Python/bytecodes.c Outdated
}

tier2 op(_SWAP_CALL_ONE_LOAD_CONST_INLINE_BORROW, (ptr/4, callable, null, arg -- value, a)) {
PyStackRef_CLOSE(arg);

@brandtbucher brandtbucher Jul 1, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This shouldn't be here, right? Because we're leaving arg on the stack in a?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tomasr8 tomasr8 Dec 22, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Veery late response 😅 but I changed and renamed this op to _SHUFFLE_2_LOAD_CONST_INLINE_BORROW since it's analogous to the recently added _SHUFFLE_3_LOAD_CONST_INLINE_BORROW. This also resolves the issue with closing the ref! :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
tomasr8 added 5 commits December 22, 2025 22:31
@tomasr8
Do not close arg
e86897a
@tomasr8
Merge remote-tracking branch 'upstream/main' into jit-refcount-call-type
bd62d15
@tomasr8
Update test
ca15e78
@tomasr8
Rename _SWAP_.. to _SHUFFLE_2_..
bcb805d
@tomasr8
Merge remote-tracking branch 'upstream/main' into jit-refcount-call-type
56fa30f
@tomasr8
Regenerate files
225d6db
@tomasr8

tomasr8 commented Dec 23, 2025

Copy link
Copy Markdown
Member Author

I think it should be good now! Though I haven't yet caught up on the latest changes in the JIT so another pair of eyes would be nice :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tomasr8
tomasr8 requested review from Fidget-Spinner and brandtbucher December 23, 2025 10:21
Fidget-Spinner
Fidget-Spinner reviewed Dec 23, 2025
View reviewed changes

@Fidget-Spinner Fidget-Spinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Just one change, otherwise LGTM!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tomasr8 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Python/bytecodes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner added 2 commits December 23, 2025 16:14
@Fidget-Spinner
address review
bba9435
@Fidget-Spinner
Merge remote-tracking branch 'upstream/main' into pr/135818
6ffffa6
Fidget-Spinner
Fidget-Spinner approved these changes Dec 23, 2025
View reviewed changes
@Fidget-Spinner

Fidget-Spinner commented Dec 23, 2025

Copy link
Copy Markdown
Member

Windows failure is unrelated. Probably due to the other known bug in executor management. I'm merging this, and will be responsible for any breakages/reverts. Thank Tomas, and happy holidays!

tomasr8 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner
Fidget-Spinner merged commit 25c294b into python:main Dec 23, 2025
69 of 70 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Dec 23, 2025
@tomasr8

tomasr8 commented Dec 23, 2025

Copy link
Copy Markdown
Member Author

Thanks for the review, Ken! Happy holidays to you as well! :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tomasr8
tomasr8 deleted the jit-refcount-call-type branch December 23, 2025 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Fidget-Spinner Fidget-Spinner Fidget-Spinner approved these changes
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@brandtbucher brandtbucher Awaiting requested review from brandtbucher

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@tomasr8 @Fidget-Spinner @brandtbucher
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.