Skip to content

Avoid unneeded memory copy of bytes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions diskcache/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def store(self, value, read, key=UNKNOWN):
return 0, MODE_RAW, None, sqlite3.Binary(value)
else:
filename, full_path = self.filename(key, value)
self._write(full_path, io.BytesIO(value), 'xb')
self._write(full_path, value, 'xb')
return len(value), MODE_BINARY, filename, None
elif type_value is str:
filename, full_path = self.filename(key, value)
Expand All @@ -224,10 +224,10 @@ def store(self, value, read, key=UNKNOWN):
return 0, MODE_PICKLE, None, sqlite3.Binary(result)
else:
filename, full_path = self.filename(key, value)
self._write(full_path, io.BytesIO(result), 'xb')
self._write(full_path, result, 'xb')
return len(result), MODE_PICKLE, filename, None

def _write(self, full_path, iterator, mode, encoding=None):
def _write(self, full_path, iterator_or_bytes, mode, encoding=None):
full_dir, _ = op.split(full_path)

for count in range(1, 11):
Expand All @@ -245,11 +245,15 @@ def _write(self, full_path, iterator, mode, encoding=None):
continue

with writer:
size = 0
for chunk in iterator:
size += len(chunk)
writer.write(chunk)
return size
if isinstance(iterator_or_bytes, bytes):
writer.write(iterator_or_bytes)
return len(iterator_or_bytes)
else:
size = 0
for chunk in iterator_or_bytes:
size += len(chunk)
writer.write(chunk)
return size

def fetch(self, mode, filename, value, read):
"""Convert fields `mode`, `filename`, and `value` from Cache table to
Expand Down