1 | n/a | /* Accumulator struct implementation */ |
---|
2 | n/a | |
---|
3 | n/a | #include "Python.h" |
---|
4 | n/a | #include "accu.h" |
---|
5 | n/a | |
---|
6 | n/a | static PyObject * |
---|
7 | n/a | join_list_unicode(PyObject *lst) |
---|
8 | n/a | { |
---|
9 | n/a | /* return ''.join(lst) */ |
---|
10 | n/a | PyObject *sep, *ret; |
---|
11 | n/a | sep = PyUnicode_FromStringAndSize("", 0); |
---|
12 | n/a | ret = PyUnicode_Join(sep, lst); |
---|
13 | n/a | Py_DECREF(sep); |
---|
14 | n/a | return ret; |
---|
15 | n/a | } |
---|
16 | n/a | |
---|
17 | n/a | int |
---|
18 | n/a | _PyAccu_Init(_PyAccu *acc) |
---|
19 | n/a | { |
---|
20 | n/a | /* Lazily allocated */ |
---|
21 | n/a | acc->large = NULL; |
---|
22 | n/a | acc->small = PyList_New(0); |
---|
23 | n/a | if (acc->small == NULL) |
---|
24 | n/a | return -1; |
---|
25 | n/a | return 0; |
---|
26 | n/a | } |
---|
27 | n/a | |
---|
28 | n/a | static int |
---|
29 | n/a | flush_accumulator(_PyAccu *acc) |
---|
30 | n/a | { |
---|
31 | n/a | Py_ssize_t nsmall = PyList_GET_SIZE(acc->small); |
---|
32 | n/a | if (nsmall) { |
---|
33 | n/a | int ret; |
---|
34 | n/a | PyObject *joined; |
---|
35 | n/a | if (acc->large == NULL) { |
---|
36 | n/a | acc->large = PyList_New(0); |
---|
37 | n/a | if (acc->large == NULL) |
---|
38 | n/a | return -1; |
---|
39 | n/a | } |
---|
40 | n/a | joined = join_list_unicode(acc->small); |
---|
41 | n/a | if (joined == NULL) |
---|
42 | n/a | return -1; |
---|
43 | n/a | if (PyList_SetSlice(acc->small, 0, nsmall, NULL)) { |
---|
44 | n/a | Py_DECREF(joined); |
---|
45 | n/a | return -1; |
---|
46 | n/a | } |
---|
47 | n/a | ret = PyList_Append(acc->large, joined); |
---|
48 | n/a | Py_DECREF(joined); |
---|
49 | n/a | return ret; |
---|
50 | n/a | } |
---|
51 | n/a | return 0; |
---|
52 | n/a | } |
---|
53 | n/a | |
---|
54 | n/a | int |
---|
55 | n/a | _PyAccu_Accumulate(_PyAccu *acc, PyObject *unicode) |
---|
56 | n/a | { |
---|
57 | n/a | Py_ssize_t nsmall; |
---|
58 | n/a | assert(PyUnicode_Check(unicode)); |
---|
59 | n/a | |
---|
60 | n/a | if (PyList_Append(acc->small, unicode)) |
---|
61 | n/a | return -1; |
---|
62 | n/a | nsmall = PyList_GET_SIZE(acc->small); |
---|
63 | n/a | /* Each item in a list of unicode objects has an overhead (in 64-bit |
---|
64 | n/a | * builds) of: |
---|
65 | n/a | * - 8 bytes for the list slot |
---|
66 | n/a | * - 56 bytes for the header of the unicode object |
---|
67 | n/a | * that is, 64 bytes. 100000 such objects waste more than 6MB |
---|
68 | n/a | * compared to a single concatenated string. |
---|
69 | n/a | */ |
---|
70 | n/a | if (nsmall < 100000) |
---|
71 | n/a | return 0; |
---|
72 | n/a | return flush_accumulator(acc); |
---|
73 | n/a | } |
---|
74 | n/a | |
---|
75 | n/a | PyObject * |
---|
76 | n/a | _PyAccu_FinishAsList(_PyAccu *acc) |
---|
77 | n/a | { |
---|
78 | n/a | int ret; |
---|
79 | n/a | PyObject *res; |
---|
80 | n/a | |
---|
81 | n/a | ret = flush_accumulator(acc); |
---|
82 | n/a | Py_CLEAR(acc->small); |
---|
83 | n/a | if (ret) { |
---|
84 | n/a | Py_CLEAR(acc->large); |
---|
85 | n/a | return NULL; |
---|
86 | n/a | } |
---|
87 | n/a | res = acc->large; |
---|
88 | n/a | acc->large = NULL; |
---|
89 | n/a | return res; |
---|
90 | n/a | } |
---|
91 | n/a | |
---|
92 | n/a | PyObject * |
---|
93 | n/a | _PyAccu_Finish(_PyAccu *acc) |
---|
94 | n/a | { |
---|
95 | n/a | PyObject *list, *res; |
---|
96 | n/a | if (acc->large == NULL) { |
---|
97 | n/a | list = acc->small; |
---|
98 | n/a | acc->small = NULL; |
---|
99 | n/a | } |
---|
100 | n/a | else { |
---|
101 | n/a | list = _PyAccu_FinishAsList(acc); |
---|
102 | n/a | if (!list) |
---|
103 | n/a | return NULL; |
---|
104 | n/a | } |
---|
105 | n/a | res = join_list_unicode(list); |
---|
106 | n/a | Py_DECREF(list); |
---|
107 | n/a | return res; |
---|
108 | n/a | } |
---|
109 | n/a | |
---|
110 | n/a | void |
---|
111 | n/a | _PyAccu_Destroy(_PyAccu *acc) |
---|
112 | n/a | { |
---|
113 | n/a | Py_CLEAR(acc->small); |
---|
114 | n/a | Py_CLEAR(acc->large); |
---|
115 | n/a | } |
---|