Skip to content

Commit 0518de7

Browse files
committed
seek+tell and cached-seek optimisation
1 parent 8ce12b5 commit 0518de7

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

kaitaistruct.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,20 @@ def pos(self):
9595
return self._io.tell()
9696

9797
def size(self):
98+
if hasattr(self, 'size_cached'):
99+
return self.size_cached
98100
# Python has no internal File object API function to get
99101
# current file / StringIO size, thus we use the following
100102
# trick.
101103
io = self._io
102104
# Remember our current position
103105
cur_pos = io.tell()
104106
# Seek to the end of the File object
105-
io.seek(0, SEEK_END)
106107
# Remember position, which is equal to the full length
107-
full_size = io.tell()
108+
full_size = io.seek(0, SEEK_END)
108109
# Seek back to the current position
109110
io.seek(cur_pos)
111+
self.size_cached = full_size
110112
return full_size
111113

112114
# ========================================================================

0 commit comments

Comments
 (0)