diff --git a/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java b/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java index 1fb1469e..0c65739a 100644 --- a/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java +++ b/android/src/main/java/com/expensify/livemarkdown/MarkdownFormatter.java @@ -15,6 +15,9 @@ public class MarkdownFormatter { private final @NonNull AssetManager mAssetManager; + private String mPrevText; + private List mPrevMarkdownRanges; + private MarkdownStyle mPrevMarkdownStyle; public MarkdownFormatter(@NonNull AssetManager assetManager) { mAssetManager = assetManager; @@ -24,8 +27,19 @@ public void format(SpannableStringBuilder ssb, List 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); }