Skip to content

Commit 27b5c36

Browse files
Performance optimization for reoder_children (#383)
The corresponding User Story: https://tfs.ansys.com:8443/tfs/ANSYS_Development/Portfolio/_workitems/edit/1223942/ This PR optimizes out the old "remove an item then insert it back to the list" manner and improves the time complexity from O(n^2) to O(n). However, n should be small (< 100), so the performance improvement won't be significant in the normal reports.
1 parent 0073fd9 commit 27b5c36

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/ansys/dynamicreporting/core/utils/report_objects.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,10 @@ def from_json(self, json_dict):
445445
# ok, re-order the children list to match the order in children_order
446446
sorted_guids = self.children_order.lower().split(",")
447447
sorted_guids.reverse()
448-
for guid in sorted_guids:
449-
idx = 0
450-
for childguid in self.children:
451-
if guid == str(childguid).lower():
452-
self.children.insert(0, self.children.pop(idx))
453-
break
454-
idx += 1
448+
lower_guid_to_child = {str(child).lower(): child for child in self.children}
449+
self.children = [
450+
lower_guid_to_child[guid] for guid in sorted_guids if guid in lower_guid_to_child
451+
]
455452
# Instance specific initialization
456453
# this may have changed the guid, so
457454
if tmpguid != self.guid:
@@ -1461,15 +1458,10 @@ def get_url_data(self):
14611458
def reorder_children(self):
14621459
sorted_guids = self.children_order.lower().split(",")
14631460
sorted_guids.reverse()
1464-
# return the children based on the order of guids in children_order
1465-
for guid in sorted_guids:
1466-
if len(guid):
1467-
idx = 0
1468-
for child in self.children:
1469-
if guid == str(child).lower():
1470-
self.children.insert(0, self.children.pop(idx))
1471-
break
1472-
idx += 1
1461+
lower_guid_to_child = {str(child).lower(): child for child in self.children}
1462+
self.children = [
1463+
lower_guid_to_child[guid] for guid in sorted_guids if guid in lower_guid_to_child
1464+
]
14731465

14741466
@classmethod
14751467
def get_url_base_name(cls):

0 commit comments

Comments
 (0)