Skip to content

Commit 2ef06d4

Browse files
authored
bpo-46131: add fastpath for PyFloat_Check() (#30200)
1 parent aeb9ef4 commit 2ef06d4

File tree

7 files changed

+23
-0
lines changed

7 files changed

+23
-0
lines changed

Doc/c-api/typeobj.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ and :c:type:`PyType_Type` effectively act as defaults.)
11451145
.. XXX Document more flags here?
11461146
11471147
1148+
.. data:: Py_TPFLAGS_FLOAT_SUBCLASS
11481149
.. data:: Py_TPFLAGS_LONG_SUBCLASS
11491150
.. data:: Py_TPFLAGS_LIST_SUBCLASS
11501151
.. data:: Py_TPFLAGS_TUPLE_SUBCLASS

Include/floatobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ extern "C" {
1414
PyAPI_DATA(PyTypeObject) PyFloat_Type;
1515

1616
#define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type)
17+
#define PyFloat_Check(op) \
18+
PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_FLOAT_SUBCLASS)
1719
#define PyFloat_CheckExact(op) Py_IS_TYPE(op, &PyFloat_Type)
1820

1921
#ifdef Py_NAN

Include/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ given type object has a specified feature.
397397
#define _Py_TPFLAGS_MATCH_SELF (1UL << 22)
398398

399399
/* These flags are used to determine if a type is a subclass. */
400+
#define Py_TPFLAGS_FLOAT_SUBCLASS (1UL << 23)
400401
#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
401402
#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
402403
#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add a fast path for ``PyFloat_Check`` via a ``Py_TPFLAGS_FLOAT_SUBCLASS``
2+
flag.

Objects/floatobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,7 @@ PyTypeObject PyFloat_Type = {
19591959
0, /* tp_setattro */
19601960
0, /* tp_as_buffer */
19611961
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1962+
Py_TPFLAGS_FLOAT_SUBCLASS |
19621963
_Py_TPFLAGS_MATCH_SELF, /* tp_flags */
19631964
float_new__doc__, /* tp_doc */
19641965
0, /* tp_traverse */

Objects/typeobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5783,6 +5783,9 @@ inherit_special(PyTypeObject *type, PyTypeObject *base)
57835783
else if (PyType_IsSubtype(base, &PyDict_Type)) {
57845784
type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
57855785
}
5786+
else if (PyType_IsSubtype(base, &PyFloat_Type)) {
5787+
type->tp_flags |= Py_TPFLAGS_FLOAT_SUBCLASS;
5788+
}
57865789
if (PyType_HasFeature(base, _Py_TPFLAGS_MATCH_SELF)) {
57875790
type->tp_flags |= _Py_TPFLAGS_MATCH_SELF;
57885791
}

Tools/gdb/libpython.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def _sizeof_void_p():
8585

8686
Py_TPFLAGS_MANAGED_DICT = (1 << 4)
8787
Py_TPFLAGS_HEAPTYPE = (1 << 9)
88+
Py_TPFLAGS_FLOAT_SUBCLASS = (1 << 23)
8889
Py_TPFLAGS_LONG_SUBCLASS = (1 << 24)
8990
Py_TPFLAGS_LIST_SUBCLASS = (1 << 25)
9091
Py_TPFLAGS_TUPLE_SUBCLASS = (1 << 26)
@@ -379,6 +380,8 @@ def subclass_from_type(cls, t):
379380
if tp_flags & Py_TPFLAGS_HEAPTYPE:
380381
return HeapTypeObjectPtr
381382

383+
if tp_flags & Py_TPFLAGS_FLOAT_SUBCLASS:
384+
return PyFloatObjectPtr
382385
if tp_flags & Py_TPFLAGS_LONG_SUBCLASS:
383386
return PyLongObjectPtr
384387
if tp_flags & Py_TPFLAGS_LIST_SUBCLASS:
@@ -910,6 +913,16 @@ class PyNoneStructPtr(PyObjectPtr):
910913
def proxyval(self, visited):
911914
return None
912915

916+
class PyFloatObjectPtr(PyObjectPtr):
917+
_typename = 'PyFloatObject'
918+
919+
def proxyval(self, visited):
920+
return self.field('ob_fval')
921+
922+
def write_repr(self, out, visited):
923+
proxy = self.proxyval(visited)
924+
out.write("%s" % proxy)
925+
913926
class PyFrameObjectPtr(PyObjectPtr):
914927
_typename = 'PyFrameObject'
915928

0 commit comments

Comments
 (0)