Skip to content

Look into ways to improve the performance of fromHtml #800

Open
@cassgenerator

Description

@cassgenerator

While looking into the root causes of this issue logged against Guttenberg Mobile. I tracked down that one of the causes of the lag that is the call to AztectTexts fromHtml call.

I made some small modifications to the app to load a large post from this comment into a string then try to load it. Then I profiled it loading that into the TextView.

From the Flame Chart we can see that the fromHtml call itself is accounting for a large portion of the time to load this string into the view, and there is also a large portion for the setText call on the TextView. Both expected results
Screen Shot 2019-03-28 at 5 19 41 pm

However if we switch over to the Call Chart there is something very interesting happening in the second half of the chart (where the view is still unresponsive despite showing the new text). We can see that `refreshText is being called at least 8 times. Which to me looks like an opportunity for performance gains.
Screen Shot 2019-03-28 at 5 19 30 pm

I think there are a number of items to look into, please note I don't expect these to all bear fruit but they are worth noting.

  • Can we offload the parsing and conversion of the string to a background thread and show progress in the UI so that we aren't blocking the main thread?
  • Why is refreshText being called at least 8 times, each calling setText?
  • setSpan, removeSpan, insert, and replace are taking up a lot of time when we are parsing and styling the HTML. Is there any small gains we can get here which might multiply given the number of calls made?

Expected

When fromHtml is called on AztecText the main thread should be blocked for smallest amount of time possible.

Observed

We are parsing and applying styling to the string in the main thread, then calling setText on the underlying TextView multiple times before the user is able to interact with the application again.

Reproduced

  1. Download the post html from the comment mentioned above, save this as a text file into the raw resource directory
  2. Add a button or other way to trigger the trigger our test code. In the test code do the following
  3. Load the file into a string (I've included a snippet of the code I used below)
  4. Call aztec.visualEditor.fromHtml(largeString)

Tested

Android Emulator on Android 9 with develop at the point of writing this issue 14b9f94

Stream conversion code

@Throws(Exception::class)
fun convertStreamToString(inputStream: InputStream): String {
    val reader = BufferedReader(InputStreamReader(inputStream))
    val builder = StringBuilder()

    var line: String? = null
    line = reader.readLine()
    while (line != null) {
        builder.append(line).append('\n')
        line = reader.readLine()
    }
    reader.close()
    return builder.toString()
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions