1
/* Accumulator struct implementation */
6
join_list_unicode(PyObject *lst)
8
/* return ''.join(lst) */
10
sep = PyUnicode_FromStringAndSize("", 0);
11
ret = PyUnicode_Join(sep, lst);
17
_PyAccu_Init(_PyAccu *acc)
19
/* Lazily allocated */
21
acc->small = PyList_New(0);
22
if (acc->small == NULL)
28
flush_accumulator(_PyAccu *acc)
30
Py_ssize_t nsmall = PyList_GET_SIZE(acc->small);
34
if (acc->large == NULL) {
35
acc->large = PyList_New(0);
36
if (acc->large == NULL)
39
joined = join_list_unicode(acc->small);
42
if (PyList_SetSlice(acc->small, 0, nsmall, NULL)) {
46
ret = PyList_Append(acc->large, joined);
54
_PyAccu_Accumulate(_PyAccu *acc, PyObject *unicode)
57
assert(PyUnicode_Check(unicode));
59
if (PyList_Append(acc->small, unicode))
61
nsmall = PyList_GET_SIZE(acc->small);
62
/* Each item in a list of unicode objects has an overhead (in 64-bit
64
* - 8 bytes for the list slot
65
* - 56 bytes for the header of the unicode object
66
* that is, 64 bytes. 100000 such objects waste more than 6MB
67
* compared to a single concatenated string.
71
return flush_accumulator(acc);
75
_PyAccu_FinishAsList(_PyAccu *acc)
80
ret = flush_accumulator(acc);
92
_PyAccu_Finish(_PyAccu *acc)
95
if (acc->large == NULL) {
100
list = _PyAccu_FinishAsList(acc);
104
res = join_list_unicode(list);
110
_PyAccu_Destroy(_PyAccu *acc)
112
Py_CLEAR(acc->small);
113
Py_CLEAR(acc->large);