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-94108: Specialize for int & operation by kumaraditya303 · Pull Request #94109 · python/cpython

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Jun 22, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Benchmark:

./python -m pyperf timeit -s "a, b= 100, 200" "a & b" 

Result:

Benchmark base patch
int & 18.2 ns 16.8 ns: 1.08x faster

Sorry, something went wrong.

Uh oh!

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

All reactions
@kumaraditya303
fastpath for int &
4ef67e9
@kumaraditya303
kumaraditya303 requested a review from markshannon as a code owner June 22, 2022 09:37
@bedevere-bot bedevere-bot added the awaiting review label Jun 22, 2022
@blurb-it
📜🤖 Added by blurb_it.
d74890d
@markshannon

markshannon commented Jun 22, 2022

Copy link
Copy Markdown
Member

Is int & int common enough to justify the extra instruction?

The number of left op right combinations is huge; we can't specialize for all of them.
Even if we tried to specialize for common builtin classes, we would omit potential specialization of third-party classes, like numpy arrays.

We have discussed having some sort of double-dispatching mechanism and cache, to allow fast dispatching of binary operators without an explosion of instructions, but I can't find the original discussion. Maybe @brandtbucher can find it?

The general idea is that we register a C function as an implementation of a binary operation for a specific pair of classes and operator. For example, we could register add_float_int(PyFloatObject *f, PyLongObject *i) for (float, int, "+") and have the VM dispatch to it efficiently.

We'd probably keep the specific specializations for the most common operations, like int + int, but other less common operations would use the more general instruction.

All reactions

Sorry, something went wrong.

Uh oh!

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

@markshannon

markshannon commented Jun 22, 2022

Copy link
Copy Markdown
Member

Looking at the stats shows the int & int is the most common specialization failure.
The question is, is it worth its own instruction, or is it worth waiting for a more general mechanism?

All reactions

Sorry, something went wrong.

Uh oh!

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

@kumaraditya303

kumaraditya303 commented Jun 22, 2022

Copy link
Copy Markdown
Contributor Author

Is int & int common enough to justify the extra instruction?

I created this PR based on stats and 33% (largest failure) seemed significant to me but if you don't think this is worth it, I have no objection if you reject this :).

All reactions

Sorry, something went wrong.

Uh oh!

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

@markshannon

markshannon commented Jun 22, 2022

Copy link
Copy Markdown
Member

I'm not rejecting this. I'm just not sure whether it is a good idea.
I think we should go with the more general mechanism long term, but that doesn't mean that we can't add this specialization until then.

I'm going to benchmark it first.

All reactions

Sorry, something went wrong.

Uh oh!

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

@markshannon markshannon mentioned this pull request Jun 22, 2022
GH-91432: Implement the FOR_ITER_SET specialization #94104
Closed
@markshannon

markshannon commented Jun 22, 2022

Copy link
Copy Markdown
Member

Pyperformance shows a small speedup, but close to noise.

All reactions

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented Jun 22, 2022

Copy link
Copy Markdown
Member

We have discussed having some sort of double-dispatching mechanism and cache, to allow fast dispatching of binary operators without an explosion of instructions, but I can't find the original discussion. Maybe @brandtbucher can find it?

Here you go: faster-cpython/ideas#162

All reactions

Sorry, something went wrong.

Uh oh!

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

@brandtbucher

brandtbucher commented Jun 22, 2022

Copy link
Copy Markdown
Member

Pyperformance shows a small speedup, but close to noise.

Yeah, I made a pass at this a few months ago. Even adding in shift operations, I wasn't able to get above "1.00x faster".

I'm skeptical of adding lots of additional code here for negligible gain.

All reactions

Sorry, something went wrong.

Uh oh!

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

@markshannon

markshannon commented Jun 23, 2022

Copy link
Copy Markdown
Member

I'm inclined to reject this in favor of a table based approach: faster-cpython/ideas#162.

The table based approach won't give as much of a speedup per specialization, but will allow many more specializations for fewer new opcodes.

All reactions

Sorry, something went wrong.

Uh oh!

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

@kumaraditya303 kumaraditya303 closed this Aug 28, 2022
@kumaraditya303
kumaraditya303 deleted the int-fast-and branch August 28, 2022 13:46
@brandtbucher brandtbucher mentioned this pull request Dec 13, 2022
gh-100165: Add BINARY_OP_AND_INT for integer & operation. #100166
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

awaiting review

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.

4 participants

@kumaraditya303 @markshannon @brandtbucher @bedevere-bot
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.