From 85c26fb5a8eef0c60aba1b27d8d516034a7e508e Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 1 Nov 2023 15:31:33 +0000 Subject: [PATCH] Instrument module init in DEV (#1787) --- patches/metro-runtime+0.76.8.patch | 50 ++++++++++++++++++++++++++++++ src/Navigation.tsx | 13 ++++++++ 2 files changed, 63 insertions(+) create mode 100644 patches/metro-runtime+0.76.8.patch diff --git a/patches/metro-runtime+0.76.8.patch b/patches/metro-runtime+0.76.8.patch new file mode 100644 index 00000000..65303775 --- /dev/null +++ b/patches/metro-runtime+0.76.8.patch @@ -0,0 +1,50 @@ +diff --git a/node_modules/metro-runtime/src/polyfills/require.js b/node_modules/metro-runtime/src/polyfills/require.js +index ce67cb4..eeeae84 100644 +--- a/node_modules/metro-runtime/src/polyfills/require.js ++++ b/node_modules/metro-runtime/src/polyfills/require.js +@@ -22,6 +22,13 @@ global.__c = clear; + global.__registerSegment = registerSegment; + var modules = clear(); + ++if (__DEV__) { ++ // Added by Dan for module init logging. ++ global.__INIT_LOGS__ = [] ++ var initModuleCounter = 0 ++ var initModuleStack = [] ++} ++ + // Don't use a Symbol here, it would pull in an extra polyfill with all sorts of + // additional stuff (e.g. Array.from). + const EMPTY = {}; +@@ -303,7 +310,30 @@ function loadModuleImplementation(moduleId, module) { + throw module.error; + } + if (__DEV__) { +- var Systrace = requireSystrace(); ++ // Added by Dan for module init logging. ++ var Systrace = { ++ beginEvent(label) { ++ let fullLabel = initModuleCounter++ + ' ' + label ++ global.__INIT_LOGS__.push( ++ ' '.repeat(initModuleStack.length) + ++ ' ENTER ' + fullLabel ++ ) ++ initModuleStack.push({ ++ fullLabel, ++ startTime: nativePerformanceNow(), ++ }) ++ }, ++ endEvent() { ++ const res = initModuleStack.pop() ++ const fullLabel = res.fullLabel ++ const startTime = res.startTime ++ const timeElapsed = Math.round(nativePerformanceNow() - startTime) ++ global.__INIT_LOGS__.push( ++ ' '.repeat(initModuleStack.length) + ++ ' LEAVE ' + fullLabel + ' [' + timeElapsed + 'ms]', ++ ) ++ } ++ }; + var Refresh = requireRefresh(); + } + diff --git a/src/Navigation.tsx b/src/Navigation.tsx index a7565198..e481d966 100644 --- a/src/Navigation.tsx +++ b/src/Navigation.tsx @@ -472,6 +472,7 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) { performance.now() - global.__BUNDLE_START_TIME__, ) console.log(`Time to first paint: ${initMs} ms`) + logModuleInitTrace() // Register the navigation container with the Sentry instrumentation (only works on native) if (isNative) { @@ -586,6 +587,18 @@ const styles = StyleSheet.create({ }, }) +function logModuleInitTrace() { + if (__DEV__) { + // This log is noisy, so keep false committed + const shouldLog = false + // Relies on our patch to polyfill.js in metro-runtime + const initLogs = (global as any).__INIT_LOGS__ + if (shouldLog && Array.isArray(initLogs)) { + console.log(initLogs.join('\n')) + } + } +} + export { navigate, resetToTab,