Skip to content

Commit 545ef0d

Browse files
committed
Improved performance of memory tools (approximately 2.5 times faster).
1 parent a74aa97 commit 545ef0d

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/core/modules/memory/memory_tools.h

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ CPointer* ExtractPointer(object oPtr);
4141
// ============================================================================
4242
inline object GetObjectPointer(object obj)
4343
{
44-
if (!PyObject_HasAttrString(obj.ptr(), GET_PTR_NAME))
44+
object _ptr;
45+
try
46+
{
47+
_ptr = obj.attr(GET_PTR_NAME);
48+
}
49+
catch (...)
50+
{
4551
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to retrieve a pointer of this object.");
52+
}
4653

47-
return obj.attr(GET_PTR_NAME)();
54+
return _ptr();
4855
}
4956

5057

@@ -53,14 +60,21 @@ inline object GetObjectPointer(object obj)
5360
// ============================================================================
5461
inline object MakeObject(object cls, object oPtr)
5562
{
56-
if (!PyObject_HasAttrString(cls.ptr(), GET_OBJ_NAME))
63+
object _obj;
64+
try
65+
{
66+
_obj = cls.attr(GET_OBJ_NAME);
67+
}
68+
catch (...)
69+
{
5770
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to make an object using this class.");
71+
}
5872

5973
CPointer* pPtr = ExtractPointer(oPtr);
6074
if (!pPtr->IsValid())
6175
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Pointer is NULL.");
6276

63-
return cls.attr(GET_OBJ_NAME)(pPtr);
77+
return _obj(pPtr);
6478
}
6579

6680

@@ -69,10 +83,17 @@ inline object MakeObject(object cls, object oPtr)
6983
// ============================================================================
7084
inline object GetSize(object cls)
7185
{
72-
if (!PyObject_HasAttrString(cls.ptr(), GET_SIZE_NAME))
86+
object _size;
87+
try
88+
{
89+
_size = cls.attr(GET_SIZE_NAME);
90+
}
91+
catch (...)
92+
{
7393
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unable to retrieve the size of this class.");
94+
}
7495

75-
return cls.attr(GET_SIZE_NAME);
96+
return _size;
7697
}
7798

7899

0 commit comments

Comments
 (0)