Skip to content

Commit

Permalink
Fix ClassTree significant performance issue due to tree grabbing all …
Browse files Browse the repository at this point in the history
…childs from parents

Short description of what happened here. The stack kept adding the parents and the childrens of the popped value from the stack.

So naturally it went like this:
ClassA -> ClassAParent -> Object -> Proceeds to add EVERY CHILD TO OBJECT AND THEIR CHILDREN TOO

The fix? Easy one. Split it into it's own while statement.
  • Loading branch information
terminalsin committed Jan 5, 2022
1 parent ec5fda1 commit f43c1cc
Showing 1 changed file with 10 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public class ClassTree extends FastDirectedGraph<ClassNode, InheritanceEdge> {
private final ApplicationClassSource source;
private final ClassNode rootNode;
private final boolean allowPhantomClasses;

private final Map<ClassNode, List<ClassNode>> parentCache = new HashMap<>();
private final Map<ClassNode, List<ClassNode>> childCache = new HashMap<>();


public ClassTree(ApplicationClassSource source) {
this(source, ALLOW_PHANTOM_CLASSES);
}
Expand Down Expand Up @@ -104,31 +101,15 @@ public List<ClassNode> getAllParents(ClassNode cn) {
if(!containsVertex(cn)) {
return new ArrayList<>();
}

List<ClassNode> classNodes = parentCache.get(cn);

if (classNodes == null) {
classNodes = SimpleDfs.topoorder(this, cn, false);
parentCache.put(cn, classNodes);
}

return classNodes;
return SimpleDfs.topoorder(this, cn, false);
}

// returns a postorder traversal of the graph starting from cn following edges in opposite direction.
public List<ClassNode> getAllChildren(ClassNode cn) {
if(!containsVertex(cn)) {
return new ArrayList<>();
}

List<ClassNode> classNodes = childCache.get(cn);

if (classNodes == null) {
classNodes = SimpleDfs.postorder(this, cn, true);
childCache.put(cn, classNodes);
}

return classNodes;
return SimpleDfs.postorder(this, cn, true);
}

/**
Expand All @@ -142,10 +123,16 @@ public Collection<ClassNode> getAllBranches(ClassNode cn) {
while (!queue.isEmpty()) {
ClassNode next = queue.remove();
if (results.add(next) && next != rootNode) {
queue.addAll(getAllParents(next));
queue.addAll(getAllChildren(next));
}
}
queue.add(cn);
while (!queue.isEmpty()) {
ClassNode next = queue.remove();
if (results.add(next) && next != rootNode) {
queue.addAll(getAllParents(next));
}
}
return results;
}

Expand Down

0 comments on commit f43c1cc

Please sign in to comment.