Skip to content

Commit

Permalink
speed optimization on ListBlock.index_by_id
Browse files Browse the repository at this point in the history
  • Loading branch information
MosesofEgypt committed Jan 21, 2025
1 parent 14f85f9 commit d9c7967
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions supyr_struct/blocks/list_block.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'''
'''
from copy import deepcopy
from itertools import takewhile
from sys import getsizeof
from types import MethodType

from supyr_struct.blocks.block import Block
from supyr_struct.defs.constants import DEF_SHOW, ALL_SHOW, SHOW_SETS,\
Expand Down Expand Up @@ -423,12 +423,14 @@ def index_by_id(self, node):
Returns the index that node is in.
Raises ValueError if node can not be found.
'''
return list(
map(id,
# NOTE: using list.__getitem__ to maximize speed
map(MethodType(list.__getitem__, self),
range(len(self))
))).index(id(node))
index_finder = takewhile(
id(node).__ne__, map(id, self)
)
index = sum(map(bool, index_finder))
if index < len(self):
return index

raise ValueError("Item '%s' is not in the list" % node)

def get_size(self, attr_index=None, **context):
'''
Expand Down

0 comments on commit d9c7967

Please sign in to comment.