180
174
self.seen_bytes = next_seen_size
182
176
if not reserved and self.num_repack >= self._max_repack:
184
# and (self.num_repack > self._max_repack
185
# or (self._max_repack == self._max_repack
186
# and not self.compressor_has_copy))):
187
# # We have packed too many times already.
177
# We already know we don't want to try to fit more
189
if not reserved and self.num_repack == self._max_repack:
190
assert self.compressor_has_copy
191
# We are trying to sneak in a few more keys before we run out
192
# of room, so copy the compressor. If we bust, we stop right
194
copy = self.compressor.copy()
195
out = self.compressor.compress(bytes)
196
out += self.compressor.flush(Z_SYNC_FLUSH)
197
total_len = sum(map(len, self.bytes_list)) + len(out)
198
if total_len + 10 > capacity:
199
self.compressor = copy
203
# It is tempting to use the copied compressor here, because it
204
# is more tightly packed. It gets us to the maximum packing
205
# value. However, it adds about the same overhead as setting
206
# _max_repack to a higher value
207
# self.compressor = copy
208
# out = self.compressor.compress(bytes)
209
self.bytes_in.append(bytes)
211
self.bytes_list.append(out)
213
179
# This may or may not fit, try to add it with Z_SYNC_FLUSH
214
180
out = self.compressor.compress(bytes)
216
self.bytes_list.append(out)
217
out = self.compressor.flush(Z_SYNC_FLUSH)
219
self.bytes_list.append(out)
221
# TODO: We may want to cache total_len, as the 'sum' call seems to
222
# be showing up a bit on lsprof output
181
out += self.compressor.flush(Z_SYNC_FLUSH)
183
self.bytes_list.append(out)
223
184
total_len = sum(map(len, self.bytes_list))
224
# Give us some extra room for a final Z_FINISH call.
185
# total_len + 10 is to give some room for Z_FINISH
225
186
if total_len + 10 > capacity:
226
187
# We are over budget, try to squeeze this in without any
227
188
# Z_SYNC_FLUSH calls
228
189
self.num_repack += 1
229
if False and self.num_repack >= self._max_repack:
231
alt_compressed = None
233
(bytes_out, compressor,
234
alt_compressed) = self._recompress_all_bytes_in(bytes)
235
this_len = sum(map(len, bytes_out))
190
bytes_out, compressor = self._recompress_all_bytes_in(bytes)
191
this_len = sum(map(len, bytes_out))
236
192
if this_len is None or this_len + 10 > capacity:
237
193
# No way we can add anymore, we need to re-pack because our
238
# compressor is now out of sync
239
if alt_compressed is None:
240
bytes_out, compressor, _ = self._recompress_all_bytes_in()
242
bytes_out, compressor = alt_compressed
194
# compressor is now out of sync.
195
# This seems to be rarely triggered over
196
# num_repack > _max_repack
197
bytes_out, compressor = self._recompress_all_bytes_in()
243
198
self.compressor = compressor
244
199
self.bytes_list = bytes_out
245
200
self.unused_bytes = bytes
248
203
# This fits when we pack it tighter, so use the new packing
249
if alt_compressed is not None:
250
# We know it will fit, so put it into another
251
# compressor without Z_SYNC_FLUSH
252
bytes_out, compressor = alt_compressed
253
compressor.compress(bytes)
256
# There is one Z_SYNC_FLUSH call in
257
# _recompress_all_bytes_in
204
# There is one Z_SYNC_FLUSH call in
205
# _recompress_all_bytes_in
259
206
self.compressor = compressor
260
207
self.bytes_in.append(bytes)
261
208
self.bytes_list = bytes_out