Skip to content

Commit 4d6288b

Browse files
committed
move dup into macro
1 parent 63c1317 commit 4d6288b

File tree

5 files changed

+66
-175
lines changed

5 files changed

+66
-175
lines changed

Lib/collections/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ def namedtuple(typename, field_names, *, rename=False, defaults=None, module=Non
452452

453453
@classmethod
454454
def _make(cls, iterable):
455+
#print(f'_make: {cls=} {iterable}')
455456
result = tuple_new(cls, iterable)
456457
if _len(result) != num_fields:
457458
raise TypeError(f'Expected {num_fields} arguments, got {len(result)}')

Lib/copy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def copy(x):
107107
weakref.ref, super})
108108
_copy_builtin_containers = frozenset({list, dict, set, bytearray})
109109

110+
import _testcapi
111+
_testcapi.pyobject_enable_deferred_refcount(_copy_atomic_types)
112+
_testcapi.pyobject_enable_deferred_refcount(_copy_builtin_containers)
113+
110114
def deepcopy(x, memo=None):
111115
"""Deep copy operation on arbitrary Python objects.
112116
@@ -168,6 +172,11 @@ def deepcopy(x, memo=None):
168172

169173
_deepcopy_dispatch = d = {}
170174

175+
#import _testcapi
176+
#_testcapi.pyobject_enable_deferred_refcount(_atomic_types)
177+
#_testcapi.pyobject_enable_deferred_refcount(_deepcopy_dispatch)
178+
179+
171180

172181
def _deepcopy_list(x, memo, deepcopy=deepcopy):
173182
y = []

Python/bytecodes.c

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -713,83 +713,53 @@ dummy_func(
713713
// the following _POP_TOP_INT becomes _POP_TOP_NOP. Tier 2 only.
714714
tier2 op(_BINARY_OP_ADD_INT_INPLACE, (left, right -- res, l, r)) {
715715
INT_INPLACE_OP(left, right, left, +, _PyCompactLong_Add);
716-
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
717-
if (_int_inplace_ok) {
718-
res = PyStackRef_DUP(left);
719-
}
720-
else {
721-
res = _int_inplace_res;
722-
}
716+
EXIT_IF(PyStackRef_IsNull(_int_inplace_res));
717+
res = _int_inplace_res;
723718
l = left;
724719
r = right;
725720
INPUTS_DEAD();
726721
}
727722

728723
tier2 op(_BINARY_OP_SUBTRACT_INT_INPLACE, (left, right -- res, l, r)) {
729724
INT_INPLACE_OP(left, right, left, -, _PyCompactLong_Subtract);
730-
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
731-
if (_int_inplace_ok) {
732-
res = PyStackRef_DUP(left);
733-
}
734-
else {
735-
res = _int_inplace_res;
736-
}
725+
EXIT_IF(PyStackRef_IsNull(_int_inplace_res));
726+
res = _int_inplace_res;
737727
l = left;
738728
r = right;
739729
INPUTS_DEAD();
740730
}
741731

742732
tier2 op(_BINARY_OP_MULTIPLY_INT_INPLACE, (left, right -- res, l, r)) {
743733
INT_INPLACE_OP(left, right, left, *, _PyCompactLong_Multiply);
744-
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
745-
if (_int_inplace_ok) {
746-
res = PyStackRef_DUP(left);
747-
}
748-
else {
749-
res = _int_inplace_res;
750-
}
734+
EXIT_IF(PyStackRef_IsNull(_int_inplace_res));
735+
res = _int_inplace_res;
751736
l = left;
752737
r = right;
753738
INPUTS_DEAD();
754739
}
755740

756741
tier2 op(_BINARY_OP_ADD_INT_INPLACE_RIGHT, (left, right -- res, l, r)) {
757742
INT_INPLACE_OP(left, right, right, +, _PyCompactLong_Add);
758-
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
759-
if (_int_inplace_ok) {
760-
res = PyStackRef_DUP(right);
761-
}
762-
else {
763-
res = _int_inplace_res;
764-
}
743+
EXIT_IF(PyStackRef_IsNull(_int_inplace_res));
744+
res = _int_inplace_res;
765745
l = left;
766746
r = right;
767747
INPUTS_DEAD();
768748
}
769749

770750
tier2 op(_BINARY_OP_SUBTRACT_INT_INPLACE_RIGHT, (left, right -- res, l, r)) {
771751
INT_INPLACE_OP(left, right, right, -, _PyCompactLong_Subtract);
772-
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
773-
if (_int_inplace_ok) {
774-
res = PyStackRef_DUP(right);
775-
}
776-
else {
777-
res = _int_inplace_res;
778-
}
752+
EXIT_IF(PyStackRef_IsNull(_int_inplace_res));
753+
res = _int_inplace_res;
779754
l = left;
780755
r = right;
781756
INPUTS_DEAD();
782757
}
783758

784759
tier2 op(_BINARY_OP_MULTIPLY_INT_INPLACE_RIGHT, (left, right -- res, l, r)) {
785760
INT_INPLACE_OP(left, right, right, *, _PyCompactLong_Multiply);
786-
EXIT_IF(!_int_inplace_ok && PyStackRef_IsNull(_int_inplace_res));
787-
if (_int_inplace_ok) {
788-
res = PyStackRef_DUP(right);
789-
}
790-
else {
791-
res = _int_inplace_res;
792-
}
761+
EXIT_IF(PyStackRef_IsNull(_int_inplace_res));
762+
res = _int_inplace_res;
793763
l = left;
794764
r = right;
795765
INPUTS_DEAD();

Python/ceval_macros.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -568,13 +568,13 @@ gen_try_set_executing(PyGenObject *gen)
568568
// cached small int singleton. We check _Py_IsImmortal on TARGET
569569
// to decide whether inplace mutation is safe.
570570
//
571-
// After the macro:
572-
// _int_inplace_ok = 1: mutated TARGET in place (caller uses TARGET as res)
573-
// _int_inplace_ok = 0: _int_inplace_res holds the fallback result
574-
// (may be NULL on allocation failure)
571+
// After the macro, _int_inplace_res holds the result (may be NULL
572+
// on allocation failure). On success, TARGET was mutated in place
573+
// and _int_inplace_res is a DUP'd reference to it. On fallback
574+
// (small int target, small int result, or overflow), _int_inplace_res
575+
// is from FUNC (_PyCompactLong_Add etc.).
575576
// FUNC is the fallback function (_PyCompactLong_Add etc.)
576577
#define INT_INPLACE_OP(left, right, TARGET, OP, FUNC) \
577-
int _int_inplace_ok = 0; \
578578
_PyStackRef _int_inplace_res = PyStackRef_NULL; \
579579
do { \
580580
PyObject *_target_o = PyStackRef_AsPyObjectBorrow(TARGET); \
@@ -595,10 +595,11 @@ gen_try_set_executing(PyGenObject *gen)
595595
(PyLongObject *)_target_o, _result < 0 ? -1 : 1, 1); \
596596
((PyLongObject *)_target_o)->long_value.ob_digit[0] = \
597597
(digit)(_result < 0 ? -_result : _result); \
598-
_int_inplace_ok = 1; \
598+
_int_inplace_res = PyStackRef_DUP(TARGET); \
599+
break; \
599600
} \
600601
} while (0); \
601-
if (!_int_inplace_ok) { \
602+
if (PyStackRef_IsNull(_int_inplace_res)) { \
602603
_int_inplace_res = FUNC( \
603604
(PyLongObject *)PyStackRef_AsPyObjectBorrow(left), \
604605
(PyLongObject *)PyStackRef_AsPyObjectBorrow(right)); \

0 commit comments

Comments
 (0)