-
Notifications
You must be signed in to change notification settings - Fork 39
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
Improve Refaster matching algorithm #104
Conversation
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.
Some context.
refasterTemplatesTree.collectCandidateTemplates( | ||
sourceIdentifiers.asList(), candidateRules::add); | ||
|
||
// XXX: Remove these debug lines |
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.
Left it here, such that you can still easily verify the changes.
@@ -2,7 +2,6 @@ | |||
|
|||
import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap; | |||
import static java.util.function.Function.identity; | |||
import static java.util.function.Predicate.not; |
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.
The changes in this file are to do a quick and easy verification of the changes. It only runs the tests and templates of the LongStream
template collection. These changes can be reverted once we are done with the PR.
@@ -161,6 +381,7 @@ private static Stream<Replacement> getReplacements( | |||
return description.fixes.stream().flatMap(fix -> fix.getReplacements(endPositions).stream()); | |||
} | |||
|
|||
// XXX: Instead create an `ImmutableList<RefasterRule>` |
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.
This is (likely) easier to pick up when we also merge #43.
18a783e
to
87dfe9c
Compare
87dfe9c
to
8285bd6
Compare
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.
Did a first pass; rebased and added a commit.
import java.util.TreeMap; | ||
|
||
@AutoValue | ||
abstract class BuildNode<T> { |
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.
I think we can make this an inner class of Node
.
return new AutoValue_BuildNode<>(new TreeMap<>(), new HashSet<>()); | ||
} | ||
|
||
static <T> BuildNode<T> create(SortedMap<String, BuildNode<T>> children, Set<T> candidateRules) { |
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.
This method seems unused.
} | ||
|
||
@SuppressWarnings("AutoValueImmutableFields" /* The BuildNode is used to construct the tree. */) | ||
public abstract SortedMap<String, BuildNode<T>> children(); |
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.
public abstract SortedMap<String, BuildNode<T>> children(); | |
abstract SortedMap<String, BuildNode<T>> children(); |
(We should check why Checkstyle or Error Prone didn't flag this.)
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.
This map doesn't need to be sorted; using a regular HashMap
will be more efficient.
public abstract SortedMap<String, BuildNode<T>> children(); | ||
|
||
@SuppressWarnings("AutoValueImmutableFields" /* The BuildNode is used to construct the tree. */) | ||
public abstract Set<T> candidateRules(); |
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.
Node
should not talk about rules; it's generic and be used for other purposes.
@AutoValue | ||
abstract class BuildNode<T> { | ||
static <T> BuildNode<T> create() { | ||
return new AutoValue_BuildNode<>(new TreeMap<>(), new HashSet<>()); |
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.
We might want to use Collections.newSetFromMap(new IdentityHashMap<>())
instead of a HashSet
, or even just ArrayList
. TBD how much time we spend on the equality checks.
case POSTFIX_INCREMENT: | ||
case PREFIX_INCREMENT: | ||
return "++"; | ||
case POSTFIX_DECREMENT: | ||
case PREFIX_DECREMENT: | ||
return "--"; |
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.
We can easily deduplicate these, e.g. like so:
case POSTFIX_INCREMENT: | |
case PREFIX_INCREMENT: | |
return "++"; | |
case POSTFIX_DECREMENT: | |
case PREFIX_DECREMENT: | |
return "--"; | |
case POSTFIX_INCREMENT: | |
return "x++"; | |
case PREFIX_INCREMENT: | |
return "++x"; | |
case POSTFIX_DECREMENT: | |
return "x--"; | |
case PREFIX_DECREMENT: | |
return "--x"; |
// XXX: List needs to be extended, as it currently only supports `BinaryTree`s and `UnaryTree`s. | ||
static String treeKindToString(Tree.Kind kind) { |
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.
Arguably this code should live in RefasterCheck
; especially if we make sure that each kind has a unique string representation, then there are currently no other uses for it.
return Description.NO_MATCH; | ||
for (RefasterRule<?, ?> rule : candidateRules) { | ||
try { | ||
rule.apply(state.getPath(), new SubContext(state.context), matches::add); |
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.
I think that the new SubContext(state.context)
can be reused.
String operator = Util.treeKindToString(node.getKind()); | ||
identifierCombinations.forEach(ids -> ids.add(operator)); |
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.
Here we can inline operator
, like below. (That, said, it seems we can deduplicate some logic here.)
8285bd6
to
e914f9d
Compare
e914f9d
to
dac45fb
Compare
We can close this in favor of: #261. The important changes are ported to that PR. |
(Will add a more detailed explanation later)
(The PR cannot succeed because of the required Error Prone changes, see branch:
sschroevers/refaster-tree-tweaks