_speedups.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <Python.h>
  2. #include <structmember.h>
  3. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  4. #define ABS(X) ((X) < 0 ? (-(X)) : (X))
  5. static unsigned long int
  6. checksum(const unsigned char *data, int len, unsigned long int sum)
  7. {
  8. unsigned long int s1, s2, i;
  9. s1 = sum & 0xffff;
  10. s2 = sum >> 16;
  11. for(i=0; i < len; i++)
  12. {
  13. s1 += data[i] + 1;
  14. s2 += s1;
  15. }
  16. return ((s2 & 0xffff) << 16) | (s1 & 0xffff);
  17. }
  18. static unsigned long int
  19. roll_checksum(unsigned long int sum, unsigned char remove, unsigned char add, int len)
  20. {
  21. unsigned long int s1, s2;
  22. s1 = sum & 0xffff;
  23. s2 = sum >> 16;
  24. s1 -= remove - add;
  25. s2 -= len * (remove + 1) - s1;
  26. return ((s2 & 0xffff) << 16) | (s1 & 0xffff);
  27. }
  28. typedef struct {
  29. PyObject_HEAD
  30. int chunk_size, window_size, i, last, done, buf_size, data_len, seed;
  31. PyObject *chunks, *fd;
  32. unsigned long int sum;
  33. unsigned char *data, add, remove;
  34. } ChunkifyIter;
  35. static PyObject*
  36. ChunkifyIter_iter(PyObject *self)
  37. {
  38. ChunkifyIter *c = (ChunkifyIter *)self;
  39. c->data_len = 0;
  40. c->done = 0;
  41. c->i = 0;
  42. c->sum = 0;
  43. c->last = 0;
  44. Py_INCREF(self);
  45. return self;
  46. }
  47. static void
  48. ChunkifyIter_dealloc(PyObject *self)
  49. {
  50. ChunkifyIter *c = (ChunkifyIter *)self;
  51. Py_DECREF(c->fd);
  52. free(c->data);
  53. self->ob_type->tp_free(self);
  54. }
  55. static PyObject*
  56. ChunkifyIter_iternext(PyObject *self)
  57. {
  58. ChunkifyIter *c = (ChunkifyIter *)self;
  59. int initial = c->window_size;
  60. if(c->done)
  61. {
  62. PyErr_SetNone(PyExc_StopIteration);
  63. return NULL;
  64. }
  65. for(;;)
  66. {
  67. if(c->i == c->buf_size)
  68. {
  69. int diff = c->last + 1 - c->window_size;
  70. assert(diff >= 0);
  71. memmove(c->data, c->data + diff, c->buf_size - diff);
  72. c->i -= diff;
  73. c->last -= diff;
  74. c->data_len -= diff;
  75. assert(c->i >= 0);
  76. assert(c->last >= -1);
  77. assert(c->data_len >= 0);
  78. }
  79. if(c->i == c->data_len)
  80. {
  81. PyObject *data = PyObject_CallMethod(c->fd, "read", "i", c->buf_size - c->data_len);
  82. int n = PyString_Size(data);
  83. memcpy(c->data + c->data_len, PyString_AsString(data), n);
  84. c->data_len += n;
  85. Py_DECREF(data);
  86. }
  87. if(c->i == c->data_len)
  88. {
  89. if(c->last < c->i) {
  90. c->done = 1;
  91. return PyBuffer_FromMemory(c->data + c->last, c->data_len - c->last);
  92. }
  93. PyErr_SetNone(PyExc_StopIteration);
  94. return NULL;
  95. }
  96. if(initial)
  97. {
  98. int bytes = MIN(initial, c->data_len - c->i);
  99. initial -= bytes;
  100. c->sum = checksum(c->data + c->i, bytes, 0);
  101. c->i += bytes;
  102. }
  103. else
  104. {
  105. c->sum = roll_checksum(c->sum,
  106. c->data[c->i - c->window_size],
  107. c->data[c->i],
  108. c->window_size);
  109. c->i++;
  110. }
  111. if((c->sum % c->chunk_size) == c->seed ||
  112. (c->i == c->buf_size && c->last <= c->window_size))
  113. {
  114. int old_last = c->last;
  115. c->last = c->i;
  116. return PyBuffer_FromMemory(c->data + old_last, c->last - old_last);
  117. }
  118. }
  119. PyErr_SetNone(PyExc_StopIteration);
  120. return NULL;
  121. }
  122. static PyTypeObject ChunkifyIterType = {
  123. PyObject_HEAD_INIT(NULL)
  124. 0, /*ob_size*/
  125. "_chunkifier._ChunkifyIter", /*tp_name*/
  126. sizeof(ChunkifyIter), /*tp_basicsize*/
  127. 0, /*tp_itemsize*/
  128. ChunkifyIter_dealloc, /*tp_dealloc*/
  129. 0, /*tp_print*/
  130. 0, /*tp_getattr*/
  131. 0, /*tp_setattr*/
  132. 0, /*tp_compare*/
  133. 0, /*tp_repr*/
  134. 0, /*tp_as_number*/
  135. 0, /*tp_as_sequence*/
  136. 0, /*tp_as_mapping*/
  137. 0, /*tp_hash */
  138. 0, /*tp_call*/
  139. 0, /*tp_str*/
  140. 0, /*tp_getattro*/
  141. 0, /*tp_setattro*/
  142. 0, /*tp_as_buffer*/
  143. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
  144. /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
  145. use tp_iter and tp_iternext fields. */
  146. "", /* tp_doc */
  147. 0, /* tp_traverse */
  148. 0, /* tp_clear */
  149. 0, /* tp_richcompare */
  150. 0, /* tp_weaklistoffset */
  151. ChunkifyIter_iter, /* tp_iter: __iter__() method */
  152. ChunkifyIter_iternext /* tp_iternext: next() method */
  153. };
  154. static PyObject *
  155. chunkify(PyObject *self, PyObject *args)
  156. {
  157. PyObject *fd;
  158. int chunk_size, window_size, seed;
  159. ChunkifyIter *c;
  160. if (!PyArg_ParseTuple(args, "Oiii", &fd, &chunk_size, &window_size, &seed))
  161. {
  162. return NULL;
  163. }
  164. if (!(c = PyObject_New(ChunkifyIter, &ChunkifyIterType)))
  165. {
  166. return NULL;
  167. }
  168. PyObject_Init((PyObject *)c, &ChunkifyIterType);
  169. c->buf_size = 10 * 1024 * 1024;
  170. c->data = malloc(c->buf_size);
  171. c->fd = fd;
  172. c->chunk_size = chunk_size;
  173. c->window_size = window_size;
  174. c->seed = seed % chunk_size;
  175. Py_INCREF(fd);
  176. return (PyObject *)c;
  177. }
  178. static PyMethodDef ChunkifierMethods[] = {
  179. {"chunkify", chunkify, METH_VARARGS, ""},
  180. {NULL, NULL, 0, NULL} /* Sentinel */
  181. };
  182. PyMODINIT_FUNC
  183. init_speedups(void)
  184. {
  185. PyObject* m;
  186. ChunkifyIterType.tp_new = PyType_GenericNew;
  187. if (PyType_Ready(&ChunkifyIterType) < 0) return;
  188. m = Py_InitModule("_speedups", ChunkifierMethods);
  189. }