Skip to content

Commit

Permalink
Merge Android UI Performance into Performance doc, reorder sidebar
Browse files Browse the repository at this point in the history
Summary:
Doing some cleanup in preparation for CRNA.
Recommend `FlatList` and React Navigation for perf.
Tag docs that may only apply to apps ejected from CRNA. Currently has no effect.
Closes facebook#12692

Differential Revision: D4654077

Pulled By: hramos

fbshipit-source-id: 1245d80d66e37d9dca9e9daf23e8b93c65cd1bf7
  • Loading branch information
hramos authored and facebook-github-bot committed Mar 6, 2017
1 parent c77f09b commit c503dae
Show file tree
Hide file tree
Showing 33 changed files with 399 additions and 483 deletions.
2 changes: 1 addition & 1 deletion docs/Accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout: docs
category: Guides
permalink: docs/accessibility.html
next: timers
previous: animations
previous: debugging
---

## Native App Accessibility (iOS and Android)
Expand Down
1 change: 1 addition & 0 deletions docs/AndroidBuildingFromSource.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: Building React Native from source
layout: docs
category: Guides (Android)
permalink: docs/android-building-from-source.html
banner: ejected
next: activityindicator
previous: android-ui-performance
---
Expand Down
162 changes: 2 additions & 160 deletions docs/AndroidUIPerformance.md
Original file line number Diff line number Diff line change
@@ -1,165 +1,7 @@
---
id: android-ui-performance
title: Profiling Android UI Performance
layout: docs
category: Guides (Android)
layout: redirect
permalink: docs/android-ui-performance.html
next: android-building-from-source
previous: signed-apk-android
destinationUrl: performance.html
---

We try our best to deliver buttery-smooth UI performance by default, but sometimes that just isn't possible. Remember, Android supports 10k+ different phones and is generalized to support software rendering: the framework architecture and need to generalize across many hardware targets unfortunately means you get less for free relative to iOS. But sometimes, there are things you can improve (and many times it's not native code's fault at all!).

The first step for debugging this jank is to answer the fundamental question of where your time is being spent during each 16ms frame. For that, we'll be using a standard Android profiling tool called systrace. But first...

> Make sure that JS dev mode is OFF!
>
> You should see `__DEV__ === false, development-level warning are OFF, performance optimizations are ON` in your application logs (which you can view using `adb logcat`)
## Profiling with Systrace

Systrace is a standard Android marker-based profiling tool (and is installed when you install the Android platform-tools package). Profiled code blocks are surrounded by markers start/end markers which are then visualized in a colorful chart format. Both the Android SDK and React Native framework provide standard markers that you can visualize.

### Collecting a trace

> NOTE:
>
> Systrace support was added in react-native `v0.15`. You will need to build with that version to collect a trace.
First, connect a device that exhibits the stuttering you want to investigate to your computer via USB and get it to the point right before the navigation/animation you want to profile. Run systrace as follows

```
$ <path_to_android_sdk>/platform-tools/systrace/systrace.py --time=10 -o trace.html sched gfx view -a <your_package_name>
```

A quick breakdown of this command:

- `time` is the length of time the trace will be collected in seconds
- `sched`, `gfx`, and `view` are the android SDK tags (collections of markers) we care about: `sched` gives you information about what's running on each core of your phone, `gfx` gives you graphics info such as frame boundaries, and `view` gives you information about measure, layout, and draw passes
- `-a <your_package_name>` enables app-specific markers, specifically the ones built into the React Native framework. `your_package_name` can be found in the `AndroidManifest.xml` of your app and looks like `com.example.app`

Once the trace starts collecting, perform the animation or interaction you care about. At the end of the trace, systrace will give you a link to the trace which you can open in your browser.

## Reading the trace

After opening the trace in your browser (preferably Chrome), you should see something like this:

![Example](img/SystraceExample.png)

If your trace .html file isn't opening correctly, check your browser console for the following:

![ObjectObserveError](img/ObjectObserveError.png)

Since Object.observe was deprecated in recent browsers, you may have to open the file from the Google Chrome Tracing tool. You can do so by:

- Opening tab in chrome chrome://tracing
- Selecting load
- Selecting the html file generated from the previous command.

**HINT**: Use the WASD keys to strafe and zoom

### Enable VSync highlighting

The first thing you should do is highlight the 16ms frame boundaries if you haven't already done that. Check this checkbox at the top right of the screen:

![Enable VSync Highlighting](img/SystraceHighlightVSync.png)

You should see zebra stripes as in the screenshot above. If you don't, try profiling on a different device: Samsung has been known to have issues displaying vsyncs while the Nexus series is generally pretty reliable.

### Find your process

Scroll until you see (part of) the name of your package. In this case, I was profiling `com.facebook.adsmanager`, which shows up as `book.adsmanager` because of silly thread name limits in the kernel.

On the left side, you'll see a set of threads which correspond to the timeline rows on the right. There are three/four threads we care about for our purposes: the UI thread (which has your package name or the name UI Thread), `mqt_js` and `mqt_native_modules`. If you're running on Android 5+, we also care about the Render Thread.

### UI Thread

This is where standard android measure/layout/draw happens. The thread name on the right will be your package name (in my case book.adsmanager) or UI Thread. The events that you see on this thread should look something like this and have to do with `Choreographer`, `traversals`, and `DispatchUI`:

![UI Thread Example](img/SystraceUIThreadExample.png)

### JS Thread

This is where JS is executed. The thread name will be either `mqt_js` or `<...>` depending on how cooperative the kernel on your device is being. To identify it if it doesn't have a name, look for things like `JSCall`, `Bridge.executeJSCall`, etc:

![JS Thread Example](img/SystraceJSThreadExample.png)

### Native Modules Thread

This is where native module calls (e.g. the `UIManager`) are executed. The thread name will be either `mqt_native_modules` or `<...>`. To identify it in the latter case, look for things like `NativeCall`, `callJavaModuleMethod`, and `onBatchComplete`:

![Native Modules Thread Example](img/SystraceNativeModulesThreadExample.png)

### Bonus: Render Thread

If you're using Android L (5.0) and up, you will also have a render thread in your application. This thread generates the actual OpenGL commands used to draw your UI. The thread name will be either `RenderThread` or `<...>`. To identify it in the latter case, look for things like `DrawFrame` and `queueBuffer`:

![Render Thread Example](img/SystraceRenderThreadExample.png)

## Identifying a culprit

A smooth animation should look something like the following:

![Smooth Animation](img/SystraceWellBehaved.png)

Each change in color is a frame -- remember that in order to display a frame, all our UI work needs to be done by the end of that 16ms period. Notice that no thread is working close to the frame boundary. An application rendering like this is rendering at 60FPS.

If you noticed chop, however, you might see something like this:

![Choppy Animation from JS](img/SystraceBadJS.png)

Notice that the JS thread is executing basically all the time, and across frame boundaries! This app is not rendering at 60FPS. In this case, **the problem lies in JS**.

You might also see something like this:

![Choppy Animation from UI](img/SystraceBadUI.png)

In this case, the UI and render threads are the ones that have work crossing frame boundaries. The UI that we're trying to render on each frame is requiring too much work to be done. In this case, **the problem lies in the native views being rendered**.

At this point, you'll have some very helpful information to inform your next steps.

## JS Issues

If you identified a JS problem, look for clues in the specific JS that you're executing. In the scenario above, we see `RCTEventEmitter` being called multiple times per frame. Here's a zoom-in of the JS thread from the trace above:

![Too much JS](img/SystraceBadJS2.png)

This doesn't seem right. Why is it being called so often? Are they actually different events? The answers to these questions will probably depend on your product code. And many times, you'll want to look into [shouldComponentUpdate](https://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate).

> **TODO**: Add more tools for profiling JS
## Native UI Issues

If you identified a native UI problem, there are usually two scenarios:

1. the UI you're trying to draw each frame involves to much work on the GPU, or
2. You're constructing new UI during the animation/interaction (e.g. loading in new content during a scroll).

### Too much GPU work

In the first scenario, you'll see a trace that has the UI thread and/or Render Thread looking like this:

![Overloaded GPU](img/SystraceBadUI.png)

Notice the long amount of time spent in `DrawFrame` that crosses frame boundaries. This is time spent waiting for the GPU to drain its command buffer from the previous frame.

To mitigate this, you should:

- investigate using `renderToHardwareTextureAndroid` for complex, static content that is being animated/transformed (e.g. the `Navigator` slide/alpha animations)
- make sure that you are **not** using `needsOffscreenAlphaCompositing`, which is disabled by default, as it greatly increases the per-frame load on the GPU in most cases.

If these don't help and you want to dig deeper into what the GPU is actually doing, you can check out [Tracer for OpenGL ES](http://developer.android.com/tools/help/gltracer.html).

### Creating new views on the UI thread

In the second scenario, you'll see something more like this:

![Creating Views](img/SystraceBadCreateUI.png)

Notice that first the JS thread thinks for a bit, then you see some work done on the native modules thread, followed by an expensive traversal on the UI thread.

There isn't an easy way to mitigate this unless you're able to postpone creating new UI until after the interaction, or you are able to simplify the UI you're creating. The react native team is working on a infrastructure level solution for this that will allow new UI to be created and configured off the main thread, allowing the interaction to continue smoothly.

## Still stuck?

If you are confused or stuck, please post ask on [Stack Overflow with the react-native tag](http://stackoverflow.com/tags/react-native). If you are unable to get a response there, or find an issue with a core component, please [File a Github issue](https://github.com/facebook/react-native/issues).
2 changes: 1 addition & 1 deletion docs/Animations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Animations
layout: docs
category: Guides
permalink: docs/animations.html
next: accessibility
next: navigation
previous: handling-touches
---

Expand Down
4 changes: 2 additions & 2 deletions docs/Colors.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ title: Colors
layout: docs
category: Guides
permalink: docs/colors.html
next: images
previous: integration-with-existing-apps
next: platform-specific-code
previous: images
---

The following formats are supported:
Expand Down
3 changes: 2 additions & 1 deletion docs/CommunicationIOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ title: Communication between native and React Native
layout: docs
category: Guides (iOS)
permalink: docs/communication-ios.html
banner: ejected
next: native-modules-android
previous: running-on-simulator-ios
previous: linking-libraries-ios
---

In [Integrating with Existing Apps guide](docs/integration-with-existing-apps.html) and [Native UI Components guide](docs/native-components-ios.html) we learn how to embed React Native in a native component and vice versa. When we mix native and React Native components, we'll eventually find a need to communicate between these two worlds. Some ways to achieve that have been already mentioned in other guides. This article summarizes available techniques.
Expand Down
4 changes: 2 additions & 2 deletions docs/Debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ title: Debugging
layout: docs
category: Guides
permalink: docs/debugging.html
next: testing
previous: direct-manipulation
next: accessibility
previous: platform-specific-code
---

## Accessing the In-App Developer Menu
Expand Down
4 changes: 2 additions & 2 deletions docs/DirectManipulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ title: Direct Manipulation
layout: docs
category: Guides
permalink: docs/direct-manipulation.html
next: debugging
previous: timers
next: performance
previous: javascript-environment
---

It is sometimes necessary to make changes directly to a component
Expand Down
4 changes: 2 additions & 2 deletions docs/GestureResponderSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ title: Gesture Responder System
layout: docs
category: Guides
permalink: docs/gesture-responder-system.html
next: native-modules-ios
previous: platform-specific-code
next: testing
previous: performance
---

The gesture responder system manages the lifecycle of gestures in your app. A touch can go through several phases as the app determines what the user's intention is. For example, the app needs to determine if the touch is scrolling, sliding on a widget, or tapping. This can even change during the duration of a touch. There can also be multiple simultaneous touches.
Expand Down
2 changes: 1 addition & 1 deletion docs/HandlingTouches.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout: docs
category: Guides
permalink: docs/handling-touches.html
next: animations
previous: images
previous: more-resources
---

Users interact with mobile apps mainly through touch. They can use a combination of gestures, such as tapping on a button, scrolling a list, or zooming on a map.
Expand Down
1 change: 1 addition & 0 deletions docs/HeadlessJSAndroid.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: Headless JS
layout: docs
category: Guides (Android)
permalink: docs/headless-js-android.html
banner: ejected
next: signed-apk-android
previous: native-components-android
---
Expand Down
4 changes: 2 additions & 2 deletions docs/Images.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ title: Images
layout: docs
category: Guides
permalink: docs/images.html
next: handling-touches
previous: colors
next: colors
previous: navigation
---

## Static Image Resources
Expand Down
5 changes: 3 additions & 2 deletions docs/IntegrationWithExistingApps.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ title: Integration With Existing Apps
layout: docs
category: Guides
permalink: docs/integration-with-existing-apps.html
next: colors
previous: more-resources
banner: ejected
next: running-on-device
previous: testing
---

<div class="integration-toggler">
Expand Down
4 changes: 2 additions & 2 deletions docs/JavaScriptEnvironment.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ title: JavaScript Environment
layout: docs
category: Guides
permalink: docs/javascript-environment.html
next: navigation
previous: testing
next: direct-manipulation
previous: timers
---

## JavaScript Runtime
Expand Down
1 change: 1 addition & 0 deletions docs/LinkingLibraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: Linking Libraries
layout: docs
category: Guides (iOS)
permalink: docs/linking-libraries-ios.html
banner: ejected
next: running-on-simulator-ios
previous: native-components-ios
---
Expand Down
2 changes: 1 addition & 1 deletion docs/MoreResources.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: More Resources
layout: docs
category: The Basics
permalink: docs/more-resources.html
next: integration-with-existing-apps
next: handling-touches
previous: networking
---

Expand Down
1 change: 1 addition & 0 deletions docs/NativeComponentsAndroid.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: Native UI Components
layout: docs
category: Guides (Android)
permalink: docs/native-components-android.html
banner: ejected
next: headless-js-android
previous: native-modules-android
---
Expand Down
1 change: 1 addition & 0 deletions docs/NativeComponentsIOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: Native UI Components
layout: docs
category: Guides (iOS)
permalink: docs/native-components-ios.html
banner: ejected
next: linking-libraries-ios
previous: native-modules-ios
---
Expand Down
7 changes: 4 additions & 3 deletions docs/NativeModulesAndroid.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ title: Native Modules
layout: docs
category: Guides (Android)
permalink: docs/native-modules-android.html
banner: ejected
next: native-components-android
previous: communication-ios
---
Expand Down Expand Up @@ -131,7 +132,7 @@ public class AnExampleReactPackage implements ReactPackage {

return modules;
}

}
```

Expand Down Expand Up @@ -369,9 +370,9 @@ public class ImagePickerModule extends ReactContextBaseJavaModule {
private static final String E_NO_IMAGE_DATA_FOUND = "E_NO_IMAGE_DATA_FOUND";

private Promise mPickerPromise;

private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
if (requestCode == IMAGE_PICKER_REQUEST) {
Expand Down
7 changes: 4 additions & 3 deletions docs/NativeModulesIOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ title: Native Modules
layout: docs
category: Guides (iOS)
permalink: docs/native-modules-ios.html
banner: ejected
next: native-components-ios
previous: gesture-responder-system
previous: upgrading
---

Sometimes an app needs access to platform API, and React Native doesn't have a corresponding module yet. Maybe you want to reuse some existing Objective-C, Swift or C++ code without having to reimplement it in JavaScript, or write some high performance, multi-threaded code such as for image processing, a database, or any number of advanced extensions.
Expand Down Expand Up @@ -402,13 +403,13 @@ You will receive a warning if you expend resources unnecessarily by emitting an
}

// Will be called when this module's first listener is added.
-(void)startObserving {
-(void)startObserving {
hasListeners = YES;
// Set up any upstream listeners or background tasks as necessary
}

// Will be called when this module's last listener is removed, or on dealloc.
-(void)stopObserving {
-(void)stopObserving {
hasListeners = NO;
// Remove upstream listeners, stop unnecessary background tasks
}
Expand Down
4 changes: 2 additions & 2 deletions docs/Navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ title: Navigation
layout: docs
category: Guides
permalink: docs/navigation.html
next: performance
previous: javascript-environment
next: images
previous: animations
---

This guide covers the various navigation components available in React Native. If you are just getting started with navigation, you will probably want to use React Navigation.
Expand Down
Loading

0 comments on commit c503dae

Please sign in to comment.