From b10dc5faeffc2efaa38aea6c0cf1783321c4fb33 Mon Sep 17 00:00:00 2001 From: Anze Date: Sun, 8 May 2022 17:52:51 +0200 Subject: [PATCH] Performance improvement: rearrange code so that instead of converting IP addresses to integers first, we construct them from bytes directly --- netflow/v9.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/netflow/v9.py b/netflow/v9.py index 22e04d2..1ad90fe 100644 --- a/netflow/v9.py +++ b/netflow/v9.py @@ -218,21 +218,20 @@ class V9DataFlowSet: # The length of the value byte slice is defined in the template dataslice = data[offset:offset + flen] - # Better solution than struct.unpack with variable field length - fdata = 0 - for idx, byte in enumerate(reversed(bytearray(dataslice))): - fdata += byte << (idx * 8) - # Special handling of IP addresses to convert integers to strings to not lose precision in dump # TODO: might only be needed for IPv6 if field.field_type in FIELD_TYPES_CONTAINING_IP: try: - ip = ipaddress.ip_address(fdata) + ip = ipaddress.ip_address(dataslice) except ValueError: print("IP address could not be parsed: {}".format(fdata)) continue new_record.data[fkey] = ip.compressed else: + # Better solution than struct.unpack with variable field length + fdata = 0 + for idx, byte in enumerate(reversed(bytearray(dataslice))): + fdata += byte << (idx * 8) new_record.data[fkey] = fdata offset += flen