8000 Read only `used` bytes from MmapedDict files, not all the zeroes too · benxiaolang-hacker/client_python@0f544eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f544eb

Browse files
committed
Read only used bytes from MmapedDict files, not all the zeroes too
Signed-off-by: Aarni Koskela <akx@iki.fi>
< 8000 /div>
1 parent 0cfb054 commit 0f544eb

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

prometheus_client/mmap_dict.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ def __init__(self, filename, read_mode=False):
8181
@staticmethod
8282
def read_all_values_from_file(filename):
8383
with open(filename, 'rb') as infp:
84-
data = infp.read()
85-
return _read_all_values(data)
84+
# Read the first block of data, including the first 4 bytes which tell us
85+
# how much of the file (which is preallocated to _INITIAL_MMAP_SIZE bytes) is occupied.
86+
data = infp.read(65535)
87+
used = _unpack_integer(data, 0)[0]
88+
if used > len(data): # Then read in the rest, if needed.
89+
data += infp.read(used - len(data))
90+
return _read_all_values(data, used)
8691

8792
def _init_value(self, key):
8893
"""Initialize a value. Lock must be held by caller."""

0 commit comments

Comments
 (0)
0