Skip to content

Commit

Permalink
Merge pull request #94 from nschloe/handle-loops
Browse files Browse the repository at this point in the history
Handle loops
  • Loading branch information
nschloe authored Dec 20, 2020
2 parents 59663ff + 9548fa4 commit 213be8b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = tuna
version = 0.5.2
version = 0.5.3
author = Nico Schlömer
author_email = [email protected]
description = Visualize Python performance profiles
Expand Down
14 changes: 9 additions & 5 deletions tuna/_runtime_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def read_runtime_profile(prof_filename):
for parent in parents:
children[parent].append(key)

def populate(key, parent):
def populate(key, parent, all_ancestors):
# stats.stats[key] returns a tuple of length 5 with the following data:
# [0]: total calls
# [1]: prim calls
Expand All @@ -61,10 +61,14 @@ def populate(key, parent):

# Convert the tuple key into a string
name = "{}::{}::{}".format(*key)

if key in all_ancestors:
# avoid loops
return {}

if len(parent_times) <= 1:
# Handle children
# merge dictionaries
c = [populate(child, key) for child in children[key]]
c = [populate(child, key, all_ancestors + [key]) for child in children[key]]
c.append(
{
"text": [name + "::self", f"{selftime:.3} s"],
Expand Down Expand Up @@ -94,14 +98,14 @@ def populate(key, parent):
return {"text": [name, f"{selftime:.3f}"], "color": 0, "value": selftime}

if len(roots) == 1:
data = populate(roots[0], None)
data = populate(roots[0], None, [])
else:
# If there is more than one root, add an artificial "master root" item that is
# parent to all roots.
assert len(roots) > 1
data = {
"text": ["root"],
"color": 0,
"children": [populate(root, None) for root in roots],
"children": [populate(root, None, []) for root in roots],
}
return data

0 comments on commit 213be8b

Please sign in to comment.