38
38
Py_ssize_t PyString_Size(object p)
39
39
Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *)
40
40
char * PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *)
41
char * PyString_AS_STRING(object)
42
Py_ssize_t PyString_GET_SIZE(object)
43
41
int PyString_AsStringAndSize_ptr(PyObject *, char **buf, Py_ssize_t *len)
44
42
void PyString_InternInPlace(PyObject **)
45
43
int PyTuple_CheckExact(object t)
57
55
# void *memrchr(void *s, int c, size_t n)
58
56
int strncmp(char *s1, char *s2, size_t n)
60
# It seems we need to import the definitions so that the pyrex compiler has
61
# local names to access them.
62
from _static_tuple_c cimport StaticTuple, \
63
import_static_tuple_c, StaticTuple_New, \
64
StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
67
59
# TODO: Find some way to import this from _dirstate_helpers
68
60
cdef void* _my_memrchr(void *s, int c, size_t n):
103
94
Py_DECREF_ptr(py_str)
106
from bzrlib import _static_tuple_c
108
_ST = _static_tuple_c.StaticTuple
109
# This sets up the StaticTuple C_API functionality
110
import_static_tuple_c()
113
98
cdef class BTreeLeafParser:
114
99
"""Parse the leaf nodes of a BTree index.
159
143
cdef char *temp_ptr
160
144
cdef int loop_counter
163
key = StaticTuple_New(self.key_length)
146
key = PyTuple_New(self.key_length)
164
147
for loop_counter from 0 <= loop_counter < self.key_length:
165
148
# grab a key segment
166
149
temp_ptr = <char*>memchr(self._start, c'\0', last - self._start)
175
158
last - self._start)))
176
159
raise AssertionError(failure_string)
177
160
# capture the key string
178
if (self.key_length == 1
179
and (temp_ptr - self._start) == 45
180
and strncmp(self._start, 'sha1:', 5) == 0):
181
key_element = safe_string_from_size(self._start,
182
temp_ptr - self._start)
184
key_element = safe_interned_string_from_size(self._start,
161
# TODO: Consider using PyIntern_FromString, the only caveat is that
162
# it assumes a NULL-terminated string, so we have to check if
163
# temp_ptr[0] == c'\0' or some other char.
164
key_element = safe_interned_string_from_size(self._start,
185
165
temp_ptr - self._start)
186
166
# advance our pointer
187
167
self._start = temp_ptr + 1
188
168
Py_INCREF(key_element)
189
StaticTuple_SET_ITEM(key, loop_counter, key_element)
190
key = StaticTuple_Intern(key)
169
PyTuple_SET_ITEM(key, loop_counter, key_element)
193
172
cdef int process_line(self) except -1:
234
212
raise AssertionError("Failed to find the value area")
236
# Because of how conversions were done, we ended up with *lots* of
237
# values that are identical. These are all of the 0-length nodes
238
# that are referred to by the TREE_ROOT (and likely some other
239
# directory nodes.) For example, bzr has 25k references to
240
# something like '12607215 328306 0 0', which ends up consuming 1MB
241
# of memory, just for those strings.
242
str_len = last - temp_ptr - 1
244
and strncmp(" 0 0", last - 4, 4) == 0):
245
# This drops peak mem for bzr.dev from 87.4MB => 86.2MB
246
# For Launchpad 236MB => 232MB
247
value = safe_interned_string_from_size(temp_ptr + 1, str_len)
249
value = safe_string_from_size(temp_ptr + 1, str_len)
214
# capture the value string
215
value = safe_string_from_size(temp_ptr + 1, last - temp_ptr - 1)
250
216
# shrink the references end point
253
218
if self.ref_list_length:
254
ref_lists = StaticTuple_New(self.ref_list_length)
256
221
while loop_counter < self.ref_list_length:
283
248
if temp_ptr == NULL:
284
249
# key runs to the end
285
250
temp_ptr = ref_ptr
287
251
PyList_Append(ref_list, self.extract_key(temp_ptr))
288
ref_list = StaticTuple_Intern(StaticTuple(*ref_list))
290
StaticTuple_SET_ITEM(ref_lists, loop_counter - 1, ref_list)
252
PyList_Append(ref_lists, tuple(ref_list))
291
253
# prepare for the next reference list
292
254
self._start = next_start
293
node_value = StaticTuple(value, ref_lists)
255
ref_lists = tuple(ref_lists)
256
node_value = (value, ref_lists)
295
258
if last != self._start:
296
259
# unexpected reference data present
297
260
raise AssertionError("unexpected reference data present")
298
node_value = StaticTuple(value, StaticTuple())
299
PyList_Append(self.keys, StaticTuple(key, node_value))
261
node_value = (value, ())
262
PyList_Append(self.keys, (key, node_value))
339
303
cdef int first_ref_list
340
304
cdef int first_reference
306
cdef PyObject *ref_bit
342
307
cdef Py_ssize_t ref_bit_len
344
if not PyTuple_CheckExact(node) and not StaticTuple_CheckExact(node):
345
raise TypeError('We expected a tuple() or StaticTuple() for node not: %s'
309
if not PyTuple_CheckExact(node):
310
raise TypeError('We expected a tuple() for node not: %s'
312
node_len = PyTuple_GET_SIZE(node)
348
313
have_reference_lists = reference_lists
349
314
if have_reference_lists:
350
315
if node_len != 4:
354
319
raise ValueError('Without ref_lists, we need at least 3 entries not: %s'
356
321
# I don't expect that we can do faster than string.join()
357
string_key = '\0'.join(node[1])# <object>PyTuple_GET_ITEM_ptr_object(node, 1))
322
string_key = '\0'.join(<object>PyTuple_GET_ITEM_ptr_object(node, 1))
359
324
# TODO: instead of using string joins, precompute the final string length,
360
325
# and then malloc a single string and copy everything in.
372
337
if have_reference_lists:
373
338
# Figure out how many bytes it will take to store the references
374
ref_lists = node[3]# <object>PyTuple_GET_ITEM_ptr_object(node, 3)
339
ref_lists = <object>PyTuple_GET_ITEM_ptr_object(node, 3)
375
340
next_len = len(ref_lists) # TODO: use a Py function
377
342
# If there are no nodes, we don't need to do any work
386
351
refs_len = refs_len + (next_len - 1)
387
352
for reference in ref_list:
388
if (not PyTuple_CheckExact(reference)
389
and not StaticTuple_CheckExact(reference)):
353
if not PyTuple_CheckExact(reference):
391
355
'We expect references to be tuples not: %s'
392
356
% type(reference))
393
next_len = len(reference)
357
next_len = PyTuple_GET_SIZE(reference)
395
359
# We will need (len - 1) '\x00' characters to
396
360
# separate the reference key
397
361
refs_len = refs_len + (next_len - 1)
398
362
for i from 0 <= i < next_len:
399
ref_bit = reference[i]
400
if not PyString_CheckExact(ref_bit):
363
ref_bit = PyTuple_GET_ITEM_ptr_object(reference, i)
364
if not PyString_CheckExact_ptr(ref_bit):
401
365
raise TypeError('We expect reference bits'
402
366
' to be strings not: %s'
403
367
% type(<object>ref_bit))
404
refs_len = refs_len + PyString_GET_SIZE(ref_bit)
368
refs_len = refs_len + PyString_GET_SIZE_ptr(ref_bit)
406
370
# So we have the (key NULL refs NULL value LF)
407
371
key_len = PyString_Size(string_key)
408
val = node[2] # PyTuple_GET_ITEM_ptr_object(node, 2)
409
if not PyString_CheckExact(val):
372
val = PyTuple_GET_ITEM_ptr_object(node, 2)
373
if not PyString_CheckExact_ptr(val):
410
374
raise TypeError('Expected a plain str for value not: %s'
412
value = PyString_AS_STRING(val)
413
value_len = PyString_GET_SIZE(val)
376
value = PyString_AS_STRING_ptr(val)
377
value_len = PyString_GET_SIZE_ptr(val)
414
378
flat_len = (key_len + 1 + refs_len + 1 + value_len + 1)
415
379
line = PyString_FromStringAndSize(NULL, flat_len)
416
380
# Get a pointer to the new buffer
434
398
first_reference = 0
435
next_len = len(reference)
399
next_len = PyTuple_GET_SIZE(reference)
436
400
for i from 0 <= i < next_len:
440
ref_bit = reference[i] #PyTuple_GET_ITEM_ptr_object(reference, i)
441
ref_bit_len = PyString_GET_SIZE(ref_bit)
442
memcpy(out, PyString_AS_STRING(ref_bit), ref_bit_len)
404
ref_bit = PyTuple_GET_ITEM_ptr_object(reference, i)
405
ref_bit_len = PyString_GET_SIZE_ptr(ref_bit)
406
memcpy(out, PyString_AS_STRING_ptr(ref_bit), ref_bit_len)
443
407
out = out + ref_bit_len