Dbrant has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/387611 )

Change subject: Slight optimization when parsing HTML from Strings.
......................................................................

Slight optimization when parsing HTML from Strings.

There are very many places in our code where we call Html.fromHtml(),
because some of the strings that we receive from the server might contain
some minimal HTML formatting, such as bold, italics, and links.

However, if you dig into the code of the fromHtml() implementation, it's
actually pretty expensive and resource-intensive, and might actually
contribute to performance degradations when used too much.

The other thing is that *most* of the time, the strings we get from the
server don't actually contain any HTML formatting, so we're actually
passing the string into fromHtml() needlessly.

This patch makes it conditional whether or not a string will be parsed by
fromHtml(), based on whether the string contains at least one opening
bracket of an HTML tag ("<"). Otherwise, the string is returned as-is.

Change-Id: Ib57d6ede66e7ca3fec6d8b0b28b01db437007276
---
M app/src/main/java/org/wikipedia/util/StringUtil.java
1 file changed, 10 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/11/387611/1

diff --git a/app/src/main/java/org/wikipedia/util/StringUtil.java 
b/app/src/main/java/org/wikipedia/util/StringUtil.java
index 54e6b95..c5bb8ab 100644
--- a/app/src/main/java/org/wikipedia/util/StringUtil.java
+++ b/app/src/main/java/org/wikipedia/util/StringUtil.java
@@ -5,6 +5,7 @@
 import android.support.annotation.Nullable;
 import android.text.Html;
 import android.text.Spanned;
+import android.text.SpannedString;
 import android.text.TextUtils;
 
 import java.io.UnsupportedEncodingException;
@@ -112,11 +113,16 @@
                 .trim();
     }
 
-    /** Fix Html.fromHtml is deprecated problem
-     * @param source provided Html string
-     * @return returned Spanned of appropriate method according to version 
check
-     * */
+    /**
+     * @param source String that may contain HTML tags.
+     * @return returned Spanned string that may contain spans parsed from the 
HTML source.
+     */
     @NonNull public static Spanned fromHtml(@NonNull String source) {
+        if (!source.contains("<")) {
+            // If the string doesn't contain any hints of HTML tags, then skip 
the expensive
+            // processing that fromHtml() performs.
+            return new SpannedString(source);
+        }
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
             return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY);
         } else {

-- 
To view, visit https://gerrit.wikimedia.org/r/387611
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib57d6ede66e7ca3fec6d8b0b28b01db437007276
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Dbrant <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to