Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit df90d37

Browse filesBrowse files
committed
Revert memory optimization changes
1 parent e514c65 commit df90d37
Copy full SHA for df90d37

File tree

Expand file treeCollapse file tree

2 files changed

+15
-128
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+15
-128
lines changed

‎Lib/test/test_sys.py

Copy file name to clipboardExpand all lines: Lib/test/test_sys.py
+5-18Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import sysconfig
1313
import test.support
1414
from test import support
15-
from test.support import os_helper, Py_GIL_DISABLED
15+
from test.support import os_helper
1616
from test.support.script_helper import assert_python_ok, assert_python_failure
1717
from test.support import threading_helper
1818
from test.support import import_helper
@@ -1224,7 +1224,7 @@ def test_pystats(self):
12241224
@test.support.cpython_only
12251225
@unittest.skipUnless(hasattr(sys, 'abiflags'), 'need sys.abiflags')
12261226
def test_disable_gil_abi(self):
1227-
self.assertEqual('t' in sys.abiflags, Py_GIL_DISABLED)
1227+
self.assertEqual('t' in sys.abiflags, support.Py_GIL_DISABLED)
12281228

12291229

12301230
@test.support.cpython_only
@@ -1575,22 +1575,9 @@ def get_gen(): yield 1
15751575
check(re.finditer('',''), size('2P'))
15761576
# list
15771577
check(list([]), vsize('Pn'))
1578-
if Py_GIL_DISABLED:
1579-
check(list([1]), vsize('Pn') + 3*self.P)
1580-
check(list([1, 2]), vsize('Pn') + 3*self.P)
1581-
check(list([1, 2, 3]), vsize('Pn') + 3*self.P)
1582-
check(list([1, 2, 3, 4]), vsize('Pn') + 7*self.P)
1583-
check(list([1, 2, 3, 4, 5]), vsize('Pn') + 7*self.P)
1584-
check(list([1, 2, 3, 4, 5, 6]), vsize('Pn') + 7*self.P)
1585-
check(list([1, 2, 3, 4, 5, 6, 7]), vsize('Pn') + 7*self.P)
1586-
else:
1587-
check(list([1]), vsize('Pn') + 2*self.P)
1588-
check(list([1, 2]), vsize('Pn') + 2*self.P)
1589-
check(list([1, 2, 3]), vsize('Pn') + 4*self.P)
1590-
check(list([1, 2, 3, 4]), vsize('Pn') + 4*self.P)
1591-
check(list([1, 2, 3, 4, 5]), vsize('Pn') + 6*self.P)
1592-
check(list([1, 2, 3, 4, 5, 6]), vsize('Pn') + 6*self.P)
1593-
check(list([1, 2, 3, 4, 5, 6, 7]), vsize('Pn') + 8*self.P)
1578+
check(list([1]), vsize('Pn') + 2*self.P)
1579+
check(list([1, 2]), vsize('Pn') + 2*self.P)
1580+
check(list([1, 2, 3]), vsize('Pn') + 4*self.P)
15941581
# sortwrapper (list)
15951582
# XXX
15961583
# cmpwrapper (list)

‎Objects/listobject.c

Copy file name to clipboardExpand all lines: Objects/listobject.c
+10-110Lines changed: 10 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -31,96 +31,8 @@ get_list_freelist(void)
3131
}
3232
#endif
3333

34-
#ifdef Py_GIL_DISABLED
35-
static size_t
36-
list_good_size(Py_ssize_t size)
37-
{
38-
// 4, 8, 16, 24, 32, 40, 48, 64, 80, ...
39-
// NOTE: we add one here so that the rounding accounts for the "allocated"
40-
size_t reqsize = (size_t)size + 1;
41-
if (reqsize <= 4) {
42-
reqsize = 4;
43-
}
44-
else if (reqsize <= 48) {
45-
reqsize = (reqsize + 7) & ~7;
46-
}
47-
else {
48-
reqsize = (reqsize + 15) & ~15;
49-
if (reqsize <= MI_MEDIUM_OBJ_WSIZE_MAX) {
50-
reqsize = mi_good_size(reqsize * sizeof(PyObject *))/sizeof(PyObject*);
51-
}
52-
else {
53-
// ensure geometric spacing for large arrays
54-
size_t shift = mi_bsr(reqsize) - 2;
55-
reqsize = ((reqsize >> shift) + 1) << shift;
56-
}
57-
}
58-
return reqsize - 1;
59-
}
60-
61-
static PyObject**
62-
list_allocate_items(size_t capacity)
63-
{
64-
if (capacity > PY_SSIZE_T_MAX / sizeof(PyObject *) - 1) {
65-
return NULL;
66-
}
67-
PyObject **items = PyMem_Malloc(capacity * sizeof(PyObject *));
68-
return items;
69-
}
70-
71-
/* Ensure ob_item has room for at least newsize elements, and set
72-
* ob_size to newsize. If newsize > ob_size on entry, the content
73-
* of the new slots at exit is undefined heap trash; it's the caller's
74-
* responsibility to overwrite them with sane values.
75-
* The number of allocated elements may grow, shrink, or stay the same.
76-
* Note that self->ob_item may change, and even if newsize is less
77-
* than ob_size on entry.
78-
*/
79-
static int
80-
list_ensure_capacity_slow(PyListObject *self, Py_ssize_t base, Py_ssize_t extra)
81-
{
82-
if (base > PY_SSIZE_T_MAX/(Py_ssize_t)sizeof(PyObject*) - extra) {
83-
PyErr_NoMemory();
84-
return -1;
85-
}
86-
87-
Py_ssize_t reqsize = base + extra;
88-
Py_ssize_t allocated = self->allocated;
89-
if (allocated >= reqsize) {
90-
assert(self->ob_item != NULL || reqsize == 0);
91-
return 0;
92-
}
93-
94-
if (!_Py_IsOwnedByCurrentThread((PyObject *)self)) {
95-
_PyObject_GC_SET_SHARED(self);
96-
}
97-
98-
size_t capacity = list_good_size(reqsize);
99-
PyObject **items = list_allocate_items(capacity);
100-
if (items == NULL) {
101-
PyErr_NoMemory();
102-
return -1;
103-
}
104-
PyObject **old = self->ob_item;
105-
if (self->ob_item) {
106-
memcpy(items, self->ob_item, allocated * sizeof(PyObject*));
107-
}
108-
_Py_atomic_store_ptr_release(&self->ob_item, items);
109-
self->allocated = capacity;
110-
if (old) {
111-
if (_PyObject_GC_IS_SHARED(self)) {
112-
_PyMem_FreeDelayed(old);
113-
}
114-
else {
115-
PyMem_Free(old);
116-
}
117-
}
118-
return 0;
119-
}
120-
#endif
121-
12234
static PyListObject *
123-
list_new(Py_ssize_t size)
35+
list_new_prealloc(Py_ssize_t size)
12436
{
12537
PyListObject *op;
12638
assert(size >= 0);
@@ -145,13 +57,8 @@ list_new(Py_ssize_t size)
14557
op->allocated = 0;
14658
}
14759
else {
148-
#ifdef Py_GIL_DISABLED
149-
size_t capacity = list_good_size(size);
150-
PyObject **items = list_allocate_items(capacity);
151-
#else
15260
size_t capacity = size;
15361
PyObject **items = (PyObject **) PyMem_Calloc(size, sizeof(PyObject *));
154-
#endif
15562
if (items == NULL) {
15663
op->ob_item = NULL;
15764
Py_DECREF(op);
@@ -234,14 +141,8 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
234141
}
235142

236143
static int
237-
list_ensure_capacity(PyListObject *self, Py_ssize_t base, Py_ssize_t extra)
144+
list_preallocate_exact(PyListObject *self, Py_ssize_t size)
238145
{
239-
#ifdef Py_GIL_DISABLED
240-
if (base > self->allocated - extra) {
241-
return list_ensure_capacity_slow(self, base, extra);
242-
}
243-
#else
244-
Py_ssize_t size = extra;
245146
assert(self->ob_item == NULL);
246147
assert(size > 0);
247148

@@ -258,7 +159,6 @@ list_ensure_capacity(PyListObject *self, Py_ssize_t base, Py_ssize_t extra)
258159
}
259160
self->ob_item = items;
260161
self->allocated = size;
261-
#endif
262162
return 0;
263163
}
264164

@@ -297,7 +197,7 @@ PyList_New(Py_ssize_t size)
297197
PyErr_BadInternalCall();
298198
return NULL;
299199
}
300-
PyListObject *op = list_new(size);
200+
PyListObject *op = list_new_prealloc(size);
301201
if (op && op->ob_item) {
302202
PyObject **items = op->ob_item;
303203
for (Py_ssize_t i = 0, n = op->allocated; i < n; i++) {
@@ -446,7 +346,7 @@ PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
446346
int
447347
_PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem)
448348
{
449-
Py_ssize_t len = PyList_GET_SIZE(self);
349+
Py_ssize_t len = Py_SIZE(self);
450350
assert(self->allocated == -1 || self->allocated == len);
451351
if (list_resize(self, len + 1) < 0) {
452352
Py_DECREF(newitem);
@@ -624,7 +524,7 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
624524
if (len <= 0) {
625525
return PyList_New(0);
626526
}
627-
np = (PyListObject *) list_new(len);
527+
np = (PyListObject *) list_new_prealloc(len);
628528
if (np == NULL)
629529
return NULL;
630530

@@ -676,7 +576,7 @@ list_concat_lock_held(PyListObject *a, PyListObject *b)
676576
if (size == 0) {
677577
return PyList_New(0);
678578
}
679-
np = (PyListObject *) list_new(size);
579+
np = (PyListObject *) list_new_prealloc(size);
680580
if (np == NULL) {
681581
return NULL;
682582
}
@@ -726,7 +626,7 @@ list_repeat_lock_held(PyListObject *a, Py_ssize_t n)
726626
return PyErr_NoMemory();
727627
Py_ssize_t output_size = input_size * n;
728628

729-
PyListObject *np = (PyListObject *) list_new(output_size);
629+
PyListObject *np = (PyListObject *) list_new_prealloc(output_size);
730630
if (np == NULL)
731631
return NULL;
732632

@@ -1093,7 +993,7 @@ list_extend_fast(PyListObject *self, PyObject *iterable)
1093993
// an overflow on any relevant platform.
1094994
assert(m < PY_SSIZE_T_MAX - n);
1095995
if (self->ob_item == NULL) {
1096-
if (list_ensure_capacity(self, m, n) < 0) {
996+
if (list_preallocate_exact(self, n) < 0) {
1097997
return -1;
1098998
}
1099999
Py_SET_SIZE(self, n);
@@ -1141,7 +1041,7 @@ list_extend_iter(PyListObject *self, PyObject *iterable)
11411041
*/
11421042
}
11431043
else if (self->ob_item == NULL) {
1144-
if (n && list_ensure_capacity(self, m, n) < 0)
1044+
if (n && list_preallocate_exact(self, n) < 0)
11451045
goto error;
11461046
}
11471047
else {
@@ -3218,7 +3118,7 @@ list_subscript(PyObject* _self, PyObject* item)
32183118
return list_slice(self, start, stop);
32193119
}
32203120
else {
3221-
result = (PyObject *)list_new(slicelength);
3121+
result = (PyObject *)list_new_prealloc(slicelength);
32223122
if (!result) return NULL;
32233123

32243124
src = self->ob_item;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.