Skip to content

Commit

Permalink
avoiud repeated index lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoles committed Mar 16, 2023
1 parent 8c0437c commit 51a88d5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.pitest.mutationtest.build.intercept;

public class RegionIndex {
private final int start;
private final int end;

public RegionIndex(int start, int end) {
this.start = start;
this.end = end;
}

public int start() {
return start;
}

public int end() {
return end;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public abstract class RegionInterceptor implements MutationInterceptor {
private ClassTree currentClass;
private Map<MethodTree, List<Region>> cache;
private Map<MethodTree, List<RegionIndex>> cache;

@Override
public InterceptorType type() {
Expand All @@ -46,13 +46,18 @@ private Predicate<MutationDetails> buildPredicate() {
final int instruction = a.getInstructionIndex();
final MethodTree method = this.currentClass.method(a.getId().getLocation()).get();

List<Region> regions = cache.computeIfAbsent(method, this::computeRegions);

List<RegionIndex> regions = cache.computeIfAbsent(method, this::computeRegionIndex);
return regions.stream()
.anyMatch(r -> instruction >= method.instructions().indexOf(r.start) && instruction <= method.instructions().indexOf(r.end));
.anyMatch(r -> r.start() <= instruction && r.end() >= instruction);
};
}

private List<RegionIndex> computeRegionIndex(MethodTree method) {
return computeRegions(method).stream()
.map(r -> new RegionIndex(method.instructions().indexOf(r.start), method.instructions().indexOf(r.end)))
.collect(Collectors.toList());
}

protected abstract List<Region> computeRegions(MethodTree method);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ private static Match<AbstractInsnNode> store(SlotWrite<AbstractInsnNode> slot) {

protected List<Region> computeRegions(MethodTree method) {
Context context = Context.start();
List<Region> regions = ASSERT_GET.contextMatches(method.instructions(), context).stream()
return ASSERT_GET.contextMatches(method.instructions(), context).stream()
.map(c -> new Region(c.retrieve(START.read()).get(), c.retrieve(END.read()).get()))
.collect(Collectors.toList());
return regions;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ private static SequenceQuery<AbstractInsnNode> switchBranchSequence() {

protected List<Region> computeRegions(MethodTree method) {
Context context = Context.start();
List<Region> regions = STRING_SWITCH.contextMatches(method.instructions(), context).stream()
return STRING_SWITCH.contextMatches(method.instructions(), context).stream()
.map(c -> new Region(c.retrieve(START.read()).get(), c.retrieve(END.read()).get()))
.collect(Collectors.toList());
return regions;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,9 @@ protected List<Region> computeRegions(MethodTree method) {

Context context = Context.start(DEBUG);
context = context.store(HANDLERS.write(), handlers);
List<Region> regions = TRY_WITH_RESOURCES.contextMatches(method.instructions(), context).stream()
return TRY_WITH_RESOURCES.contextMatches(method.instructions(), context).stream()
.map(c -> new Region(c.retrieve(START.read()).get(), c.retrieve(END.read()).get()))
.collect(Collectors.toList());
return regions;
}


Expand Down

0 comments on commit 51a88d5

Please sign in to comment.