Skip to content

Commit 68fba82

Browse files
committed
Optimized __getslice__ implementation a bit
1 parent b00bc5e commit 68fba82

1 file changed

Lines changed: 5 additions & 10 deletions

File tree

smmap/buf.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,17 @@ def __getslice__(self, i, j):
6161
else:
6262
l = j-i # total length
6363
ofs = i
64-
# keep tokens, and join afterwards. This is faster
65-
# as it can preallocate the total amoint of space needed
66-
# (and its verified the implementation does that)
67-
# Question is whether the list allocation doesn't counteract this,
68-
# but lets see ...
69-
tokens = list()
70-
tappend = tokens.append
71-
64+
# Keeping tokens in a list could possible be faster, but the list
65+
# overhead outweighs the benefits (tested) !
66+
md = str()
7267
while l:
7368
c.use_region(ofs, l)
7469
d = c.buffer()[:l]
7570
ofs += len(d)
7671
l -= len(d)
77-
tappend(d)
72+
md += d
7873
#END while there are bytes to read
79-
return ''.join(tokens)
74+
return md
8075
# END fast or slow path
8176
#{ Interface
8277

0 commit comments

Comments
 (0)