_speedups.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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, last, done, buf_size, seed, remaining, position;
  31. PyObject *chunks, *fd;
  32. unsigned char *data;
  33. } ChunkifyIter;
  34. static PyObject*
  35. ChunkifyIter_iter(PyObject *self)
  36. {
  37. ChunkifyIter *c = (ChunkifyIter *)self;
  38. c->remaining = 0;
  39. c->position = 0;
  40. c->done = 0;
  41. c->last = 0;
  42. Py_INCREF(self);
  43. return self;
  44. }
  45. static void
  46. ChunkifyIter_dealloc(PyObject *self)
  47. {
  48. ChunkifyIter *c = (ChunkifyIter *)self;
  49. Py_DECREF(c->fd);
  50. free(c->data);
  51. self->ob_type->tp_free(self);
  52. }
  53. static void
  54. ChunkifyIter_fill(PyObject *self)
  55. {
  56. ChunkifyIter *c = (ChunkifyIter *)self;
  57. memmove(c->data, c->data + c->last, c->position + c->remaining - c->last);
  58. c->position -= c->last;
  59. c->last = 0;
  60. PyObject *data = PyObject_CallMethod(c->fd, "read", "i", c->buf_size - c->position - c->remaining);
  61. int n = PyString_Size(data);
  62. memcpy(c->data + c->position + c->remaining, PyString_AsString(data), n);
  63. c->remaining += n;
  64. Py_DECREF(data);
  65. }
  66. static PyObject*
  67. ChunkifyIter_iternext(PyObject *self)
  68. {
  69. ChunkifyIter *c = (ChunkifyIter *)self;
  70. unsigned long int sum;
  71. if(c->done) {
  72. PyErr_SetNone(PyExc_StopIteration);
  73. return NULL;
  74. }
  75. if(c->remaining <= c->window_size) {
  76. ChunkifyIter_fill(self);
  77. }
  78. if(c->remaining < c->window_size) {
  79. c->done = 1;
  80. return PyBuffer_FromMemory(c->data + c->position, c->remaining);
  81. }
  82. sum = checksum(c->data + c->position, c->window_size, 0);
  83. c->remaining -= c->window_size;
  84. c->position += c->window_size;
  85. while(c->remaining && (sum & 0xffff) != c->seed) {
  86. sum = roll_checksum(sum, c->data[c->position - c->window_size],
  87. c->data[c->position],
  88. c->window_size);
  89. c->position++;
  90. c->remaining--;
  91. if(c->remaining == 0) {
  92. ChunkifyIter_fill(self);
  93. }
  94. }
  95. int old_last = c->last;
  96. c->last = c->position;
  97. return PyBuffer_FromMemory(c->data + old_last, c->last - old_last);
  98. }
  99. static PyTypeObject ChunkifyIterType = {
  100. PyObject_HEAD_INIT(NULL)
  101. 0, /*ob_size*/
  102. "_chunkifier._ChunkifyIter", /*tp_name*/
  103. sizeof(ChunkifyIter), /*tp_basicsize*/
  104. 0, /*tp_itemsize*/
  105. ChunkifyIter_dealloc, /*tp_dealloc*/
  106. 0, /*tp_print*/
  107. 0, /*tp_getattr*/
  108. 0, /*tp_setattr*/
  109. 0, /*tp_compare*/
  110. 0, /*tp_repr*/
  111. 0, /*tp_as_number*/
  112. 0, /*tp_as_sequence*/
  113. 0, /*tp_as_mapping*/
  114. 0, /*tp_hash */
  115. 0, /*tp_call*/
  116. 0, /*tp_str*/
  117. 0, /*tp_getattro*/
  118. 0, /*tp_setattro*/
  119. 0, /*tp_as_buffer*/
  120. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
  121. /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
  122. use tp_iter and tp_iternext fields. */
  123. "", /* tp_doc */
  124. 0, /* tp_traverse */
  125. 0, /* tp_clear */
  126. 0, /* tp_richcompare */
  127. 0, /* tp_weaklistoffset */
  128. ChunkifyIter_iter, /* tp_iter: __iter__() method */
  129. ChunkifyIter_iternext /* tp_iternext: next() method */
  130. };
  131. static PyObject *
  132. chunkify(PyObject *self, PyObject *args)
  133. {
  134. PyObject *fd;
  135. int chunk_size, window_size, seed;
  136. ChunkifyIter *c;
  137. if (!PyArg_ParseTuple(args, "Oiii", &fd, &chunk_size, &window_size, &seed))
  138. {
  139. return NULL;
  140. }
  141. if (!(c = PyObject_New(ChunkifyIter, &ChunkifyIterType)))
  142. {
  143. return NULL;
  144. }
  145. PyObject_Init((PyObject *)c, &ChunkifyIterType);
  146. c->buf_size = 10 * 1024 * 1024;
  147. c->data = malloc(c->buf_size);
  148. c->fd = fd;
  149. c->chunk_size = chunk_size;
  150. c->window_size = window_size;
  151. c->seed = seed % chunk_size;
  152. Py_INCREF(fd);
  153. return (PyObject *)c;
  154. }
  155. static PyMethodDef ChunkifierMethods[] = {
  156. {"chunkify", chunkify, METH_VARARGS, ""},
  157. {NULL, NULL, 0, NULL} /* Sentinel */
  158. };
  159. PyMODINIT_FUNC
  160. init_speedups(void)
  161. {
  162. PyObject* m;
  163. ChunkifyIterType.tp_new = PyType_GenericNew;
  164. if (PyType_Ready(&ChunkifyIterType) < 0) return;
  165. m = Py_InitModule("_speedups", ChunkifierMethods);
  166. }