Performance improvement: use struct.unpack instead of manually constructing bytes when possible
This commit is contained in:
parent
b10dc5faef
commit
77da7b16b6
|
@ -228,7 +228,15 @@ class V9DataFlowSet:
|
||||||
continue
|
continue
|
||||||
new_record.data[fkey] = ip.compressed
|
new_record.data[fkey] = ip.compressed
|
||||||
else:
|
else:
|
||||||
# Better solution than struct.unpack with variable field length
|
# For performance reasons, we use struct.unpack for known lengths:
|
||||||
|
if flen == 4:
|
||||||
|
new_record.data[fkey], = struct.unpack('!L', dataslice)
|
||||||
|
elif flen == 2:
|
||||||
|
new_record.data[fkey], = struct.unpack('!H', dataslice)
|
||||||
|
elif flen == 1:
|
||||||
|
new_record.data[fkey], = struct.unpack('!B', dataslice)
|
||||||
|
else:
|
||||||
|
# Caveat: this code assumes little-endian system (like x86)
|
||||||
fdata = 0
|
fdata = 0
|
||||||
for idx, byte in enumerate(reversed(bytearray(dataslice))):
|
for idx, byte in enumerate(reversed(bytearray(dataslice))):
|
||||||
fdata += byte << (idx * 8)
|
fdata += byte << (idx * 8)
|
||||||
|
|
Loading…
Reference in a new issue