mirror of
https://github.com/python/cpython.git
synced 2025-08-20 00:32:12 +00:00
Backport r61458:
Issue 2321: reduce memory usage (increase the memory that is returned to the system) by using pymalloc for the data of unicode objects.
This commit is contained in:
parent
a6f5bf2387
commit
b3635f99a2
2 changed files with 17 additions and 11 deletions
|
@ -11,6 +11,8 @@ What's New in Python 2.5.3?
|
||||||
|
|
||||||
Core and builtins
|
Core and builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
- Issue #2321: use pymalloc for unicode object string data to reduce
|
||||||
|
memory usage in some circumstances.
|
||||||
|
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
|
@ -200,7 +200,8 @@ int unicode_resize(register PyUnicodeObject *unicode,
|
||||||
it contains). */
|
it contains). */
|
||||||
|
|
||||||
oldstr = unicode->str;
|
oldstr = unicode->str;
|
||||||
PyMem_RESIZE(unicode->str, Py_UNICODE, length + 1);
|
unicode->str = PyObject_REALLOC(unicode->str,
|
||||||
|
sizeof(Py_UNICODE) * (length + 1));
|
||||||
if (!unicode->str) {
|
if (!unicode->str) {
|
||||||
unicode->str = (Py_UNICODE *)oldstr;
|
unicode->str = (Py_UNICODE *)oldstr;
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
|
@ -249,20 +250,23 @@ PyUnicodeObject *_PyUnicode_New(Py_ssize_t length)
|
||||||
never downsize it. */
|
never downsize it. */
|
||||||
if ((unicode->length < length) &&
|
if ((unicode->length < length) &&
|
||||||
unicode_resize(unicode, length) < 0) {
|
unicode_resize(unicode, length) < 0) {
|
||||||
PyMem_DEL(unicode->str);
|
PyObject_DEL(unicode->str);
|
||||||
goto onError;
|
goto onError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
unicode->str = PyMem_NEW(Py_UNICODE, length + 1);
|
size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
|
||||||
|
unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);
|
||||||
}
|
}
|
||||||
PyObject_INIT(unicode, &PyUnicode_Type);
|
PyObject_INIT(unicode, &PyUnicode_Type);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
size_t new_size;
|
||||||
unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
|
unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
|
||||||
if (unicode == NULL)
|
if (unicode == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
unicode->str = PyMem_NEW(Py_UNICODE, length + 1);
|
new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
|
||||||
|
unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!unicode->str) {
|
if (!unicode->str) {
|
||||||
|
@ -296,7 +300,7 @@ void unicode_dealloc(register PyUnicodeObject *unicode)
|
||||||
unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) {
|
unicode_freelist_size < MAX_UNICODE_FREELIST_SIZE) {
|
||||||
/* Keep-Alive optimization */
|
/* Keep-Alive optimization */
|
||||||
if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
|
if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
|
||||||
PyMem_DEL(unicode->str);
|
PyObject_DEL(unicode->str);
|
||||||
unicode->str = NULL;
|
unicode->str = NULL;
|
||||||
unicode->length = 0;
|
unicode->length = 0;
|
||||||
}
|
}
|
||||||
|
@ -310,7 +314,7 @@ void unicode_dealloc(register PyUnicodeObject *unicode)
|
||||||
unicode_freelist_size++;
|
unicode_freelist_size++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
PyMem_DEL(unicode->str);
|
PyObject_DEL(unicode->str);
|
||||||
Py_XDECREF(unicode->defenc);
|
Py_XDECREF(unicode->defenc);
|
||||||
unicode->ob_type->tp_free((PyObject *)unicode);
|
unicode->ob_type->tp_free((PyObject *)unicode);
|
||||||
}
|
}
|
||||||
|
@ -7129,7 +7133,7 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item)
|
||||||
return PyUnicode_FromUnicode(NULL, 0);
|
return PyUnicode_FromUnicode(NULL, 0);
|
||||||
} else {
|
} else {
|
||||||
source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
|
source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
|
||||||
result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength*
|
result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength*
|
||||||
sizeof(Py_UNICODE));
|
sizeof(Py_UNICODE));
|
||||||
|
|
||||||
if (result_buf == NULL)
|
if (result_buf == NULL)
|
||||||
|
@ -7140,7 +7144,7 @@ unicode_subscript(PyUnicodeObject* self, PyObject* item)
|
||||||
}
|
}
|
||||||
|
|
||||||
result = PyUnicode_FromUnicode(result_buf, slicelength);
|
result = PyUnicode_FromUnicode(result_buf, slicelength);
|
||||||
PyMem_FREE(result_buf);
|
PyObject_FREE(result_buf);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -7948,7 +7952,7 @@ unicode_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||||
Py_DECREF(tmp);
|
Py_DECREF(tmp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
pnew->str = PyMem_NEW(Py_UNICODE, n+1);
|
pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1));
|
||||||
if (pnew->str == NULL) {
|
if (pnew->str == NULL) {
|
||||||
_Py_ForgetReference((PyObject *)pnew);
|
_Py_ForgetReference((PyObject *)pnew);
|
||||||
PyObject_Del(pnew);
|
PyObject_Del(pnew);
|
||||||
|
@ -8075,7 +8079,7 @@ _PyUnicode_Fini(void)
|
||||||
PyUnicodeObject *v = u;
|
PyUnicodeObject *v = u;
|
||||||
u = *(PyUnicodeObject **)u;
|
u = *(PyUnicodeObject **)u;
|
||||||
if (v->str)
|
if (v->str)
|
||||||
PyMem_DEL(v->str);
|
PyObject_DEL(v->str);
|
||||||
Py_XDECREF(v->defenc);
|
Py_XDECREF(v->defenc);
|
||||||
PyObject_Del(v);
|
PyObject_Del(v);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue