Skip to content

Commit 21aac0d

Browse files
authored
Merge pull request #839 from MarcMil/faster-sysclass
Summaries: Cache the XMLInputFactory
2 parents b33697e + cbe51d4 commit 21aac0d

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

soot-infoflow-summaries/src/soot/jimple/infoflow/methodSummary/xml/SummaryReader.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.FileReader;
1919
import java.io.IOException;
2020
import java.io.Reader;
21+
import java.lang.ref.WeakReference;
2122
import java.util.ArrayList;
2223
import java.util.HashMap;
2324
import java.util.List;
@@ -55,6 +56,30 @@ private enum State {
5556
summary, hierarchy, intf, methods, method, flow, clear, gaps, gap, constraints, key, index
5657
}
5758

59+
/**
60+
* It takes quite a while to create a new XML Input Factory
61+
*/
62+
private static class CachedFactory {
63+
WeakReference<Thread> thread;
64+
XMLInputFactory factory;
65+
66+
public CachedFactory() {
67+
68+
XMLInputFactory factory = XMLInputFactory.newInstance();
69+
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
70+
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
71+
this.factory = factory;
72+
this.thread = new WeakReference<Thread>(Thread.currentThread());
73+
}
74+
75+
public boolean isValidForThisThread() {
76+
Thread thr = thread.get();
77+
return Thread.currentThread() == thr;
78+
}
79+
}
80+
81+
private CachedFactory cachedFactory;
82+
5883
/**
5984
* Reads a summary xml and places the new summaries into the given data object.
6085
* This method closes the reader.
@@ -68,10 +93,13 @@ public void read(Reader reader, ClassMethodSummaries summaries)
6893
throws XMLStreamException, SummaryXMLException, IOException {
6994
XMLStreamReader xmlreader = null;
7095
try {
71-
XMLInputFactory factory = XMLInputFactory.newInstance();
72-
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
73-
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
74-
xmlreader = factory.createXMLStreamReader(reader);
96+
//Sadly, the XML Input Factory is not thread safe :/
97+
CachedFactory cachedFact = cachedFactory;
98+
if (cachedFact == null || !cachedFact.isValidForThisThread()) {
99+
cachedFact = new CachedFactory();
100+
this.cachedFactory = cachedFact;
101+
}
102+
xmlreader = cachedFact.factory.createXMLStreamReader(reader);
75103
final MethodSummaries summary = summaries.getMethodSummaries();
76104

77105
Map<String, String> sourceAttributes = new HashMap<String, String>();

soot-infoflow/src/soot/jimple/infoflow/AbstractInfoflow.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,7 @@ else if (reason instanceof TimeoutReason)
12571257
performanceData.updateMaxMemoryConsumption(getUsedMemory());
12581258
logger.info(String.format("Memory consumption after cleanup: %d MB", getUsedMemory()));
12591259

1260+
beforePathReconstruction = System.nanoTime();
12601261
// Reconstruct the paths from source to sink
12611262
reconstructPaths(builder, resultExecutor, res);
12621263
} finally {
@@ -1339,6 +1340,9 @@ else if (logger.isInfoEnabled()) {
13391340
*/
13401341
protected void reconstructPaths(IAbstractionPathBuilder builder, InterruptableExecutor executor,
13411342
Set<AbstractionAtSink> ifdsResults) {
1343+
if (ifdsResults.isEmpty())
1344+
return;
1345+
13421346
FlowDroidTimeoutWatcher pathTimeoutWatcher = null;
13431347

13441348
try {

0 commit comments

Comments
 (0)