-
-
Notifications
You must be signed in to change notification settings - Fork 32k
GH-132508: Use tagged integers on the evaluation stack for the last instruction offset #132545
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ba30e9a
Use tagged integers on the evaluation stack for lasti
markshannon 1d223c3
Add support for Py_STACKREF_DEBUG
markshannon 246199b
Add news
markshannon b1b1252
Fix dump_item
markshannon 8d3c3ea
Clarify division between pre-defined stackref indexes and others
markshannon aa4ddf0
Cast before shifting
markshannon e223e36
Integer tagging doesn't escape
markshannon 9e11cc7
Push assert up to caller
markshannon 79ed469
Avoid side effects in macro
markshannon b491544
Remove debug code
markshannon f6126b1
Handle negative ints properly in Py_STACKREF_DEBUG build
markshannon 1b4aba6
Brandt's fix to the stencils
markshannon d84cd2d
Put Brandt's patch in the right place :)
markshannon fb89cd9
Replace is_tagged_int with PyStackRef_IsTaggedInt
markshannon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -63,11 +63,13 @@ extern void _Py_stackref_associate(PyInterpreterState *interp, PyObject *obj, _P | |||||
|
||||||
static const _PyStackRef PyStackRef_NULL = { .index = 0 }; | ||||||
|
||||||
#define PyStackRef_None ((_PyStackRef){ .index = 1 } ) | ||||||
#define PyStackRef_False ((_PyStackRef){ .index = 2 }) | ||||||
#define PyStackRef_True ((_PyStackRef){ .index = 3 }) | ||||||
// Use the first 3 even numbers for None, True and False. | ||||||
// Odd numbers are reserved for (tagged) integers | ||||||
#define PyStackRef_None ((_PyStackRef){ .index = 2 } ) | ||||||
#define PyStackRef_False ((_PyStackRef){ .index = 4 }) | ||||||
#define PyStackRef_True ((_PyStackRef){ .index = 6 }) | ||||||
|
||||||
#define LAST_PREDEFINED_STACKREF_INDEX 3 | ||||||
#define INITIAL_STACKREF_INDEX 8 | ||||||
|
||||||
static inline int | ||||||
PyStackRef_IsNull(_PyStackRef ref) | ||||||
|
@@ -96,6 +98,7 @@ PyStackRef_IsNone(_PyStackRef ref) | |||||
static inline PyObject * | ||||||
_PyStackRef_AsPyObjectBorrow(_PyStackRef ref, const char *filename, int linenumber) | ||||||
{ | ||||||
assert((ref.index & 1) == 0); | ||||||
iritkatriel marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
_Py_stackref_record_borrow(ref, filename, linenumber); | ||||||
return _Py_stackref_get_object(ref); | ||||||
} | ||||||
|
@@ -132,31 +135,45 @@ _PyStackRef_FromPyObjectImmortal(PyObject *obj, const char *filename, int linenu | |||||
} | ||||||
#define PyStackRef_FromPyObjectImmortal(obj) _PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj), __FILE__, __LINE__) | ||||||
|
||||||
static inline bool | ||||||
is_tagged_int(_PyStackRef ref) | ||||||
{ | ||||||
return (ref.index & 1) == 1; | ||||||
markshannon marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
static inline void | ||||||
_PyStackRef_CLOSE(_PyStackRef ref, const char *filename, int linenumber) | ||||||
{ | ||||||
if (is_tagged_int(ref)) { | ||||||
return; | ||||||
} | ||||||
PyObject *obj = _Py_stackref_close(ref, filename, linenumber); | ||||||
Py_DECREF(obj); | ||||||
} | ||||||
#define PyStackRef_CLOSE(REF) _PyStackRef_CLOSE((REF), __FILE__, __LINE__) | ||||||
|
||||||
|
||||||
static inline void | ||||||
_PyStackRef_XCLOSE(_PyStackRef ref, const char *filename, int linenumber) | ||||||
{ | ||||||
if (PyStackRef_IsNull(ref)) { | ||||||
return; | ||||||
} | ||||||
PyObject *obj = _Py_stackref_close(ref, filename, linenumber); | ||||||
Py_DECREF(obj); | ||||||
_PyStackRef_CLOSE(ref, filename, linenumber); | ||||||
} | ||||||
#define PyStackRef_XCLOSE(REF) _PyStackRef_XCLOSE((REF), __FILE__, __LINE__) | ||||||
|
||||||
static inline _PyStackRef | ||||||
_PyStackRef_DUP(_PyStackRef ref, const char *filename, int linenumber) | ||||||
{ | ||||||
PyObject *obj = _Py_stackref_get_object(ref); | ||||||
Py_INCREF(obj); | ||||||
return _Py_stackref_create(obj, filename, linenumber); | ||||||
if (ref.index & 1) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return ref; | ||||||
} | ||||||
else { | ||||||
PyObject *obj = _Py_stackref_get_object(ref); | ||||||
Py_INCREF(obj); | ||||||
return _Py_stackref_create(obj, filename, linenumber); | ||||||
} | ||||||
} | ||||||
#define PyStackRef_DUP(REF) _PyStackRef_DUP(REF, __FILE__, __LINE__) | ||||||
|
||||||
|
@@ -210,8 +227,39 @@ _PyStackRef_FromPyObjectNewMortal(PyObject *obj, const char *filename, int linen | |||||
|
||||||
extern int PyStackRef_Is(_PyStackRef a, _PyStackRef b); | ||||||
|
||||||
extern bool PyStackRef_IsTaggedInt(_PyStackRef ref); | ||||||
|
||||||
extern intptr_t PyStackRef_UntagInt(_PyStackRef ref); | ||||||
|
||||||
extern _PyStackRef PyStackRef_TagInt(intptr_t i); | ||||||
|
||||||
extern bool | ||||||
PyStackRef_IsNullOrInt(_PyStackRef ref); | ||||||
|
||||||
#else | ||||||
|
||||||
#define Py_INT_TAG 3 | ||||||
|
||||||
static inline bool | ||||||
PyStackRef_IsTaggedInt(_PyStackRef i) | ||||||
{ | ||||||
return (i.bits & Py_INT_TAG) == Py_INT_TAG; | ||||||
} | ||||||
|
||||||
static inline _PyStackRef | ||||||
PyStackRef_TagInt(intptr_t i) | ||||||
{ | ||||||
assert(Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, (i << 2), 2) == i); | ||||||
Fidget-Spinner marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return (_PyStackRef){ .bits = ((((uintptr_t)i) << 2) | Py_INT_TAG) }; | ||||||
} | ||||||
|
||||||
static inline intptr_t | ||||||
PyStackRef_UntagInt(_PyStackRef i) | ||||||
{ | ||||||
assert((i.bits & Py_INT_TAG) == Py_INT_TAG); | ||||||
return Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, i.bits, 2); | ||||||
} | ||||||
|
||||||
|
||||||
#ifdef Py_GIL_DISABLED | ||||||
|
||||||
|
@@ -232,6 +280,8 @@ static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED}; | |||||
#define PyStackRef_IsTrue(ref) (PyStackRef_AsPyObjectBorrow(ref) == Py_True) | ||||||
#define PyStackRef_IsFalse(ref) (PyStackRef_AsPyObjectBorrow(ref) == Py_False) | ||||||
|
||||||
#define PyStackRef_IsNullOrInt(stackref) (PyStackRef_IsNull(stackref) || PyStackRef_IsTaggedInt(stackref)) | ||||||
|
||||||
static inline PyObject * | ||||||
PyStackRef_AsPyObjectBorrow(_PyStackRef stackref) | ||||||
{ | ||||||
|
@@ -451,6 +501,7 @@ PyStackRef_RefcountOnObject(_PyStackRef ref) | |||||
static inline PyObject * | ||||||
PyStackRef_AsPyObjectBorrow(_PyStackRef ref) | ||||||
{ | ||||||
assert(!PyStackRef_IsTaggedInt(ref)); | ||||||
return BITS_TO_PTR_MASKED(ref); | ||||||
} | ||||||
|
||||||
|
@@ -587,6 +638,12 @@ PyStackRef_CLOSE(_PyStackRef ref) | |||||
} | ||||||
#endif | ||||||
|
||||||
static inline bool | ||||||
PyStackRef_IsNullOrInt(_PyStackRef ref) | ||||||
{ | ||||||
return PyStackRef_IsNull(ref) || PyStackRef_IsTaggedInt(ref); | ||||||
} | ||||||
|
||||||
static inline void | ||||||
PyStackRef_CLOSE_SPECIALIZED(_PyStackRef ref, destructor destruct) | ||||||
{ | ||||||
|
@@ -726,7 +783,7 @@ _Py_TryXGetStackRef(PyObject **src, _PyStackRef *out) | |||||
// Like Py_VISIT but for _PyStackRef fields | ||||||
#define _Py_VISIT_STACKREF(ref) \ | ||||||
do { \ | ||||||
if (!PyStackRef_IsNull(ref)) { \ | ||||||
if (!PyStackRef_IsNullOrInt(ref)) { \ | ||||||
int vret = _PyGC_VisitStackRef(&(ref), visit, arg); \ | ||||||
if (vret) \ | ||||||
return vret; \ | ||||||
|
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Core_and_Builtins/2025-04-15-10-09-49.gh-issue-132508.zVe3iI.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Uses tagged integers on the evaluation stack to represent the instruction | ||
offsets when reraising an exception. This avoids the need to box the integer | ||
which could fail in low memory conditions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.