Skip to content

Commit

Permalink
cache the stack items
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarv committed Dec 10, 2017
1 parent 7daf24a commit 7f0c63c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.dkarv.jdcallgraph;

import com.dkarv.jdcallgraph.callgraph.CallGraph;
import com.dkarv.jdcallgraph.util.StackItemCache;
import com.dkarv.jdcallgraph.util.log.Logger;
import com.dkarv.jdcallgraph.util.StackItem;

Expand All @@ -49,7 +50,8 @@ public static void beforeMethod(String className, String methodName, int lineNum
graph = new CallGraph(threadId);
GRAPHS.put(threadId, graph);
}
graph.called(new StackItem(className, methodName, lineNumber, isTest));
StackItem item = StackItemCache.get(className, methodName, lineNumber, isTest);
graph.called(item);
} catch (Throwable e) {
LOG.error("Error in beforeMethod", e);
}
Expand All @@ -65,7 +67,8 @@ public static void afterMethod(String className, String methodName, int lineNumb
// not interesting
return;
}
graph.returned(new StackItem(className, methodName, lineNumber, isTest));
StackItem item = StackItemCache.get(className, methodName, lineNumber, isTest);
graph.returned(item);
} catch (Throwable e) {
LOG.error("Error in afterMethod", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.dkarv.jdcallgraph.callgraph.CallGraph;
import com.dkarv.jdcallgraph.data.DataDependenceGraph;
import com.dkarv.jdcallgraph.util.StackItem;
import com.dkarv.jdcallgraph.util.StackItemCache;
import com.dkarv.jdcallgraph.util.config.*;
import com.dkarv.jdcallgraph.util.log.Logger;
import com.dkarv.jdcallgraph.util.options.*;
Expand Down Expand Up @@ -63,7 +64,8 @@ public static void write(String fromClass, String fromMethod, int lineNumber, St
graph = new DataDependenceGraph(threadId);
GRAPHS.put(threadId, graph);
}
graph.addWrite(new StackItem(fromClass, fromMethod, lineNumber, false),
StackItem item = StackItemCache.get(fromClass, fromMethod, lineNumber, false);
graph.addWrite(item,
fieldClass + "::" + fieldName);
} catch (Exception e) {
LOG.error("Error in write", e);
Expand All @@ -82,11 +84,11 @@ public static void read(String fromClass, String fromMethod, int lineNumber, Str
}
if (needCombined) {
CallGraph callGraph = CallRecorder.GRAPHS.get(threadId);
graph.addRead(new StackItem(fromClass, fromMethod, lineNumber, false), fieldClass + "::" +
fieldName, callGraph);
StackItem item = StackItemCache.get(fromClass, fromMethod, lineNumber, false);
graph.addRead(item, fieldClass + "::" + fieldName, callGraph);
} else {
graph.addRead(new StackItem(fromClass, fromMethod, lineNumber, false), fieldClass + "::" +
fieldName, null);
StackItem item = StackItemCache.get(fromClass, fromMethod, lineNumber, false);
graph.addRead(item, fieldClass + "::" + fieldName, null);
}
} catch (Exception e) {
LOG.error("Error in read", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* MIT License
* <p>
* Copyright (c) 2017 David Krebs
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.dkarv.jdcallgraph.util;

import java.util.*;

public class StackItemCache {
private static final Map<String, Map<String, StackItem>> CACHE = new HashMap<>();

public static StackItem get(String type, String method, int lineNumber, boolean isTest) {
Map<String, StackItem> methodMap = getBy(type);
StackItem item = methodMap.get(method);
if (item == null) {
item = new StackItem(type, method, lineNumber, isTest);
methodMap.put(method, item);
}
return item;
}

private static Map<String, StackItem> getBy(String type) {
Map<String, StackItem> map = CACHE.get(type);
if (map == null) {
map = new HashMap<>();
CACHE.put(type, map);
}
return map;
}
}

0 comments on commit 7f0c63c

Please sign in to comment.