You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.4 KiB

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