cache split result as String[], foreach optimization

http-api
gongdewei 5 years ago
parent 080eabc300
commit 67dbe49009

@ -1,7 +1,6 @@
package com.taobao.arthas.core.advisor; package com.taobao.arthas.core.advisor;
import java.arthas.SpyAPI.AbstractSpy; import java.arthas.SpyAPI.AbstractSpy;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -25,20 +24,21 @@ import com.taobao.arthas.core.util.StringUtils;
*/ */
public class SpyImpl extends AbstractSpy { public class SpyImpl extends AbstractSpy {
private static final Logger logger = LoggerFactory.getLogger(SpyImpl.class); private static final Logger logger = LoggerFactory.getLogger(SpyImpl.class);
private final Map<String, List<String>> splitCache = new ConcurrentHashMap<String, List<String>>(); private final Map<String, String[]> splitCache = new ConcurrentHashMap<String, String[]>();
@Override @Override
public void atEnter(Class<?> clazz, String methodInfo, Object target, Object[] args) { public void atEnter(Class<?> clazz, String methodInfo, Object target, Object[] args) {
ClassLoader classLoader = clazz.getClassLoader(); ClassLoader classLoader = clazz.getClassLoader();
List<String> strs = splitMethodInfo(methodInfo); String[] info = splitMethodInfo(methodInfo);
String methodName = strs.get(0); String methodName = info[0];
String methodDesc = strs.get(1); String methodDesc = info[1];
// TODO listener 只用查一次,放到 thread local里保存起来就可以了 // TODO listener 只用查一次,放到 thread local里保存起来就可以了
List<AdviceListener> listeners = AdviceListenerManager.queryAdviceListeners(classLoader, clazz.getName(), List<AdviceListener> listeners = AdviceListenerManager.queryAdviceListeners(classLoader, clazz.getName(),
methodName, methodDesc); methodName, methodDesc);
if (listeners != null) { if (listeners != null) {
for (AdviceListener adviceListener : listeners) { for (int i = 0; i < listeners.size(); i++) {
AdviceListener adviceListener = listeners.get(i);
try { try {
if (skipAdviceListener(adviceListener)) { if (skipAdviceListener(adviceListener)) {
continue; continue;
@ -58,14 +58,15 @@ public class SpyImpl extends AbstractSpy {
public void atExit(Class<?> clazz, String methodInfo, Object target, Object[] args, Object returnObject) { public void atExit(Class<?> clazz, String methodInfo, Object target, Object[] args, Object returnObject) {
ClassLoader classLoader = clazz.getClassLoader(); ClassLoader classLoader = clazz.getClassLoader();
List<String> strs = splitMethodInfo(methodInfo); String[] info = splitMethodInfo(methodInfo);
String methodName = strs.get(0); String methodName = info[0];
String methodDesc = strs.get(1); String methodDesc = info[1];
List<AdviceListener> listeners = AdviceListenerManager.queryAdviceListeners(classLoader, clazz.getName(), List<AdviceListener> listeners = AdviceListenerManager.queryAdviceListeners(classLoader, clazz.getName(),
methodName, methodDesc); methodName, methodDesc);
if (listeners != null) { if (listeners != null) {
for (AdviceListener adviceListener : listeners) { for (int i = 0; i < listeners.size(); i++) {
AdviceListener adviceListener = listeners.get(i);
try { try {
if (skipAdviceListener(adviceListener)) { if (skipAdviceListener(adviceListener)) {
continue; continue;
@ -84,14 +85,15 @@ public class SpyImpl extends AbstractSpy {
public void atExceptionExit(Class<?> clazz, String methodInfo, Object target, Object[] args, Throwable throwable) { public void atExceptionExit(Class<?> clazz, String methodInfo, Object target, Object[] args, Throwable throwable) {
ClassLoader classLoader = clazz.getClassLoader(); ClassLoader classLoader = clazz.getClassLoader();
List<String> strs = splitMethodInfo(methodInfo); String[] info = splitMethodInfo(methodInfo);
String methodName = strs.get(0); String methodName = info[0];
String methodDesc = strs.get(1); String methodDesc = info[1];
List<AdviceListener> listeners = AdviceListenerManager.queryAdviceListeners(classLoader, clazz.getName(), List<AdviceListener> listeners = AdviceListenerManager.queryAdviceListeners(classLoader, clazz.getName(),
methodName, methodDesc); methodName, methodDesc);
if (listeners != null) { if (listeners != null) {
for (AdviceListener adviceListener : listeners) { for (int i = 0; i < listeners.size(); i++) {
AdviceListener adviceListener = listeners.get(i);
try { try {
if (skipAdviceListener(adviceListener)) { if (skipAdviceListener(adviceListener)) {
continue; continue;
@ -109,23 +111,23 @@ public class SpyImpl extends AbstractSpy {
@Override @Override
public void atBeforeInvoke(Class<?> clazz, String invokeInfo, Object target) { public void atBeforeInvoke(Class<?> clazz, String invokeInfo, Object target) {
ClassLoader classLoader = clazz.getClassLoader(); ClassLoader classLoader = clazz.getClassLoader();
List<String> strs = splitInvokeInfo(invokeInfo); String[] info = splitInvokeInfo(invokeInfo);
String owner = strs.get(0); String owner = info[0];
String methodName = strs.get(1); String methodName = info[1];
String methodDesc = strs.get(2); String methodDesc = info[2];
List<AdviceListener> listeners = AdviceListenerManager.queryTraceAdviceListeners(classLoader, clazz.getName(), List<AdviceListener> listeners = AdviceListenerManager.queryTraceAdviceListeners(classLoader, clazz.getName(),
owner, methodName, methodDesc); owner, methodName, methodDesc);
if (listeners != null) { if (listeners != null) {
int lineNumber = Integer.parseInt(strs.get(3)); for (int i = 0; i < listeners.size(); i++) {
for (AdviceListener adviceListener : listeners) { AdviceListener adviceListener = listeners.get(i);
try { try {
if (skipAdviceListener(adviceListener)) { if (skipAdviceListener(adviceListener)) {
continue; continue;
} }
final InvokeTraceable listener = (InvokeTraceable) adviceListener; final InvokeTraceable listener = (InvokeTraceable) adviceListener;
listener.invokeBeforeTracing(owner, methodName, methodDesc, lineNumber); listener.invokeBeforeTracing(owner, methodName, methodDesc, Integer.parseInt(info[3]));
} catch (Throwable e) { } catch (Throwable e) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.error("class: {}, invokeInfo: {}", clazz.getName(), invokeInfo, e); logger.error("class: {}, invokeInfo: {}", clazz.getName(), invokeInfo, e);
@ -138,22 +140,22 @@ public class SpyImpl extends AbstractSpy {
@Override @Override
public void atAfterInvoke(Class<?> clazz, String invokeInfo, Object target) { public void atAfterInvoke(Class<?> clazz, String invokeInfo, Object target) {
ClassLoader classLoader = clazz.getClassLoader(); ClassLoader classLoader = clazz.getClassLoader();
List<String> strs = splitInvokeInfo(invokeInfo); String[] info = splitInvokeInfo(invokeInfo);
String owner = strs.get(0); String owner = info[0];
String methodName = strs.get(1); String methodName = info[1];
String methodDesc = strs.get(2); String methodDesc = info[2];
List<AdviceListener> listeners = AdviceListenerManager.queryTraceAdviceListeners(classLoader, clazz.getName(), List<AdviceListener> listeners = AdviceListenerManager.queryTraceAdviceListeners(classLoader, clazz.getName(),
owner, methodName, methodDesc); owner, methodName, methodDesc);
if (listeners != null) { if (listeners != null) {
int lineNumber = Integer.parseInt(strs.get(3)); for (int i = 0; i < listeners.size(); i++) {
for (AdviceListener adviceListener : listeners) { AdviceListener adviceListener = listeners.get(i);
try { try {
if (skipAdviceListener(adviceListener)) { if (skipAdviceListener(adviceListener)) {
continue; continue;
} }
final InvokeTraceable listener = (InvokeTraceable) adviceListener; final InvokeTraceable listener = (InvokeTraceable) adviceListener;
listener.invokeAfterTracing(owner, methodName, methodDesc, lineNumber); listener.invokeAfterTracing(owner, methodName, methodDesc, Integer.parseInt(info[3]));
} catch (Throwable e) { } catch (Throwable e) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.error("class: {}, invokeInfo: {}", clazz.getName(), invokeInfo, e); logger.error("class: {}, invokeInfo: {}", clazz.getName(), invokeInfo, e);
@ -167,23 +169,23 @@ public class SpyImpl extends AbstractSpy {
@Override @Override
public void atInvokeException(Class<?> clazz, String invokeInfo, Object target, Throwable throwable) { public void atInvokeException(Class<?> clazz, String invokeInfo, Object target, Throwable throwable) {
ClassLoader classLoader = clazz.getClassLoader(); ClassLoader classLoader = clazz.getClassLoader();
List<String> strs = splitInvokeInfo(invokeInfo); String[] info = splitInvokeInfo(invokeInfo);
String owner = strs.get(0); String owner = info[0];
String methodName = strs.get(1); String methodName = info[1];
String methodDesc = strs.get(2); String methodDesc = info[2];
List<AdviceListener> listeners = AdviceListenerManager.queryTraceAdviceListeners(classLoader, clazz.getName(), List<AdviceListener> listeners = AdviceListenerManager.queryTraceAdviceListeners(classLoader, clazz.getName(),
owner, methodName, methodDesc); owner, methodName, methodDesc);
if (listeners != null) { if (listeners != null) {
int lineNumber = Integer.parseInt(strs.get(3)); for (int i = 0; i < listeners.size(); i++) {
for (AdviceListener adviceListener : listeners) { AdviceListener adviceListener = listeners.get(i);
try { try {
if (skipAdviceListener(adviceListener)) { if (skipAdviceListener(adviceListener)) {
continue; continue;
} }
final InvokeTraceable listener = (InvokeTraceable) adviceListener; final InvokeTraceable listener = (InvokeTraceable) adviceListener;
listener.invokeThrowTracing(owner, methodName, methodDesc, lineNumber); listener.invokeThrowTracing(owner, methodName, methodDesc, Integer.parseInt(info[3]));
} catch (Throwable e) { } catch (Throwable e) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.error("class: {}, invokeInfo: {}", clazz.getName(), invokeInfo, e); logger.error("class: {}, invokeInfo: {}", clazz.getName(), invokeInfo, e);
@ -193,26 +195,24 @@ public class SpyImpl extends AbstractSpy {
} }
} }
private List<String> splitMethodInfo(String methodInfo) { private String[] splitMethodInfo(String methodInfo) {
return splitString(methodInfo); return splitString(methodInfo);
} }
private List<String> splitInvokeInfo(String invokeInfo) { private String[] splitInvokeInfo(String invokeInfo) {
return splitString(invokeInfo); return splitString(invokeInfo);
} }
/** /**
* split * split
* trace/watch split * trace/watch/monitor split
* List
* @param str * @param str
* @return * @return
*/ */
private List<String> splitString(String str) { private String[] splitString(String str) {
List<String> strs = splitCache.get(str); String[] strs = splitCache.get(str);
if (strs == null) { if (strs == null) {
strs = new ArrayList<String>(8); strs = StringUtils.split(str, '|');
StringUtils.splitToList(str, '|', strs);
splitCache.put(str, strs); splitCache.put(str, strs);
} }
return strs; return strs;

Loading…
Cancel
Save