-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize NestedPackages by deferring sorting #513
Open
BasiqueEvangelist
wants to merge
2
commits into
FabricMC:master
Choose a base branch
from
BasiqueEvangelist:optimize-nested-packages
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−34
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
enigma-swing/src/main/java/cuchaz/enigma/gui/node/SortedMutableTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package cuchaz.enigma.gui.node; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.Enumeration; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import javax.swing.tree.DefaultMutableTreeNode; | ||
import javax.swing.tree.MutableTreeNode; | ||
import javax.swing.tree.TreeNode; | ||
|
||
import com.google.common.collect.Iterables; | ||
|
||
/** | ||
* A MutableTreeNode whose contents are always guaranteed to be sorted with the given comparator. | ||
*/ | ||
public class SortedMutableTreeNode extends DefaultMutableTreeNode { | ||
private final Comparator<TreeNode> comparator; | ||
private final List<TreeNode> children; | ||
private boolean isSorted = true; | ||
|
||
public SortedMutableTreeNode(Comparator<TreeNode> comparator) { | ||
this.comparator = comparator; | ||
this.children = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public void insert(MutableTreeNode child, int index) { | ||
if (child == null) { | ||
throw new IllegalArgumentException("child is null"); | ||
} | ||
|
||
MutableTreeNode oldParent = (MutableTreeNode) child.getParent(); | ||
|
||
if (oldParent != null) { | ||
oldParent.remove(child); | ||
} | ||
|
||
child.setParent(this); | ||
this.children.add(child); | ||
this.isSorted = false; | ||
} | ||
|
||
private void checkSorted() { | ||
if (!this.isSorted) { | ||
this.isSorted = true; | ||
this.children.sort(this.comparator); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove(int index) { | ||
checkSorted(); | ||
|
||
remove((MutableTreeNode) getChildAt(index)); | ||
} | ||
|
||
@Override | ||
public void remove(MutableTreeNode node) { | ||
this.children.remove(node); | ||
node.setParent(null); | ||
} | ||
|
||
@Override | ||
public TreeNode getChildAt(int childIndex) { | ||
checkSorted(); | ||
|
||
return this.children.get(childIndex); | ||
} | ||
|
||
@Override | ||
public int getChildCount() { | ||
return this.children.size(); | ||
} | ||
|
||
@Override | ||
public int getIndex(TreeNode node) { | ||
return Iterables.indexOf(this.children, other -> this.comparator.compare(node, other) == 0); | ||
} | ||
|
||
@Override | ||
public boolean getAllowsChildren() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isLeaf() { | ||
return this.children.isEmpty(); | ||
} | ||
|
||
@Override | ||
public Enumeration<TreeNode> children() { | ||
Iterator<TreeNode> iter = this.children.iterator(); | ||
BasiqueEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return new Enumeration<>() { | ||
@Override | ||
public boolean hasMoreElements() { | ||
return iter.hasNext(); | ||
} | ||
|
||
@Override | ||
public TreeNode nextElement() { | ||
return iter.next(); | ||
} | ||
}; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this call
checkSorted()
as well, since the order of the node in the list before and after sorting may be different? (Or, if this is intended, perhaps a comment is in order.)Also, why use
Iterables.indexOf
instead ofList.indexOf
? If the comparator is known to be consistent withequals
(and the javadoc forComparator
recommends that comparators used with sorted collections be consistent with equals), then both ways should be equivalent. (Theoretically,List.indexOf
would be faster by virtue of theList
implementation taking advantage of impl. details.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't the
ClassEntry
comparator be inconsistent withequals
, though?In any case, I'm probably just going to turn this into a manual for loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, the
ClassEntry
comparators -- defined inClassSelector#DEOBF_CLASS_COMPARATOR
and used inDeobfPanel#<init>
, and defined as a lambda inObfPanel#<init>
-- are both consistent withequals
.ClassEntry#equals
checks if the parent and class name of the entry are equivalent; the comparators above compare using the full name of the class1, which is derived from the class name of the entry and of its parent. Because of that, if twoClassEntry
s are equal in terms ofequals
, the comparators will also declare them equivalent (returning0
).Though, that is the present state of the code; the future may possibly bring comparators inconsistent with
equals
, but I think that's improbable (and would already cause problems anyway, because then you'd have entries that don't equal themselves or are equal to other entries according to the comparator).Footnotes
In the
ObfPanel
comparator's case which compares the lengths of the full names first: if the two full names are equivalent, then necessarily the lengths are also the same. ↩