Skip to content

Commit 475e02a

Browse files
author
Zhen
committed
Do not create new buffer unless it is needed
1 parent ae4d13e commit 475e02a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

neo4j/bolt/io.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,39 @@ def read(self, n):
6868
if n == 0 or self._current_pane == -1:
6969
return _empty_view
7070
value = None
71-
while n > 0 and self._current_pane >= 0:
71+
is_memoryview = False
72+
offset = 0
73+
74+
to_read = n
75+
while to_read > 0 and self._current_pane >= 0:
7276
p, q = self._panes[self._current_pane]
7377
size = q - p
7478
remaining = size - self._current_offset
7579
start = p + self._current_offset
76-
if n <= remaining:
77-
end = start + n
78-
if n < remaining:
79-
self._current_offset += n
80+
if to_read <= remaining:
81+
end = start + to_read
82+
if to_read < remaining:
83+
self._current_offset += to_read
8084
else:
8185
self._next_pane()
8286
else:
8387
end = q
8488
self._next_pane()
89+
90+
read = end - start
8591
if value:
86-
value.extend(self._view[start:end])
92+
if is_memoryview:
93+
new_value = bytearray(n)
94+
new_value[:offset] = value[:offset]
95+
value = new_value
96+
is_memoryview = False
97+
value[offset:offset+read] = self._view[start:end]
8798
else:
88-
value = bytearray(self._view[start:end])
89-
n -= end - start
90-
return memoryview(value)
99+
value = memoryview(self._view[start:end])
100+
is_memoryview = True
101+
offset += read
102+
to_read -= read
103+
return value
91104

92105

93106
class ChunkedInputBuffer(object):

0 commit comments

Comments
 (0)