From 2b11e980f138abcef8daf91d2fefc15e74ba8eeb Mon Sep 17 00:00:00 2001 From: Tomek Zawadzki Date: Sat, 7 Dec 2024 12:07:57 +0100 Subject: [PATCH] Skip formatting if data unchanged on Android --- .../expensify/livemarkdown/MarkdownFormatter.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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); }