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-124153: Introduce PyType_GetBaseByToken function (PoC) by neonene · Pull Request #121079 · python/cpython

Conversation

@neonene

@neonene neonene commented Jun 27, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Reference implementation of the following C-API functinons:

  • PyType_GetBaseByToken()
  • PyType_GetToken()

Discussion: https://discuss.python.org/t/55598


📚 Documentation preview 📚: https://cpython-previews--121079.org.readthedocs.build/

  • Issue: Add PyType_GetBaseByToken function with Py_tp_token slot #124153

Sorry, something went wrong.

Uh oh!

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

All reactions
neonene added 17 commits June 27, 2024 17:13
@neonene
add document
eb0d23d
@neonene
introduce Py_tp_token and ht_token
273bac1
@neonene
fix PyType_GetSlot()
d041167
@neonene
add PyType_GetBaseByToken() PyType_GetToken()
d41cead
@neonene
add test
1456baa
@neonene
add ctypes example
14b4103
@neonene
use tp_mro directly (like PyType_IsSubtype) for free-threading err
7af3378
@neonene
silence warning
0b8bd07
@neonene
edit testcase
f917ad9
@neonene
remove an assertion
e08d58f
@neonene
add missing cast
22c20d9
@neonene
cast again
7fa0d81
@neonene
edit testcase
5621704
@neonene
remove redundant testcase
492a7d5
@neonene
edit testcase
f71fa78
@neonene
add test for no-result mode 1/2
83760d7
@neonene
add test for no-result mode 2/2
245d9d4
@erlend-aasland erlend-aasland added the topic-C-API label Jun 28, 2024
neonene added 3 commits June 29, 2024 07:13
@neonene
edit test
a0858a7
@neonene
add functions to check perf
4fb9cf8
@neonene
remove previous experiment commit
eb29cf8
@neonene

This comment was marked as outdated.

Sign in to view
neonene added 5 commits July 3, 2024 18:34
@neonene
abandon the proposal of PyType_GetToken()
ccd5ede
@neonene
optimize GetBaseByToken like GetModuleByDef
7d3f8b6
@neonene
add a repeat test (temporary use)
100972d
@neonene
Merge branch 'main' into bytoken
c58ef1c
@neonene
fix a build error
7e89a98
@neonene

neonene commented Jul 3, 2024

Copy link
Copy Markdown
Contributor Author

The tp_bases can be used in a fallback implementation. I checked the overhead, adding a repeat function temporarily (100972d):

from timeit import timeit
setup = f"""if 1:
    import _testcapi
    A = _testcapi.create_type_with_token("_testcapi.A", 0)
    tokenA = _testcapi.get_tp_token(A)
    class B(A): pass
    class C(B): pass
    class D(C): pass
    class E(D): pass
    getbase = _testcapi.repeat_getbasebytoken
"""
c_repeat = 10  # py_repeat = timeit default (1000000)
mro   = timeit(s1 := f'getbase(C, tokenA, {c_repeat}, True)', setup)
bases = timeit(s2 := f'getbase(C, tokenA, {c_repeat}, False)', setup)
print(s1, mro)
print(s2, bases,  bases / mro)

Win non-debug: (the higher, the slower)

find A
from
starts
with
run once
in C
repeat 10
in C
mro class A 1.00 1.00
bases class A 1.00x 1.00x
mro class B 1.00x 1.05x
bases class B 1.01x 1.14x
mro class C 1.01x 1.10x
bases class C 1.04x 1.40x
mro class E 1.02x 1.16x
bases class E 1.08x 1.50x
All reactions

Sorry, something went wrong.

Uh oh!

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

@neonene neonene changed the title Introduce PyType_GetBaseByToken function and friends Introduce PyType_GetBaseByToken function Jul 3, 2024
@neonene
remove the repeat test
1de4cba
neonene added 20 commits August 30, 2024 00:21
@neonene
_decimal: restore PyType_GetModuleByDef() in bin-ops
52616f1
This will keep the slowdown by up to 2% on the `telco` benchmark (PGO).  Unlike the `PyDecContextObject`, extending the `PyDecObject` struct seems to affect only binary ops and seems to be a waste of memory.
@neonene
Merge branch 'main' into bytoken
5e094d0
@neonene
fix build error
ac03429
@neonene
optimize PyType_GetBaseByToken()
ac82d36
@neonene
_decimal: improve performance
2a24934
Faster than the upstream by up to 2% on the `telco` benchmarks (PGO/non-PGO).  Based on the GetBaseByToken() optimization
by ac82d36.
@neonene
Merge branch 'main' into bytoken
ce68195
@neonene
rename
fa936d7
@neonene
clarify the optimizations
a9121fc
@neonene
typo
7f50633
@neonene
Add a private function and use it
ea6e9d3
Keeps the performance unchanged even if the private function is not inlined (i.e. not trained well on PGO).
@neonene
Merge branch 'main' into bytoken
60779f8
@neonene
revert private function (borrowed ref ver.)
588d5ee
PyType_GetBaseByToken() fails to inline the wrapped ptivate function, whose overhead appears to be not ignorable.
@neonene
Merge branch 'main' into bytoken
de19b5a
@neonene
edit header file
b730abb
@neonene
bad PyType_GetBaseByToken() example
77f143a
This cleanup can cause a slowdown by 10% on the `telco` benchmark for some reason.
@neonene
recover performance when a reference is needed
b114bc8
@neonene
Merge branch 'main' into bytoken
08bfb87
@neonene
edit PyType_GetBaseByToken()
f08da25
@neonene
recover performance take2
e3c1182
This version sets the *result to NULL at the end to reduce the overhead of double memory acces when returning true. Under verification.
@neonene
Merge branch 'main' into bytoken
d95e422
encukou
encukou reviewed Sep 16, 2024
View reviewed changes

@encukou encukou 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.

The code looks good. Do you want to clear the draft bit, and file an issue?

If there are performance more tweaks to make, they can go in a follow-up PR.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Modules/_ctypes/ctypes.h Outdated
PyErr_Format(PyExc_TypeError, "expected a ctypes type, got '%N'", type);
return NULL;
}
exercise_get_base_by_token(PyCType_Type);

@encukou encukou Sep 16, 2024

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.

Could you remove the exercise from this PR?
Hopefully the training will get better as PyType_GetBaseByToken is used more; if not, we can adjust it in a future PR.

Sorry, something went wrong.

Uh oh!

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

All reactions

@neonene neonene Sep 17, 2024

Copy link
Copy Markdown
Contributor 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.

I'll post a new PR. It may be better to have a dedicated branch when the result argument is NULL.

Sorry, something went wrong.

Uh oh!

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

encukou reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@neonene neonene changed the title Introduce PyType_GetBaseByToken function gh-124153: Introduce PyType_GetBaseByToken function (PoC) Sep 17, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Sep 17, 2024
Add PyType_GetBaseByToken function with Py_tp_token slot #124153
Closed
6 tasks done
neonene and others added 5 commits September 17, 2024 12:45
@neonene
do not exercise in ctypes
974fce3
@neonene
edit this version of PyType_GetBaseByToken()
61bb346
@neonene
ditto
cd750ca
@blurb-it
📜🤖 Added by blurb_it.
ca3043f
@neonene
typo
5ccf0b8
@neonene neonene closed this Sep 20, 2024
@neonene
neonene deleted the bytoken branch September 20, 2024 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou left review comments
@rhettinger rhettinger Awaiting requested review from rhettinger rhettinger will be requested when the pull request is marked ready for review rhettinger is a code owner
@markshannon markshannon Awaiting requested review from markshannon markshannon will be requested when the pull request is marked ready for review markshannon is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner will be requested when the pull request is marked ready for review AA-Turner is a code owner
@ZeroIntensity ZeroIntensity Awaiting requested review from ZeroIntensity ZeroIntensity will be requested when the pull request is marked ready for review ZeroIntensity is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

topic-C-API

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

@neonene @encukou @erlend-aasland
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.