Skip to content

Commit 4044984

Browse files
committed
Improve performance when reading 28 bit nodes
This seems to provide a 2-3% improvement in performance. The code is also a bit simpler.
1 parent 2a4eb44 commit 4044984

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

maxminddb/reader.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import struct
1717

18-
from maxminddb.compat import byte_from_int, compat_ip_address, string_type
18+
from maxminddb.compat import compat_ip_address, string_type
1919
from maxminddb.const import MODE_AUTO, MODE_MMAP, MODE_FILE, MODE_MEMORY, MODE_FD
2020
from maxminddb.decoder import Decoder
2121
from maxminddb.errors import InvalidDatabaseError
@@ -179,15 +179,13 @@ def _read_node(self, node_number, index):
179179
offset = base_offset + index * 3
180180
node_bytes = b'\x00' + self._buffer[offset:offset + 3]
181181
elif record_size == 28:
182-
(middle, ) = struct.unpack(
183-
b'!B', self._buffer[base_offset + 3:base_offset + 4])
182+
offset = base_offset + 3 * index
183+
node_bytes = bytearray(self._buffer[offset:offset + 4])
184184
if index:
185-
middle &= 0x0F
185+
node_bytes[0] = 0x0F & node_bytes[0]
186186
else:
187-
middle = (0xF0 & middle) >> 4
188-
offset = base_offset + index * 4
189-
node_bytes = byte_from_int(middle) + self._buffer[offset:offset +
190-
3]
187+
middle = (0xF0 & node_bytes.pop()) >> 4
188+
node_bytes.insert(0, middle)
191189
elif record_size == 32:
192190
offset = base_offset + index * 4
193191
node_bytes = self._buffer[offset:offset + 4]

0 commit comments

Comments
 (0)