/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
1
# Copyright (C) 2008 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
3641.3.29 by John Arbash Meinel
Cleanup the copyright headers
16
#
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
17
18
"""Pyrex extensions to btree node parsing."""
19
20
cdef extern from "stdlib.h":
21
    ctypedef unsigned size_t
22
23
cdef extern from "Python.h":
3641.3.32 by John Arbash Meinel
PQM's pyrex version requires Py_ssize_t to be manually defined
24
    ctypedef int Py_ssize_t # Required for older pyrex versions
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
25
    ctypedef struct PyObject:
26
        pass
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
27
    int PyList_Append(object lst, object item) except -1
28
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
29
    char *PyString_AsString(object p) except NULL
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
30
    object PyString_FromStringAndSize(char *, Py_ssize_t)
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
31
    int PyString_CheckExact(object s)
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
32
    int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *)
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
33
    Py_ssize_t PyString_Size(object p)
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
34
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *)
35
    char * PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *)
36
    int PyString_AsStringAndSize_ptr(PyObject *, char **buf, Py_ssize_t *len)
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
37
    int PyTuple_CheckExact(object t)
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
38
    Py_ssize_t PyTuple_GET_SIZE(object t)
39
    PyObject *PyTuple_GET_ITEM_ptr_object "PyTuple_GET_ITEM" (object tpl, int index)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
40
41
cdef extern from "string.h":
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
42
    void *memcpy(void *dest, void *src, size_t n)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
43
    void *memchr(void *s, int c, size_t n)
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
44
    # GNU extension
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
45
    # void *memrchr(void *s, int c, size_t n)
46
    int strncmp(char *s1, char *s2, size_t n)
47
48
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
49
# TODO: Find some way to import this from _dirstate_helpers
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
50
cdef void* _my_memrchr(void *s, int c, size_t n):
51
    # memrchr seems to be a GNU extension, so we have to implement it ourselves
52
    # It is not present in any win32 standard library
53
    cdef char *pos
54
    cdef char *start
55
56
    start = <char*>s
57
    pos = start + n - 1
58
    while pos >= start:
59
        if pos[0] == c:
60
            return <void*>pos
61
        pos = pos - 1
62
    return NULL
63
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
64
# TODO: Import this from _dirstate_helpers when it is merged
65
cdef object safe_string_from_size(char *s, Py_ssize_t size):
66
    if size < 0:
67
        raise AssertionError(
68
            'tried to create a string with an invalid size: %d @0x%x'
69
            % (size, <int>s))
70
    return PyString_FromStringAndSize(s, size)
71
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
72
73
cdef class BTreeLeafParser:
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
74
    """Parse the leaf nodes of a BTree index.
75
76
    :ivar bytes: The PyString object containing the uncompressed text for the
77
        node.
78
    :ivar key_length: An integer describing how many pieces the keys have for
79
        this index.
80
    :ivar ref_list_length: An integer describing how many references this index
81
        contains.
82
    :ivar keys: A PyList of keys found in this node.
83
84
    :ivar _cur_str: A pointer to the start of the next line to parse
85
    :ivar _end_str: A pointer to the end of bytes
86
    :ivar _start: Pointer to the location within the current line while
87
        parsing.
88
    :ivar _header_found: True when we have parsed the header for this node
89
    """
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
90
91
    cdef object bytes
92
    cdef int key_length
93
    cdef int ref_list_length
94
    cdef object keys
95
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
96
    cdef char * _cur_str
97
    cdef char * _end_str
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
98
    # The current start point for parsing
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
99
    cdef char * _start
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
100
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
101
    cdef int _header_found
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
102
103
    def __init__(self, bytes, key_length, ref_list_length):
104
        self.bytes = bytes
105
        self.key_length = key_length
106
        self.ref_list_length = ref_list_length
107
        self.keys = []
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
108
        self._cur_str = NULL
109
        self._end_str = NULL
110
        self._header_found = 0
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
111
112
    cdef extract_key(self, char * last):
113
        """Extract a key.
114
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
115
        :param last: points at the byte after the last byte permitted for the
116
            key.
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
117
        """
118
        cdef char *temp_ptr
119
        cdef int loop_counter
120
        # keys are tuples
121
        loop_counter = 0
122
        key_segments = []
123
        while loop_counter < self.key_length:
124
            loop_counter = loop_counter + 1
125
            # grab a key segment
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
126
            temp_ptr = <char*>memchr(self._start, c'\0', last - self._start)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
127
            if temp_ptr == NULL:
128
                if loop_counter == self.key_length:
129
                    # capture to last
130
                    temp_ptr = last
131
                else:
132
                    # Invalid line
133
                    failure_string = ("invalid key, wanted segment from " +
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
134
                        repr(safe_string_from_size(self._start,
135
                                                   last - self._start)))
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
136
                    raise AssertionError(failure_string)
137
            # capture the key string
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
138
            # TODO: Consider using PyIntern_FromString, the only caveat is that
139
            # it assumes a NULL-terminated string, so we have to check if
140
            # temp_ptr[0] == c'\0' or some other char.
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
141
            key_element = safe_string_from_size(self._start,
142
                                                temp_ptr - self._start)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
143
            # advance our pointer
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
144
            self._start = temp_ptr + 1
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
145
            PyList_Append(key_segments, key_element)
146
        return tuple(key_segments)
147
148
    cdef int process_line(self) except -1:
149
        """Process a line in the bytes."""
150
        cdef char *last
151
        cdef char *temp_ptr
152
        cdef char *ref_ptr
153
        cdef char *next_start
154
        cdef int loop_counter
155
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
156
        self._start = self._cur_str
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
157
        # Find the next newline
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
158
        last = <char*>memchr(self._start, c'\n', self._end_str - self._start)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
159
        if last == NULL:
160
            # Process until the end of the file
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
161
            last = self._end_str
162
            self._cur_str = self._end_str
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
163
        else:
164
            # And the next string is right after it
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
165
            self._cur_str = last + 1
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
166
            # The last character is right before the '\n'
167
            last = last
168
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
169
        if last == self._start:
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
170
            # parsed it all.
171
            return 0
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
172
        if last < self._start:
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
173
            # Unexpected error condition - fail
174
            return -1
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
175
        if 0 == self._header_found:
176
            # The first line in a leaf node is the header "type=leaf\n"
177
            if strncmp("type=leaf", self._start, last - self._start) == 0:
178
                self._header_found = 1
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
179
                return 0
180
            else:
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
181
                raise AssertionError('Node did not start with "type=leaf": %r'
182
                    % (safe_string_from_size(self._start, last - self._start)))
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
183
                return -1
184
185
        key = self.extract_key(last)
186
        # find the value area
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
187
        temp_ptr = <char*>_my_memrchr(self._start, c'\0', last - self._start)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
188
        if temp_ptr == NULL:
189
            # Invalid line
190
            return -1
191
        else:
192
            # capture the value string
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
193
            value = safe_string_from_size(temp_ptr + 1, last - temp_ptr - 1)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
194
            # shrink the references end point
195
            last = temp_ptr
196
        if self.ref_list_length:
197
            ref_lists = []
198
            loop_counter = 0
199
            while loop_counter < self.ref_list_length:
200
                ref_list = []
201
                # extract a reference list
202
                loop_counter = loop_counter + 1
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
203
                if last < self._start:
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
204
                    return -1
205
                # find the next reference list end point:
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
206
                temp_ptr = <char*>memchr(self._start, c'\t', last - self._start)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
207
                if temp_ptr == NULL:
208
                    # Only valid for the last list
209
                    if loop_counter != self.ref_list_length:
210
                        # Invalid line
211
                        return -1
212
                        raise AssertionError("invalid key")
213
                    else:
214
                        # scan to the end of the ref list area
215
                        ref_ptr = last
216
                        next_start = last
217
                else:
218
                    # scan to the end of this ref list
219
                    ref_ptr = temp_ptr
220
                    next_start = temp_ptr + 1
221
                # Now, there may be multiple keys in the ref list.
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
222
                while self._start < ref_ptr:
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
223
                    # loop finding keys and extracting them
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
224
                    temp_ptr = <char*>memchr(self._start, c'\r',
225
                                             ref_ptr - self._start)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
226
                    if temp_ptr == NULL:
227
                        # key runs to the end
228
                        temp_ptr = ref_ptr
229
                    PyList_Append(ref_list, self.extract_key(temp_ptr))
230
                PyList_Append(ref_lists, tuple(ref_list))
231
                # prepare for the next reference list
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
232
                self._start = next_start
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
233
            ref_lists = tuple(ref_lists)
234
            node_value = (value, ref_lists)
235
        else:
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
236
            if last != self._start:
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
237
                # unexpected reference data present
238
                return -1
239
            node_value = (value, ())
240
        PyList_Append(self.keys, (key, node_value))
241
        return 0
242
243
    def parse(self):
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
244
        cdef Py_ssize_t byte_count
245
        if not PyString_CheckExact(self.bytes):
246
            raise AssertionError('self.bytes is not a string.')
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
247
        byte_count = PyString_Size(self.bytes)
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
248
        self._cur_str = PyString_AsString(self.bytes)
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
249
        # This points to the last character in the string
3641.3.2 by John Arbash Meinel
Clean up some variable names, add some documentation.
250
        self._end_str = self._cur_str + byte_count
251
        while self._cur_str < self._end_str:
3641.3.1 by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests.
252
            self.process_line()
253
        return self.keys
254
255
256
def _parse_leaf_lines(bytes, key_length, ref_list_length):
257
    parser = BTreeLeafParser(bytes, key_length, ref_list_length)
258
    return parser.parse()
3641.3.18 by John Arbash Meinel
Start working on a compiled function for transforming
259
260
261
def _flatten_node(node, reference_lists):
262
    """Convert a node into the serialized form.
263
264
    :param node: A tuple representing a node:
265
        (index, key_tuple, value, references)
266
    :param reference_lists: Does this index have reference lists?
267
    :return: (string_key, flattened)
268
        string_key  The serialized key for referencing this node
269
        flattened   A string with the serialized form for the contents
270
    """
3641.3.26 by John Arbash Meinel
A couple small tweaks.
271
    cdef int have_reference_lists
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
272
    cdef Py_ssize_t flat_len
273
    cdef Py_ssize_t key_len
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
274
    cdef Py_ssize_t node_len
275
    cdef PyObject * val
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
276
    cdef char * value
277
    cdef Py_ssize_t value_len
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
278
    cdef char * out
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
279
    cdef Py_ssize_t refs_len
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
280
    cdef Py_ssize_t next_len
3641.3.21 by John Arbash Meinel
Flatten the outermost str.join() into memcpy's
281
    cdef int first_ref_list
3641.3.22 by John Arbash Meinel
flatten the next level
282
    cdef int first_reference
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
283
    cdef int i
284
    cdef PyObject *ref_bit
285
    cdef Py_ssize_t ref_bit_len
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
286
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
287
    if not PyTuple_CheckExact(node):
288
        raise TypeError('We expected a tuple() for node not: %s'
289
            % type(node))
290
    node_len = PyTuple_GET_SIZE(node)
3641.3.26 by John Arbash Meinel
A couple small tweaks.
291
    have_reference_lists = reference_lists
292
    if have_reference_lists:
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
293
        if node_len != 4:
294
            raise ValueError('With ref_lists, we expected 4 entries not: %s'
295
                % len(node))
296
    elif node_len < 3:
297
        raise ValueError('Without ref_lists, we need at least 3 entries not: %s'
298
            % len(node))
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
299
    # I don't expect that we can do faster than string.join()
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
300
    string_key = '\0'.join(<object>PyTuple_GET_ITEM_ptr_object(node, 1))
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
301
3641.3.18 by John Arbash Meinel
Start working on a compiled function for transforming
302
    # TODO: instead of using string joins, precompute the final string length,
303
    #       and then malloc a single string and copy everything in.
3641.3.19 by John Arbash Meinel
The flatten code now handles the no-ref-list case.
304
305
    # TODO: We probably want to use PySequenceFast, because we have lists and
306
    #       tuples, but we aren't sure which we will get.
307
308
    # line := string_key NULL flat_refs NULL value LF
309
    # string_key := BYTES (NULL BYTES)*
310
    # flat_refs := ref_list (TAB ref_list)*
311
    # ref_list := ref (CR ref)*
312
    # ref := BYTES (NULL BYTES)*
313
    # value := BYTES
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
314
    refs_len = 0
3641.3.26 by John Arbash Meinel
A couple small tweaks.
315
    if have_reference_lists:
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
316
        # Figure out how many bytes it will take to store the references
3641.3.26 by John Arbash Meinel
A couple small tweaks.
317
        ref_lists = <object>PyTuple_GET_ITEM_ptr_object(node, 3)
3641.3.24 by John Arbash Meinel
Use the compiled flatten function.
318
        next_len = len(ref_lists) # TODO: use a Py function
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
319
        if next_len > 0:
320
            # If there are no nodes, we don't need to do any work
321
            # Otherwise we will need (len - 1) '\t' characters to separate
322
            # the reference lists
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
323
            refs_len = refs_len + (next_len - 1)
3641.3.24 by John Arbash Meinel
Use the compiled flatten function.
324
            for ref_list in ref_lists:
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
325
                next_len = len(ref_list)
326
                if next_len > 0:
327
                    # We will need (len - 1) '\r' characters to separate the
328
                    # references
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
329
                    refs_len = refs_len + (next_len - 1)
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
330
                    for reference in ref_list:
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
331
                        if not PyTuple_CheckExact(reference):
332
                            raise TypeError(
333
                                'We expect references to be tuples not: %s'
334
                                % type(reference))
335
                        next_len = PyTuple_GET_SIZE(reference)
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
336
                        if next_len > 0:
337
                            # We will need (len - 1) '\x00' characters to
338
                            # separate the reference key
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
339
                            refs_len = refs_len + (next_len - 1)
340
                            for i from 0 <= i < next_len:
341
                                ref_bit = PyTuple_GET_ITEM_ptr_object(reference, i)
3641.3.26 by John Arbash Meinel
A couple small tweaks.
342
                                if not PyString_CheckExact_ptr(ref_bit):
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
343
                                    raise TypeError('We expect reference bits'
344
                                        ' to be strings not: %s'
345
                                        % type(<object>ref_bit))
346
                                refs_len = refs_len + PyString_GET_SIZE_ptr(ref_bit)
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
347
348
    # So we have the (key NULL refs NULL value LF)
349
    key_len = PyString_Size(string_key)
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
350
    val = PyTuple_GET_ITEM_ptr_object(node, 2)
3641.3.26 by John Arbash Meinel
A couple small tweaks.
351
    if not PyString_CheckExact_ptr(val):
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
352
        raise TypeError('Expected a plain str for value not: %s'
353
                        % type(<object>val))
354
    value = PyString_AS_STRING_ptr(val)
355
    value_len = PyString_GET_SIZE_ptr(val)
356
    flat_len = (key_len + 1 + refs_len + 1 + value_len + 1)
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
357
    line = PyString_FromStringAndSize(NULL, flat_len)
358
    # Get a pointer to the new buffer
359
    out = PyString_AsString(line)
360
    memcpy(out, PyString_AsString(string_key), key_len)
361
    out = out + key_len
362
    out[0] = c'\0'
363
    out = out + 1
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
364
    if refs_len > 0:
3641.3.21 by John Arbash Meinel
Flatten the outermost str.join() into memcpy's
365
        first_ref_list = 1
3641.3.24 by John Arbash Meinel
Use the compiled flatten function.
366
        for ref_list in ref_lists:
3641.3.21 by John Arbash Meinel
Flatten the outermost str.join() into memcpy's
367
            if first_ref_list == 0:
368
                out[0] = c'\t'
369
                out = out + 1
370
            first_ref_list = 0
3641.3.22 by John Arbash Meinel
flatten the next level
371
            first_reference = 1
3641.3.21 by John Arbash Meinel
Flatten the outermost str.join() into memcpy's
372
            for reference in ref_list:
3641.3.22 by John Arbash Meinel
flatten the next level
373
                if first_reference == 0:
374
                    out[0] = c'\r'
375
                    out = out + 1
376
                first_reference = 0
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
377
                next_len = PyTuple_GET_SIZE(reference)
378
                for i from 0 <= i < next_len:
379
                    if i != 0:
3641.3.23 by John Arbash Meinel
Flattened all the way down the stack.
380
                        out[0] = c'\x00'
381
                        out = out + 1
3641.3.25 by John Arbash Meinel
Shave off some more time by using exact accessors.
382
                    ref_bit = PyTuple_GET_ITEM_ptr_object(reference, i)
383
                    ref_bit_len = PyString_GET_SIZE_ptr(ref_bit)
384
                    memcpy(out, PyString_AS_STRING_ptr(ref_bit), ref_bit_len)
385
                    out = out + ref_bit_len
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
386
    out[0] = c'\0'
3641.3.22 by John Arbash Meinel
flatten the next level
387
    out = out  + 1
3641.3.20 by John Arbash Meinel
We have a single malloc for the final output.
388
    memcpy(out, value, value_len)
389
    out = out + value_len
390
    out[0] = c'\n'
3641.3.18 by John Arbash Meinel
Start working on a compiled function for transforming
391
    return string_key, line