Skip to content

Commit

Permalink
Add systraces on Android (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw authored Dec 6, 2024
1 parent 9ad0acd commit 0391655
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,22 +21,37 @@ public MarkdownFormatter(@NonNull AssetManager assetManager) {
}

public void format(SpannableStringBuilder ssb, List<MarkdownRange> 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<MarkdownRange> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,46 +32,58 @@ public MarkdownParser(@NonNull ReactContext reactContext) {
private native String nativeParse(String text, int parserId);

public synchronized List<MarkdownRange> 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<MarkdownRange> 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<MarkdownRange> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.annotation.NonNull;

import com.facebook.react.bridge.ReactContext;
import com.facebook.systrace.Systrace;

import java.util.List;

Expand All @@ -29,8 +30,13 @@ public void setParserId(int parserId) {
}

public void applyMarkdownFormatting(SpannableStringBuilder ssb) {
String text = ssb.toString();
List<MarkdownRange> markdownRanges = mMarkdownParser.parse(text, mParserId);
mMarkdownFormatter.format(ssb, markdownRanges, mMarkdownStyle);
try {
Systrace.beginSection(0, "applyMarkdownFormatting");
String text = ssb.toString();
List<MarkdownRange> markdownRanges = mMarkdownParser.parse(text, mParserId);
mMarkdownFormatter.format(ssb, markdownRanges, mMarkdownStyle);
} finally {
Systrace.endSection(0);
}
}
}

0 comments on commit 0391655

Please sign in to comment.