Skip to content

Commit

Permalink
Skip formatting if data unchanged on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw committed Dec 7, 2024
1 parent 5695408 commit 2b11e98
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

public class MarkdownFormatter {
private final @NonNull AssetManager mAssetManager;
private String mPrevText;
private List<MarkdownRange> mPrevMarkdownRanges;
private MarkdownStyle mPrevMarkdownStyle;

public MarkdownFormatter(@NonNull AssetManager assetManager) {
mAssetManager = assetManager;
Expand All @@ -24,8 +27,19 @@ public void format(SpannableStringBuilder ssb, List<MarkdownRange> markdownRange
try {
Systrace.beginSection(0, "format");
Objects.requireNonNull(markdownStyle, "mMarkdownStyle is null");

String text = ssb.toString();
if (text.equals(mPrevText) && markdownRanges == mPrevMarkdownRanges && markdownStyle == mPrevMarkdownStyle) {
// Use shallow comparison of markdown ranges and markdown style
// to optimistically skip removing and applying the same spans
return;
}

removeSpans(ssb);
applyRanges(ssb, markdownRanges, markdownStyle);
mPrevText = text;
mPrevMarkdownRanges = markdownRanges;
mPrevMarkdownStyle = markdownStyle;
} finally {
Systrace.endSection(0);
}
Expand Down

0 comments on commit 2b11e98

Please sign in to comment.