Download - Get Off My Thread! - keep your UI super-responsive

Transcript
Page 1: Get Off My Thread! - keep your UI super-responsive

Get off my thread! Adventures in perceived performance

Ori Peleg Software Engineer DroidCon TLV, June, 2014

Page 2: Get Off My Thread! - keep your UI super-responsive

1 Our app is stuck

2 Background service

3 Jank hunters never sleep

4 Nonblocking frenzy

5 Conclusion

Agenda

Page 3: Get Off My Thread! - keep your UI super-responsive

Our app is stuck

Page 4: Get Off My Thread! - keep your UI super-responsive

Slow activity

▪  App was stuck

▪  ~5 seconds on older devices

Page 5: Get Off My Thread! - keep your UI super-responsive

Slow activity

▪  App was stuck

▪  ~5 seconds on older devices

▪  ANR :-(

▪  Computed items to display

Page 6: Get Off My Thread! - keep your UI super-responsive

Make it faster?

▪  The app was still stuck!

▪  Turns out we were Main Thread bandits

▪  Any work done on the main thread

▪  Prevents UI updates

▪  Prevents interaction (touch, back button)

Page 7: Get Off My Thread! - keep your UI super-responsive

Make it seem faster!

▪  The app was still stuck!

▪  Turns out we were Main Thread bandits

▪  Any work done on the main thread

▪  Prevents UI updates

▪  Prevents interaction (touch, back button)

▪  AsyncTask to the rescue!

Page 8: Get Off My Thread! - keep your UI super-responsive

Background service still slow

Page 9: Get Off My Thread! - keep your UI super-responsive

Complex logic in the background

▪  “Make it a service”

▪  The app is super-slow

▪  “Separate process”

▪  The app isn’t stuck, but…

▪  Multi-process adds a lot of complexity

Page 10: Get Off My Thread! - keep your UI super-responsive

Back to a single process

▪  App takes 10 seconds to load

▪  ANR :-(

▪  Can the service affect the UI’s performance?

▪  (yes)

▪  Many dependent initialization steps

▪  AsyncTask?

Page 11: Get Off My Thread! - keep your UI super-responsive

Threads

▪  Many things can happen in parallel

▪  Main thread stays free

▪  But we create a lot of threads

Page 12: Get Off My Thread! - keep your UI super-responsive

Thread pools (Executors)

▪  Reuse threads

▪  Grows as needed

Page 13: Get Off My Thread! - keep your UI super-responsive

Jank hunters never sleep

Page 14: Get Off My Thread! - keep your UI super-responsive

Jank hunters

▪  We must fight to keep the main thread free!

▪  Every Activity Service callback is called in the main thread!

▪  Computations – too slow

▪  Network access – too slow

▪  Disk I/O?

▪  Even that is too slow

▪  StrictMode FTW

Page 15: Get Off My Thread! - keep your UI super-responsive

SharedPreferences

▪  In-memory cache

▪  Fast enough for the main thread

▪  But what about editing?

▪  .commit() flushes to disk (can block)

▪  No guarantees (no fsync)

▪  .apply() is good

Page 16: Get Off My Thread! - keep your UI super-responsive

Common patterns

▪  Loading images

▪  Picasso, by Square

▪  UI isn’t ready

▪  Progress indicator

▪  Some of the UI isn’t ready

▪  Show later (progress / fade in)

▪  Background processing

▪  IntentService (instead of Service)

Page 17: Get Off My Thread! - keep your UI super-responsive

Common errors

▪  Activity/Fragment have died while processing

▪  Cancel work in onStop/onDestroy

▪  Check isDestroyed/isAdded

▪  Thread-sensitive code

▪  ThreadLocal

Page 18: Get Off My Thread! - keep your UI super-responsive

Nonblocking frenzy manageable?

Page 19: Get Off My Thread! - keep your UI super-responsive

High-Velocity News – the FAST news app

▪  Fetch headlines in topic

▪  Fetch stories before you tap

▪  Simplify Concurrency with Futures™

Page 20: Get Off My Thread! - keep your UI super-responsive

Futures (Guava’s extensions)

Page 21: Get Off My Thread! - keep your UI super-responsive

Get news feed (blocking)

Page 22: Get Off My Thread! - keep your UI super-responsive

Get news feed (Future)

Page 23: Get Off My Thread! - keep your UI super-responsive

Get news stories (parsed)

Page 24: Get Off My Thread! - keep your UI super-responsive

Get top news story

Page 25: Get Off My Thread! - keep your UI super-responsive

Display the news story

Page 26: Get Off My Thread! - keep your UI super-responsive

Alternatives to the callback chain

▪  Futures.transform

▪  RxJava

Page 27: Get Off My Thread! - keep your UI super-responsive

Conclusion get off my thread!

Page 28: Get Off My Thread! - keep your UI super-responsive

Simple rules to follow

▪  Be aware what’s running on which thread

▪  Remember what’s slow

▪  Offload asynchronicity to libraries

▪  Test on typical (vs high-end) devices

▪  Threads/Executors/Futures FTW

Page 29: Get Off My Thread! - keep your UI super-responsive

Ori Peleg [email protected]