Skip to content
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 Body.collectUses using stream and group by #1144

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions sootup.core/src/main/java/sootup/core/model/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair;
import sootup.core.graph.MutableBlockStmtGraph;
import sootup.core.graph.MutableStmtGraph;
import sootup.core.graph.StmtGraph;
Expand Down Expand Up @@ -497,21 +499,16 @@ public static Map<LValue, Collection<Stmt>> collectDefs(Collection<Stmt> stmts)
}

/**
* Collects all using statements of a Local from a list of statements
* Collects all using statements of a Values from a list of statements
*
* @param stmts The searched list of statements
* @return A map of Locals and their using statements
* @return A map of Values and their using statements
*/
public static Map<Value, Collection<Stmt>> collectUses(Collection<Stmt> stmts) {
Map<Value, Collection<Stmt>> allUses = new HashMap<>();
for (Stmt stmt : stmts) {
for (Iterator<Value> iterator = stmt.getUses().iterator(); iterator.hasNext(); ) {
Value value = iterator.next();
Collection<Stmt> localUses = allUses.computeIfAbsent(value, key -> new ArrayList<>());
localUses.add(stmt);
allUses.put(value, localUses);
}
}
return allUses;
public static Map<Value, List<Stmt>> collectUses(Collection<Stmt> stmts) {
return stmts.stream()
.flatMap(stmt -> stmt.getUses().map(value -> (Pair.of(value, stmt))))
.collect(
Collectors.groupingBy(
Pair::getLeft, Collectors.mapping(Pair::getRight, Collectors.toList())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Aggregator(boolean dontAggregateFieldLocals) {
public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) {
MutableStmtGraph graph = builder.getStmtGraph();
List<Stmt> stmts = builder.getStmts();
Map<Value, Collection<Stmt>> usesMap = Body.collectUses(stmts);
Map<Value, List<Stmt>> usesMap = Body.collectUses(stmts);

for (Stmt stmt : stmts) {
if (!(stmt instanceof JAssignStmt)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view)
return;
}

Map<Value, Collection<Stmt>> essentialUses = Body.collectUses(essentialStmts);
Map<Value, List<Stmt>> essentialUses = Body.collectUses(essentialStmts);
// Eliminate dead assignments from invokes such as x = f(), where x is no longer used
List<JAssignStmt> postProcess = new ArrayList<>();
for (Stmt stmt : stmts) {
Expand Down
Loading