Skip to content

Commit 60b4f37

Browse files
committed
Improved performance of python implementation by 10 percent, just by removing function calls and object creations
1 parent 60f7768 commit 60b4f37

File tree

1 file changed

+17
-65
lines changed

1 file changed

+17
-65
lines changed

fun.py

Lines changed: 17 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ def _set_delta_rbound(d, size):
5555
:param size: size relative to our target offset, may not be 0, must be smaller or equal
5656
to our size
5757
:return: d"""
58-
if d.ts == size:
59-
return
60-
6158
d.ts = size
6259

6360
# NOTE: data is truncated automatically when applying the delta
@@ -154,76 +151,30 @@ def _closest_index(dcl, absofs):
154151
# END for each delta absofs
155152
return len(dcl)-1
156153

157-
def delta_list_apply(dcl, bbuf, write, lbound_offset=0, size=0):
154+
def delta_list_apply(dcl, bbuf, write):
158155
"""Apply the chain's changes and write the final result using the passed
159156
write function.
160157
:param bbuf: base buffer containing the base of all deltas contained in this
161158
list. It will only be used if the chunk in question does not have a base
162159
chain.
163-
:param lbound_offset: offset at which to start applying the delta, relative to
164-
our lbound
165-
:param size: if larger than 0, only the given amount of bytes will be applied
166160
:param write: function taking a string of bytes to write to the output"""
167-
slen = len(dcl)
168-
if slen == 0:
169-
return
170-
# END early abort
171-
absofs = dcl.lbound() + lbound_offset
172-
if size == 0:
173-
size = dcl.rbound() - absofs
174-
# END initialize size
175-
176-
if lbound_offset or absofs + size != dcl.rbound():
177-
cdi = _closest_index(dcl, absofs)
178-
cd = dcl[cdi]
179-
if cd.to != absofs:
180-
tcd = delta_duplicate(cd)
181-
_move_delta_lbound(tcd, absofs - cd.to)
182-
_set_delta_rbound(tcd, min(tcd.ts, size))
183-
delta_chunk_apply(tcd, bbuf, write)
184-
size -= tcd.ts
185-
cdi += 1
186-
# END handle first chunk
187-
188-
# here we have to either apply full chunks, or smaller ones, but
189-
# we always start at the chunks target offset
190-
while cdi < slen and size:
191-
cd = dcl[cdi]
192-
if cd.ts <= size:
193-
delta_chunk_apply(cd, bbuf, write)
194-
size -= cd.ts
195-
else:
196-
tcd = delta_duplicate(cd)
197-
_set_delta_rbound(tcd, size)
198-
delta_chunk_apply(tcd, bbuf, write)
199-
size -= tcd.ts
200-
break
201-
# END handle bytes to apply
202-
cdi += 1
203-
# END handle rest
204-
else:
205-
for dc in dcl:
206-
delta_chunk_apply(dc, bbuf, write)
207-
# END for each dc
208-
# END handle application values
161+
for dc in dcl:
162+
delta_chunk_apply(dc, bbuf, write)
163+
# END for each dc
209164

210-
def delta_list_slice(dcl, absofs, size):
165+
def delta_list_slice(dcl, absofs, size, ndcl):
211166
""":return: Subsection of this list at the given absolute offset, with the given
212167
size in bytes.
213-
:return: list (copy) which represents the given chunk"""
214-
dcllbound = dcl.lbound()
215-
absofs = max(absofs, dcllbound)
216-
size = min(dcl.rbound() - dcllbound, size)
168+
:return: None"""
217169
cdi = _closest_index(dcl, absofs) # delta start index
218170
cd = dcl[cdi]
219171
slen = len(dcl)
220-
ndcl = list()
221172
lappend = ndcl.append
222173

223174
if cd.to != absofs:
224-
tcd = delta_duplicate(cd)
175+
tcd = DeltaChunk(cd.to, cd.ts, cd.so, cd.data)
225176
_move_delta_lbound(tcd, absofs - cd.to)
226-
_set_delta_rbound(tcd, min(tcd.ts, size))
177+
tcd.ts = min(tcd.ts, size)
227178
lappend(tcd)
228179
size -= tcd.ts
229180
cdi += 1
@@ -233,19 +184,18 @@ def delta_list_slice(dcl, absofs, size):
233184
# are we larger than the current block
234185
cd = dcl[cdi]
235186
if cd.ts <= size:
236-
lappend(delta_duplicate(cd))
187+
lappend(DeltaChunk(cd.to, cd.ts, cd.so, cd.data))
237188
size -= cd.ts
238189
else:
239-
tcd = delta_duplicate(cd)
240-
_set_delta_rbound(tcd, size)
190+
tcd = DeltaChunk(cd.to, cd.ts, cd.so, cd.data)
191+
tcd.ts = size
241192
lappend(tcd)
242193
size -= tcd.ts
243194
break
244195
# END hadle size
245196
cdi += 1
246197
# END for each chunk
247198

248-
return ndcl
249199

250200
class DeltaChunkList(list):
251201
"""List with special functionality to deal with DeltaChunks.
@@ -272,10 +222,10 @@ def size(self):
272222
""":return: size of bytes as measured by our delta chunks"""
273223
return self.rbound() - self.lbound()
274224

275-
def apply(self, bbuf, write, lbound_offset=0, size=0):
225+
def apply(self, bbuf, write):
276226
"""Only used by public clients, internally we only use the global routines
277227
for performance"""
278-
return delta_list_apply(self, bbuf, write, lbound_offset, size)
228+
return delta_list_apply(self, bbuf, write)
279229

280230
def compress(self):
281231
"""Alter the list to reduce the amount of nodes. Currently we concatenate
@@ -369,6 +319,7 @@ def connect_with_next_base(self, bdcl):
369319
nfc = 0 # number of frozen chunks
370320
dci = 0 # delta chunk index
371321
slen = len(self) # len of self
322+
ccl = list() # temporary list
372323
while dci < slen:
373324
dc = self[dci]
374325
dci += 1
@@ -384,8 +335,9 @@ def connect_with_next_base(self, bdcl):
384335
# dont support efficient insertion ( just one at a time ), but for now
385336
# we live with it. Internally, its all just a 32/64bit pointer, and
386337
# the portions of moved memory should be smallish. Maybe we just rebuild
387-
# ourselves in order to reduce the amount of insertions ...
388-
ccl = delta_list_slice(bdcl, dc.so, dc.ts)
338+
# ourselves in order to reduce the amount of insertions ...
339+
del(ccl[:])
340+
delta_list_slice(bdcl, dc.so, dc.ts, ccl)
389341

390342
# move the target bounds into place to match with our chunk
391343
ofs = dc.to - dc.so

0 commit comments

Comments
 (0)