diff --git a/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java b/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java index c7f683bf..1fb1469e 100644 --- a/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java +++ b/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java @@ -8,6 +8,7 @@ import com.expensify.livemarkdown.spans.*; import com.facebook.react.views.text.internal.span.CustomLineHeightSpan; +import com.facebook.systrace.Systrace; import java.util.List; import java.util.Objects; @@ -20,22 +21,37 @@ public MarkdownFormatter(@NonNull AssetManager assetManager) { } public void format(SpannableStringBuilder ssb, List markdownRanges, @NonNull MarkdownStyle markdownStyle) { - Objects.requireNonNull(markdownStyle, "mMarkdownStyle is null"); - removeSpans(ssb); - applyRanges(ssb, markdownRanges, markdownStyle); + try { + Systrace.beginSection(0, "format"); + Objects.requireNonNull(markdownStyle, "mMarkdownStyle is null"); + removeSpans(ssb); + applyRanges(ssb, markdownRanges, markdownStyle); + } finally { + Systrace.endSection(0); + } } private void removeSpans(SpannableStringBuilder ssb) { - // We shouldn't use `removeSpans()` because it also removes SpellcheckSpan, SuggestionSpan etc. - MarkdownSpan[] spans = ssb.getSpans(0, ssb.length(), MarkdownSpan.class); - for (MarkdownSpan span : spans) { - ssb.removeSpan(span); + try { + Systrace.beginSection(0, "removeSpans"); + // We shouldn't use `removeSpans()` because it also removes SpellcheckSpan, SuggestionSpan etc. + MarkdownSpan[] spans = ssb.getSpans(0, ssb.length(), MarkdownSpan.class); + for (MarkdownSpan span : spans) { + ssb.removeSpan(span); + } + } finally { + Systrace.endSection(0); } } private void applyRanges(SpannableStringBuilder ssb, List markdownRanges, @NonNull MarkdownStyle markdownStyle) { - for (MarkdownRange markdownRange : markdownRanges) { - applyRange(ssb, markdownRange, markdownStyle); + try { + Systrace.beginSection(0, "applyRanges"); + for (MarkdownRange markdownRange : markdownRanges) { + applyRange(ssb, markdownRange, markdownStyle); + } + } finally { + Systrace.endSection(0); } } diff --git a/android/src/main/java/com/expensify/livemarkdown/MarkdownParser.java b/android/src/main/java/com/expensify/livemarkdown/MarkdownParser.java index e79963bb..292c4e13 100644 --- a/android/src/main/java/com/expensify/livemarkdown/MarkdownParser.java +++ b/android/src/main/java/com/expensify/livemarkdown/MarkdownParser.java @@ -5,6 +5,7 @@ import com.facebook.react.bridge.ReactContext; import com.facebook.react.util.RNLog; import com.facebook.soloader.SoLoader; +import com.facebook.systrace.Systrace; import org.json.JSONArray; import org.json.JSONException; @@ -31,46 +32,58 @@ public MarkdownParser(@NonNull ReactContext reactContext) { private native String nativeParse(String text, int parserId); public synchronized List parse(String text, int parserId) { - if (text.equals(mPrevText) && parserId == mPrevParserId) { - return mPrevMarkdownRanges; - } - - String json; try { - json = nativeParse(text, parserId); - } catch (Exception e) { - // Skip formatting, runGuarded will show the error in LogBox - mPrevText = text; - mPrevParserId = parserId; - mPrevMarkdownRanges = Collections.emptyList(); - return mPrevMarkdownRanges; - } + Systrace.beginSection(0, "parse"); - List markdownRanges = new LinkedList<>(); - try { - JSONArray ranges = new JSONArray(json); - for (int i = 0; i < ranges.length(); i++) { - JSONObject range = ranges.getJSONObject(i); - String type = range.getString("type"); - int start = range.getInt("start"); - int length = range.getInt("length"); - int depth = range.optInt("depth", 1); - if (length == 0 || start + length > text.length()) { - continue; + if (text.equals(mPrevText) && parserId == mPrevParserId) { + return mPrevMarkdownRanges; + } + + String json; + try { + Systrace.beginSection(0, "nativeParse"); + json = nativeParse(text, parserId); + } catch (Exception e) { + // Skip formatting, runGuarded will show the error in LogBox + mPrevText = text; + mPrevParserId = parserId; + mPrevMarkdownRanges = Collections.emptyList(); + return mPrevMarkdownRanges; + } finally { + Systrace.endSection(0); + } + + List markdownRanges = new LinkedList<>(); + try { + Systrace.beginSection(0, "markdownRanges"); + JSONArray ranges = new JSONArray(json); + for (int i = 0; i < ranges.length(); i++) { + JSONObject range = ranges.getJSONObject(i); + String type = range.getString("type"); + int start = range.getInt("start"); + int length = range.getInt("length"); + int depth = range.optInt("depth", 1); + if (length == 0 || start + length > text.length()) { + continue; + } + markdownRanges.add(new MarkdownRange(type, start, length, depth)); } - markdownRanges.add(new MarkdownRange(type, start, length, depth)); + } catch (JSONException e) { + RNLog.w(mReactContext, "[react-native-live-markdown] Incorrect schema of worklet parser output: " + e.getMessage()); + mPrevText = text; + mPrevParserId = parserId; + mPrevMarkdownRanges = Collections.emptyList(); + return mPrevMarkdownRanges; + } finally { + Systrace.endSection(0); } - } catch (JSONException e) { - RNLog.w(mReactContext, "[react-native-live-markdown] Incorrect schema of worklet parser output: " + e.getMessage()); + mPrevText = text; mPrevParserId = parserId; - mPrevMarkdownRanges = Collections.emptyList(); + mPrevMarkdownRanges = markdownRanges; return mPrevMarkdownRanges; + } finally { + Systrace.endSection(0); } - - mPrevText = text; - mPrevParserId = parserId; - mPrevMarkdownRanges = markdownRanges; - return mPrevMarkdownRanges; } } diff --git a/android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java b/android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java index be72ec4a..6877e46f 100644 --- a/android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java +++ b/android/src/main/java/com/expensify/livemarkdown/MarkdownUtils.java @@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import com.facebook.react.bridge.ReactContext; +import com.facebook.systrace.Systrace; import java.util.List; @@ -29,8 +30,13 @@ public void setParserId(int parserId) { } public void applyMarkdownFormatting(SpannableStringBuilder ssb) { - String text = ssb.toString(); - List markdownRanges = mMarkdownParser.parse(text, mParserId); - mMarkdownFormatter.format(ssb, markdownRanges, mMarkdownStyle); + try { + Systrace.beginSection(0, "applyMarkdownFormatting"); + String text = ssb.toString(); + List markdownRanges = mMarkdownParser.parse(text, mParserId); + mMarkdownFormatter.format(ssb, markdownRanges, mMarkdownStyle); + } finally { + Systrace.endSection(0); + } } }