From: Ronan Dunklau Date: Wed, 17 Aug 2022 12:42:21 +0000 (+0200) Subject: Cache instantiation of DWARF structs. (#435) X-Git-Tag: v0.30~23 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ea5c8879224155edb5712ff208dc1cf94ba99b3d;p=pyelftools.git Cache instantiation of DWARF structs. (#435) This takes a huge amount of time when iterating over compile units. --- diff --git a/elftools/dwarf/structs.py b/elftools/dwarf/structs.py index a74d5de..e0a67c4 100644 --- a/elftools/dwarf/structs.py +++ b/elftools/dwarf/structs.py @@ -76,8 +76,10 @@ class DWARFStructs(object): See also the documentation of public methods. """ - def __init__(self, - little_endian, dwarf_format, address_size, dwarf_version=2): + + __StructsCache = {} + + def __new__(cls, little_endian, dwarf_format, address_size, dwarf_version=2): """ dwarf_version: Numeric DWARF version @@ -91,6 +93,12 @@ class DWARFStructs(object): Target machine address size, in bytes (4 or 8). (See spec section 7.5.1) """ + key = (little_endian, dwarf_format, address_size, dwarf_version) + + if key in cls.__StructsCache: + return cls.__StructsCache[key] + + self = super().__new__(cls) assert dwarf_format == 32 or dwarf_format == 64 assert address_size == 8 or address_size == 4, str(address_size) self.little_endian = little_endian @@ -98,6 +106,8 @@ class DWARFStructs(object): self.address_size = address_size self.dwarf_version = dwarf_version self._create_structs() + cls.__StructsCache[key] = self + return self def initial_length_field_size(self): """ Size of an initial length field.