From a3c47d3baad339e7006ba040583d55e3efa8e0fd Mon Sep 17 00:00:00 2001 From: Xin Chen Date: Mon, 12 Dec 2022 13:50:44 -0800 Subject: [PATCH] Replace RN Dev Tool perf logger usage of API 24 method in java.util.Comparator.naturalOrder Summary: Previous diff D41486648 is causing crashes and a sev S311353, which is due to usages of an old Android API that only work after level 24 (D36500518 (https://github.com/facebook/react-native/commit/0fc42fd35c577c71825f020b0fa09bee6d9ad105)). ~~This diff updates the implementation to use a compatible API, but with worse runtime complexity.~~ ~~https://fburl.com/txd0r89e is a good explanation on the two algorithm to calculate a streaming median value. This diff uses the approach described in https://stackoverflow.com/a/4903642.~~ ## Update Following suggestion from sshic, I preserved the existing algorithm but with a custom comparator approach. Changelog: [Internal] Reviewed By: makovkastar Differential Revision: D41505143 fbshipit-source-id: 494e07fa627b5cf8bad7971fa5de86d270a7412c --- .../react/fabric/DevToolsReactPerfLogger.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/DevToolsReactPerfLogger.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/DevToolsReactPerfLogger.java index e6a3bf2fe0f..d62ae267cbe 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/DevToolsReactPerfLogger.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/DevToolsReactPerfLogger.java @@ -21,8 +21,27 @@ import java.util.PriorityQueue; import java.util.Queue; class LongStreamingStats { - private Queue minHeap = new PriorityQueue<>(Comparator.naturalOrder()); - private Queue maxHeap = new PriorityQueue<>(Comparator.reverseOrder()); + // TODO(T138627466): Calculate median value with better algorithm after Android API 24. + private Queue minHeap = + new PriorityQueue<>( + 11, + new Comparator() { + @Override + public int compare(Long first, Long second) { + // Natural order + return Long.compare(first, second); + } + }); + private Queue maxHeap = + new PriorityQueue<>( + 11, + new Comparator() { + @Override + public int compare(Long first, Long second) { + // Reversed order + return Long.compare(second, first); + } + }); private double streamingAverage = 0.0; private int len = 0; private long max = 0;