From dd389f23ee743ffbea32b8468ba4270f6e679115 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 10 Dec 2024 15:24:59 -0600 Subject: [PATCH] scripts: Switched to sorted sets for result notes God, I wish Python had an OrderedSet. This is a fix for duplicate "cycle detected" notes when using -t/--hot. This mix of merging both _hot_notes and _notes in the HotResult class is tricky when the underlying container is a list. The order is unlikely to be guaranteed anyways, when different results with different notes are folded. And if we ever want more control over the order of notes in result scripts we can always change this back later. --- scripts/code.py | 10 +++++----- scripts/cov.py | 10 +++++----- scripts/csv.py | 10 +++++----- scripts/ctx.py | 25 ++++++++++++------------- scripts/data.py | 10 +++++----- scripts/perf.py | 10 +++++----- scripts/perfbd.py | 10 +++++----- scripts/stack.py | 21 ++++++++++----------- scripts/structs.py | 10 +++++----- 9 files changed, 57 insertions(+), 59 deletions(-) diff --git a/scripts/code.py b/scripts/code.py index 042866d4..0433a45e 100755 --- a/scripts/code.py +++ b/scripts/code.py @@ -695,9 +695,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -707,7 +707,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -734,7 +734,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -890,7 +890,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: diff --git a/scripts/cov.py b/scripts/cov.py index 0dfb2c55..4e5974e8 100755 --- a/scripts/cov.py +++ b/scripts/cov.py @@ -429,9 +429,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -441,7 +441,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -468,7 +468,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -624,7 +624,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: diff --git a/scripts/csv.py b/scripts/csv.py index ed5c2ca2..097c9da3 100755 --- a/scripts/csv.py +++ b/scripts/csv.py @@ -1444,9 +1444,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -1456,7 +1456,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -1483,7 +1483,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -1639,7 +1639,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: diff --git a/scripts/ctx.py b/scripts/ctx.py index 35a2defd..dc1da3cd 100755 --- a/scripts/ctx.py +++ b/scripts/ctx.py @@ -148,7 +148,7 @@ class CtxResult(co.namedtuple('CtxResult', [ RInt(size), i, children if children is not None else [], - notes if notes is not None else []) + notes if notes is not None else set()) def __add__(self, other): return CtxResult(self.file, self.function, @@ -157,8 +157,7 @@ class CtxResult(co.namedtuple('CtxResult', [ else other.i if self.i is None else min(self.i, other.i), self.children + other.children, - list(co.OrderedDict.fromkeys(it.chain( - self.notes, other.notes)).keys())) + self.notes | other.notes) def openio(path, mode='r', buffering=-1): @@ -682,7 +681,7 @@ def collect(obj_paths, *, def childrenof(entry, seen=set()): # found a cycle? stop here if entry.off in seen: - return [], ['cycle detected'], True + return [], {'cycle detected'}, True # cached? if not hasattr(childrenof, 'cache'): childrenof.cache = {} @@ -691,7 +690,7 @@ def collect(obj_paths, *, # pointer? deref and include size if entry.tag == 'DW_TAG_pointer_type': - children, notes, dirty = [], [], False + children, notes, dirty = [], set(), False if 'DW_AT_type' in entry: type = info[int(entry['DW_AT_type'].strip('<>'), 0)] # skip modifiers to try to find name @@ -718,7 +717,7 @@ def collect(obj_paths, *, elif entry.tag in { 'DW_TAG_structure_type', 'DW_TAG_union_type'}: - children, notes, dirty = [], [], False + children, notes, dirty = [], set(), False for child in entry.children: if child.tag != 'DW_TAG_member': continue @@ -740,7 +739,7 @@ def collect(obj_paths, *, elif entry.tag in { 'DW_TAG_base_type', 'DW_TAG_subroutine_type'}: - children, notes, dirty = [], [], False + children, notes, dirty = [], set(), False # a modifier? elif (entry.tag in { 'DW_TAG_typedef', @@ -758,7 +757,7 @@ def collect(obj_paths, *, # void? elif ('DW_AT_type' not in entry and 'DW_AT_byte_size' not in entry): - children, notes = [], [], False + children, notes = [], set(), False else: assert False, "Unknown dwarf entry? %r" % entry.tag @@ -904,9 +903,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -916,7 +915,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -943,7 +942,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -1099,7 +1098,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: diff --git a/scripts/data.py b/scripts/data.py index 2d55ade4..52b0dc23 100755 --- a/scripts/data.py +++ b/scripts/data.py @@ -695,9 +695,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -707,7 +707,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -734,7 +734,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -890,7 +890,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: diff --git a/scripts/perf.py b/scripts/perf.py index d2daeff1..27e8906d 100755 --- a/scripts/perf.py +++ b/scripts/perf.py @@ -875,9 +875,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -887,7 +887,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -914,7 +914,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -1070,7 +1070,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: diff --git a/scripts/perfbd.py b/scripts/perfbd.py index 751b4a3d..00f37d66 100755 --- a/scripts/perfbd.py +++ b/scripts/perfbd.py @@ -845,9 +845,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -857,7 +857,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -884,7 +884,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -1040,7 +1040,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: diff --git a/scripts/stack.py b/scripts/stack.py index 625971b6..ca0a8a3d 100755 --- a/scripts/stack.py +++ b/scripts/stack.py @@ -146,15 +146,14 @@ class StackResult(co.namedtuple('StackResult', [ return super().__new__(cls, file, function, RInt(frame), RInt(limit), children if children is not None else [], - notes if notes is not None else []) + notes if notes is not None else set()) def __add__(self, other): return StackResult(self.file, self.function, self.frame + other.frame, max(self.limit, other.limit), self.children + other.children, - list(co.OrderedDict.fromkeys(it.chain( - self.notes, other.notes)).keys())) + self.notes | other.notes) def openio(path, mode='r', buffering=-1): @@ -768,7 +767,7 @@ def collect(obj_paths, ci_paths, *, def childrenof(node, seen=set()): # found a cycle? stop here if node.name in seen: - return [], ['cycle detected'], True + return [], {'cycle detected'}, True # cached? if not hasattr(childrenof, 'cache'): childrenof.cache = {} @@ -791,8 +790,8 @@ def collect(obj_paths, ci_paths, *, dirty = dirty or dirty_ if not dirty: - childrenof.cache[node.name] = children, [], dirty - return children, [], dirty + childrenof.cache[node.name] = children, set(), dirty + return children, set(), dirty results = [] for obj_path in obj_paths: @@ -935,9 +934,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -947,7 +946,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -974,7 +973,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -1130,7 +1129,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: diff --git a/scripts/structs.py b/scripts/structs.py index 32862622..7a08ed7d 100755 --- a/scripts/structs.py +++ b/scripts/structs.py @@ -717,9 +717,9 @@ def table(Result, results, diff_results=None, *, self = HotResult._make(r) self._hot_i = i self._hot_children = children if children is not None else [] - self._hot_notes = notes if notes is not None else [] + self._hot_notes = notes if notes is not None else set() if hasattr(Result_, '_notes'): - self._hot_notes.extend(getattr(r, r._notes)) + self._hot_notes.update(getattr(r, r._notes)) return self def __add__(self, other): @@ -729,7 +729,7 @@ def table(Result, results, diff_results=None, *, else other._hot_i if self._hot_i is None else min(self._hot_i, other._hot_i), self._hot_children + other._hot_children, - self._hot_notes + other._hot_notes) + self._hot_notes | other._hot_notes) results_ = [] for r in results: @@ -756,7 +756,7 @@ def table(Result, results, diff_results=None, *, # found a cycle? if (detect_cycles and tuple(getattr(r, k) for k in Result._by) in seen): - hot_[-1]._hot_notes.append('cycle detected') + hot_[-1]._hot_notes.add('cycle detected') return # recurse? @@ -912,7 +912,7 @@ def table(Result, results, diff_results=None, *, getattr(diff_r, k, None))))) # append any notes if hasattr(Result, '_notes') and r is not None: - notes = getattr(r, Result._notes) + notes = sorted(getattr(r, Result._notes)) if isinstance(entry[-1], tuple): entry[-1] = (entry[-1][0], entry[-1][1] + notes) else: