From 0a561193abd0fdf15c71361a608f6b8083935bb6 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 16 Oct 2014 20:00:46 +0200 Subject: [PATCH] Speedup BlendFile.find_block_from_offset (use hash) --- packer/blendfile.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packer/blendfile.py b/packer/blendfile.py index 56d20bf..1e6f571 100644 --- a/packer/blendfile.py +++ b/packer/blendfile.py @@ -99,6 +99,8 @@ class BlendFile: # dict {b'StructName': sdna_index} # (where the index is an index into 'structs') "sdna_index_from_id", + # dict {addr_old: block} + "block_from_offset", # int "code_index", # bool (did we make a change) @@ -131,6 +133,9 @@ class BlendFile: self.is_modified = False self.blocks.append(block) + # cache (could lazy init, incase we never use?) + self.block_from_offset = {block.addr_old: block for block in self.blocks} + def find_blocks_from_code(self, code): assert(type(code) == bytes) @@ -139,10 +144,10 @@ class BlendFile: return self.code_index[code] def find_block_from_offset(self, offset): - for block in self.blocks: - if block.addr_old == offset: - return block - return None + # same as looking looping over all blocks, + # then checking ``block.addr_old == offset`` + assert(type(offset) is int) + return self.block_from_offset.get(offset) def close(self): """ @@ -594,7 +599,6 @@ class DNAStruct: dna_type = field.dna_type dna_name = field.dna_name - print(dna_type.dna_type_id) if dna_name.is_pointer: return DNA_IO.read_pointer(handle, header) elif dna_type.dna_type_id == b'int':