From 3f80a31fe92dec8fe00eb13c5b1e1e1cde2d27f8 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 6 Mar 2025 23:56:29 +0800 Subject: [PATCH] inline all the method, optmize the performance. --- .../matrix/trace/core/AppMethodBeat.java | 127 ++++++++++++------ 1 file changed, 86 insertions(+), 41 deletions(-) diff --git a/matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/core/AppMethodBeat.java b/matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/core/AppMethodBeat.java index 70999c90d..229b84f27 100644 --- a/matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/core/AppMethodBeat.java +++ b/matrix/matrix-android/matrix-trace-canary/src/main/java/com/tencent/matrix/trace/core/AppMethodBeat.java @@ -250,10 +250,52 @@ public static void i(int methodId) { } assertIn = true; if (sIndex < Constants.BUFFER_SIZE) { - mergeData(methodId, sIndex, true); + if (methodId == AppMethodBeat.METHOD_ID_DISPATCH) { + sCurrentDiffTime = SystemClock.uptimeMillis() - sDiffTime; + } + try { + long trueId = 0L; + trueId |= 1L << 63; + trueId |= (long) methodId << 43; + trueId |= sCurrentDiffTime & 0x7FFFFFFFFFFL; + sBuffer[sIndex] = trueId; + IndexRecord indexRecord = sIndexRecordHead; + while (indexRecord != null) { + if (indexRecord.index == sIndex || (indexRecord.index == -1 && sLastIndex == Constants.BUFFER_SIZE - 1)) { + indexRecord.isValid = false; + sIndexRecordHead = indexRecord = indexRecord.next; + } else { + break; + } + } + sLastIndex = sIndex; + } catch (Throwable t) { + MatrixLog.e(TAG, t.getMessage()); + } } else { sIndex = 0; - mergeData(methodId, sIndex, true); + if (methodId == AppMethodBeat.METHOD_ID_DISPATCH) { + sCurrentDiffTime = SystemClock.uptimeMillis() - sDiffTime; + } + try { + long trueId = 0L; + trueId |= 1L << 63; + trueId |= (long) methodId << 43; + trueId |= sCurrentDiffTime & 0x7FFFFFFFFFFL; + sBuffer[sIndex] = trueId; + IndexRecord indexRecord = sIndexRecordHead; + while (indexRecord != null) { + if (indexRecord.index == sIndex || (indexRecord.index == -1 && sLastIndex == Constants.BUFFER_SIZE - 1)) { + indexRecord.isValid = false; + sIndexRecordHead = indexRecord = indexRecord.next; + } else { + break; + } + } + sLastIndex = sIndex; + } catch (Throwable t) { + MatrixLog.e(TAG, t.getMessage()); + } } ++sIndex; assertIn = false; @@ -274,10 +316,50 @@ public static void o(int methodId) { } if (Thread.currentThread().getId() == sMainThreadId) { if (sIndex < Constants.BUFFER_SIZE) { - mergeData(methodId, sIndex, false); + if (methodId == AppMethodBeat.METHOD_ID_DISPATCH) { + sCurrentDiffTime = SystemClock.uptimeMillis() - sDiffTime; + } + try { + long trueId = 0L; + trueId |= (long) methodId << 43; + trueId |= sCurrentDiffTime & 0x7FFFFFFFFFFL; + sBuffer[sIndex] = trueId; + IndexRecord indexRecord = sIndexRecordHead; + while (indexRecord != null) { + if (indexRecord.index == sIndex || (indexRecord.index == -1 && sLastIndex == Constants.BUFFER_SIZE - 1)) { + indexRecord.isValid = false; + sIndexRecordHead = indexRecord = indexRecord.next; + } else { + break; + } + } + sLastIndex = sIndex; + } catch (Throwable t) { + MatrixLog.e(TAG, t.getMessage()); + } } else { sIndex = 0; - mergeData(methodId, sIndex, false); + if (methodId == AppMethodBeat.METHOD_ID_DISPATCH) { + sCurrentDiffTime = SystemClock.uptimeMillis() - sDiffTime; + } + try { + long trueId = 0L; + trueId |= (long) methodId << 43; + trueId |= sCurrentDiffTime & 0x7FFFFFFFFFFL; + sBuffer[sIndex] = trueId; + IndexRecord indexRecord = sIndexRecordHead; + while (indexRecord != null) { + if (indexRecord.index == sIndex || (indexRecord.index == -1 && sLastIndex == Constants.BUFFER_SIZE - 1)) { + indexRecord.isValid = false; + sIndexRecordHead = indexRecord = indexRecord.next; + } else { + break; + } + } + sLastIndex = sIndex; + } catch (Throwable t) { + MatrixLog.e(TAG, t.getMessage()); + } } ++sIndex; } @@ -313,32 +395,7 @@ public static String getVisibleScene() { return ProcessUILifecycleOwner.INSTANCE.getVisibleScene(); } - /** - * merge trace info as a long data - * - * @param methodId - * @param index - * @param isIn - */ - private static void mergeData(int methodId, int index, boolean isIn) { - if (methodId == AppMethodBeat.METHOD_ID_DISPATCH) { - sCurrentDiffTime = SystemClock.uptimeMillis() - sDiffTime; - } - try { - long trueId = 0L; - if (isIn) { - trueId |= 1L << 63; - } - trueId |= (long) methodId << 43; - trueId |= sCurrentDiffTime & 0x7FFFFFFFFFFL; - sBuffer[index] = trueId; - checkPileup(index); - sLastIndex = index; - } catch (Throwable t) { - MatrixLog.e(TAG, t.getMessage()); - } - } public void addListener(IAppMethodBeatListener listener) { synchronized (listeners) { @@ -387,18 +444,6 @@ record = record.next; } } - private static void checkPileup(int index) { - IndexRecord indexRecord = sIndexRecordHead; - while (indexRecord != null) { - if (indexRecord.index == index || (indexRecord.index == -1 && sLastIndex == Constants.BUFFER_SIZE - 1)) { - indexRecord.isValid = false; - MatrixLog.w(TAG, "[checkPileup] %s", indexRecord.toString()); - sIndexRecordHead = indexRecord = indexRecord.next; - } else { - break; - } - } - } public static final class IndexRecord { public IndexRecord(int index) {