mozilla-mobile / perf-frontend-issues

A repository to hold issues related to front-end mobile application performance.
4 stars 0 forks source link

Discuss current pattern of creating a new thread for serial execution guarantees using coroutines #180

Open mcomella opened 3 years ago

mcomella commented 3 years ago

I'm creating this issue to discuss during triage.

In a few places in the code, the comments indicate we use singleThreadExecutors to serialize execution of the code:

In addition to some areas of the code that look like they use singleThreadExecutors to serialize execution but don't explicitly comment as such.

This seems like an unnecessary theoretical performance hit to me – we have existing thread pools that can be re-used, avoiding thread allocation time, memory considerations, and context switching time. I'm thinking of a mechanism similar to Apple's Grand Central Dispatch's serial DispatchQueues (or even Android Handler queues) where execution of tasks on the queue is guaranteed to be sequential and non-overlapping by FIFO ordering:

DispatchQueue.main.async {
    // Do something
}

In practice

In practice, we don't know what the impact of having so many threads is (currently, we have over 100 registered on start up). Ideally, we'd make a prototype and measure the difference in implementations. However, threading can have an inconsistent impact on app behavior so this may be hard to do.

Furthermore, there's an open question: what pattern should new code use? If we continue creating new threads for every sequential execution need, we may start to see performance impacts even if we're not seeing them now.

Questions

Sequential FIFO APIs

The coroutines APIs that I'm aware of that might be relevant for sequential, non-overlapping FIFO ordering are:

mcomella commented 3 years ago

We could try spawning the same number of threads in a sample app and measure it compared to the default to see how it affects startup time, maybe?

mcomella commented 3 years ago

We might be able to do some rough measuring with the vmstat command, which has a measure of the "number of context switches per second" and "the time spent running kernel code". There are other measures that might be useful to us for other purposes, such as "time spent waiting on IO".

mcomella commented 3 years ago

Triage: This is a subset of https://github.com/mozilla-mobile/android-components/issues/9424 so we'll leave this issue in triage to remind us to check in with that issue to make sure it gets worked on.

mcomella commented 3 years ago

Triage: this isn't urgent for the FE perf team b/c we're focused on start up: low priority. However, csadilek has memory consumption analysis https://github.com/mozilla-mobile/fenix/issues/17590 on their radar, which relates to this, so it may get addressed by them.