1
# Copyright (C) 2008, 2009 Canonical Ltd
1
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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)
41
43
int PyString_AsStringAndSize_ptr(PyObject *, char **buf, Py_ssize_t *len)
42
44
void PyString_InternInPlace(PyObject **)
43
45
int PyTuple_CheckExact(object t)
55
57
# void *memrchr(void *s, int c, size_t n)
56
58
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
59
67
# TODO: Find some way to import this from _dirstate_helpers
60
cdef void* _my_memrchr(void *s, int c, size_t n):
68
cdef void* _my_memrchr(void *s, int c, size_t n): # cannot_raise
61
69
# memrchr seems to be a GNU extension, so we have to implement it ourselves
62
70
# It is not present in any win32 standard library
143
157
cdef char *temp_ptr
144
158
cdef int loop_counter
146
key = PyTuple_New(self.key_length)
161
key = StaticTuple_New(self.key_length)
147
162
for loop_counter from 0 <= loop_counter < self.key_length:
148
163
# grab a key segment
149
164
temp_ptr = <char*>memchr(self._start, c'\0', last - self._start)
158
173
last - self._start)))
159
174
raise AssertionError(failure_string)
160
175
# capture the key string
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,
176
if (self.key_length == 1
177
and (temp_ptr - self._start) == 45
178
and strncmp(self._start, 'sha1:', 5) == 0):
179
key_element = safe_string_from_size(self._start,
180
temp_ptr - self._start)
182
key_element = safe_interned_string_from_size(self._start,
165
183
temp_ptr - self._start)
166
184
# advance our pointer
167
185
self._start = temp_ptr + 1
168
186
Py_INCREF(key_element)
169
PyTuple_SET_ITEM(key, loop_counter, key_element)
187
StaticTuple_SET_ITEM(key, loop_counter, key_element)
188
key = StaticTuple_Intern(key)
172
191
cdef int process_line(self) except -1:
188
208
# And the next string is right after it
189
209
self._cur_str = last + 1
190
210
# The last character is right before the '\n'
193
212
if last == self._start:
196
215
if last < self._start:
197
216
# Unexpected error condition - fail
217
raise AssertionError("last < self._start")
199
218
if 0 == self._header_found:
200
219
# The first line in a leaf node is the header "type=leaf\n"
201
220
if strncmp("type=leaf", self._start, last - self._start) == 0:
205
224
raise AssertionError('Node did not start with "type=leaf": %r'
206
225
% (safe_string_from_size(self._start, last - self._start)))
209
227
key = self.extract_key(last)
210
228
# find the value area
211
229
temp_ptr = <char*>_my_memrchr(self._start, c'\0', last - self._start)
212
230
if temp_ptr == NULL:
232
raise AssertionError("Failed to find the value area")
216
# capture the value string
217
value = safe_string_from_size(temp_ptr + 1, last - temp_ptr - 1)
234
# Because of how conversions were done, we ended up with *lots* of
235
# values that are identical. These are all of the 0-length nodes
236
# that are referred to by the TREE_ROOT (and likely some other
237
# directory nodes.) For example, bzr has 25k references to
238
# something like '12607215 328306 0 0', which ends up consuming 1MB
239
# of memory, just for those strings.
240
str_len = last - temp_ptr - 1
242
and strncmp(" 0 0", last - 4, 4) == 0):
243
# This drops peak mem for bzr.dev from 87.4MB => 86.2MB
244
# For Launchpad 236MB => 232MB
245
value = safe_interned_string_from_size(temp_ptr + 1, str_len)
247
value = safe_string_from_size(temp_ptr + 1, str_len)
218
248
# shrink the references end point
220
251
if self.ref_list_length:
252
ref_lists = StaticTuple_New(self.ref_list_length)
223
254
while loop_counter < self.ref_list_length:
225
256
# extract a reference list
226
257
loop_counter = loop_counter + 1
227
258
if last < self._start:
259
raise AssertionError("last < self._start")
229
260
# find the next reference list end point:
230
261
temp_ptr = <char*>memchr(self._start, c'\t', last - self._start)
231
262
if temp_ptr == NULL:
232
263
# Only valid for the last list
233
264
if loop_counter != self.ref_list_length:
236
raise AssertionError("invalid key")
266
raise AssertionError(
267
"invalid key, loop_counter != self.ref_list_length")
238
269
# scan to the end of the ref list area
250
281
if temp_ptr == NULL:
251
282
# key runs to the end
252
283
temp_ptr = ref_ptr
253
285
PyList_Append(ref_list, self.extract_key(temp_ptr))
254
PyList_Append(ref_lists, tuple(ref_list))
286
ref_list = StaticTuple_Intern(StaticTuple(*ref_list))
288
StaticTuple_SET_ITEM(ref_lists, loop_counter - 1, ref_list)
255
289
# prepare for the next reference list
256
290
self._start = next_start
257
ref_lists = tuple(ref_lists)
258
node_value = (value, ref_lists)
291
node_value = StaticTuple(value, ref_lists)
260
293
if last != self._start:
261
294
# unexpected reference data present
263
node_value = (value, ())
264
PyList_Append(self.keys, (key, node_value))
295
raise AssertionError("unexpected reference data present")
296
node_value = StaticTuple(value, StaticTuple())
297
PyList_Append(self.keys, StaticTuple(key, node_value))
305
337
cdef int first_ref_list
306
338
cdef int first_reference
308
cdef PyObject *ref_bit
309
340
cdef Py_ssize_t ref_bit_len
311
if not PyTuple_CheckExact(node):
312
raise TypeError('We expected a tuple() for node not: %s'
342
if not PyTuple_CheckExact(node) and not StaticTuple_CheckExact(node):
343
raise TypeError('We expected a tuple() or StaticTuple() for node not: %s'
314
node_len = PyTuple_GET_SIZE(node)
315
346
have_reference_lists = reference_lists
316
347
if have_reference_lists:
317
348
if node_len != 4:
320
351
elif node_len < 3:
321
352
raise ValueError('Without ref_lists, we need at least 3 entries not: %s'
323
# I don't expect that we can do faster than string.join()
324
string_key = '\0'.join(<object>PyTuple_GET_ITEM_ptr_object(node, 1))
354
# TODO: We can probably do better than string.join(), namely
355
# when key has only 1 item, we can just grab that string
356
# And when there are 2 items, we could do a single malloc + len() + 1
357
# also, doing .join() requires a PyObject_GetAttrString call, which
358
# we could also avoid.
359
# TODO: Note that pyrex 0.9.6 generates fairly crummy code here, using the
360
# python object interface, versus 0.9.8+ which uses a helper that
361
# checks if this supports the sequence interface.
362
# We *could* do more work on our own, and grab the actual items
363
# lists. For now, just ask people to use a better compiler. :)
364
string_key = '\0'.join(node[1])
326
366
# TODO: instead of using string joins, precompute the final string length,
327
367
# and then malloc a single string and copy everything in.
339
379
if have_reference_lists:
340
380
# Figure out how many bytes it will take to store the references
341
ref_lists = <object>PyTuple_GET_ITEM_ptr_object(node, 3)
342
382
next_len = len(ref_lists) # TODO: use a Py function
344
384
# If there are no nodes, we don't need to do any work
353
393
refs_len = refs_len + (next_len - 1)
354
394
for reference in ref_list:
355
if not PyTuple_CheckExact(reference):
395
if (not PyTuple_CheckExact(reference)
396
and not StaticTuple_CheckExact(reference)):
357
398
'We expect references to be tuples not: %s'
358
399
% type(reference))
359
next_len = PyTuple_GET_SIZE(reference)
400
next_len = len(reference)
361
402
# We will need (len - 1) '\x00' characters to
362
403
# separate the reference key
363
404
refs_len = refs_len + (next_len - 1)
364
for i from 0 <= i < next_len:
365
ref_bit = PyTuple_GET_ITEM_ptr_object(reference, i)
366
if not PyString_CheckExact_ptr(ref_bit):
405
for ref_bit in reference:
406
if not PyString_CheckExact(ref_bit):
367
407
raise TypeError('We expect reference bits'
368
408
' to be strings not: %s'
369
409
% type(<object>ref_bit))
370
refs_len = refs_len + PyString_GET_SIZE_ptr(ref_bit)
410
refs_len = refs_len + PyString_GET_SIZE(ref_bit)
372
412
# So we have the (key NULL refs NULL value LF)
373
413
key_len = PyString_Size(string_key)
374
val = PyTuple_GET_ITEM_ptr_object(node, 2)
375
if not PyString_CheckExact_ptr(val):
415
if not PyString_CheckExact(val):
376
416
raise TypeError('Expected a plain str for value not: %s'
378
value = PyString_AS_STRING_ptr(val)
379
value_len = PyString_GET_SIZE_ptr(val)
418
value = PyString_AS_STRING(val)
419
value_len = PyString_GET_SIZE(val)
380
420
flat_len = (key_len + 1 + refs_len + 1 + value_len + 1)
381
421
line = PyString_FromStringAndSize(NULL, flat_len)
382
422
# Get a pointer to the new buffer
400
440
first_reference = 0
401
next_len = PyTuple_GET_SIZE(reference)
441
next_len = len(reference)
402
442
for i from 0 <= i < next_len:
406
ref_bit = PyTuple_GET_ITEM_ptr_object(reference, i)
407
ref_bit_len = PyString_GET_SIZE_ptr(ref_bit)
408
memcpy(out, PyString_AS_STRING_ptr(ref_bit), ref_bit_len)
446
ref_bit = reference[i]
447
ref_bit_len = PyString_GET_SIZE(ref_bit)
448
memcpy(out, PyString_AS_STRING(ref_bit), ref_bit_len)
409
449
out = out + ref_bit_len