Skip to content

Commit cd219b9

Browse files
kwalrathmhevery
authored andcommitted
docs: edit DART_TOOLS.md
Add more info about reflector, and copyedit other sections. Closes angular#4422
1 parent a31b217 commit cd219b9

1 file changed

Lines changed: 87 additions & 35 deletions

File tree

TOOLS_DART.md

Lines changed: 87 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ In the browser, open the dev console. The top-level object is called `ng` and
3838
contains more specific tools inside it.
3939

4040
For example, to run the change detection profiler on your app:
41-
<!-- QUESTION: is "on your app" accurate?
42-
is "run the change detection profiler on your app" the best wording? -->
4341

4442
```javascript
4543
// In the dev console:
@@ -68,7 +66,7 @@ Options for investigating code size include the `--dump-info` dart2js option,
6866
ng2soyc, `reflector.trackUsage()`, and code coverage information
6967
from the Dart VM.
7068

71-
#### --dump-info
69+
#### Use --dump-info
7270

7371
The `--dump-info` option of `dart2js` outputs information about what happened
7472
during compilation. You can specify `--dump-info` in `pubspec.yaml`:
@@ -86,7 +84,7 @@ can help you analyze the output.
8684
For more information, see the
8785
[dart2js_info API reference](http://dart-lang.github.io/dart2js_info/doc/api/).
8886
89-
#### ng2soyc.dart
87+
#### Use ng2soyc.dart
9088
9189
[ng2soyc](https://github.com/angular/ng2soyc.dart) is a utility for analyzing
9290
code size contributors in Angular 2 applications. It groups code size by
@@ -97,17 +95,61 @@ each level. To reduce noise in the output of very large apps, ng2soyc provides
9795
an option to hide libraries that are too small, so you can focus on the biggest
9896
contributors.
9997
100-
#### Track unused reflection data
98+
#### Find unused reflection data
10199
102-
<!-- QUESTION: How do you get access to reflector & ReflectionInfo? -->
100+
Your app might have types that are annotated with `@Component` or `@Injectable`
101+
but never used.
102+
To find these unused types, use `reflector.trackUsage()` and then,
103+
after exercising your app, `reflector.listUnusedKeys()`.
104+
For example:
103105

104-
Call `reflector.trackUsage()` to track reflection information used
105-
by the application. Reflection information (`ReflectionInfo`) is a data
106-
structure that stores information about your application that Angular uses for
107-
locating DI factories, generated change detectors and other code related to a
108-
given type. After exercising your application, call `reflector.listUnusedKeys()`
109-
to get a list of types and functions whose reflection information was retained
110-
but never used by the application.
106+
```
107+
import 'package:angular2/src/core/reflection/reflection.dart';
108+
...
109+
main() async {
110+
reflector.trackUsage();
111+
await bootstrap(AppComponent);
112+
print('Unused keys: ${reflector.listUnusedKeys()}');
113+
}
114+
```
115+
116+
When you run that code (in Dartium or another browser),
117+
you'll see a list of types that Angular _can_ inject but hasn't needed to.
118+
Consider removing those types or their `@Component`/`@Injectable` annotation
119+
to decrease your app's code size.
120+
121+
Three conditions must be true for `listUnusedKeys()` to return helpful data:
122+
123+
1. The angular2 transformer must run on the app.
124+
2. If you're running a JavaScript version of the app,
125+
the app must not be minified, so that the names are readable.
126+
3. You must exercise your app in as many ways as possible
127+
before calling `listUnusedKeys()`.
128+
Otherwise, you might get false positives:
129+
keys that haven't been used only because you didn't exercise
130+
the relevant feature of the app.
131+
132+
To run the angular2 transformer, first specify it in `pubspec.yaml`:
133+
134+
```
135+
name: hello_world
136+
...
137+
transformers:
138+
- angular2:
139+
entry_points: web/main.dart
140+
```
141+
142+
Then use pub to run the transformer. If you use `pub serve`,
143+
it provides both Dart and unminified (by default) JavaScript versions.
144+
If you want to serve actual files, then use `pub build` in debug mode
145+
to generate Dart and unminified JavaScript files:
146+
`pub build --mode=debug`.
147+
148+
The `reflector.trackUsage()` method makes Angular track the reflection
149+
information used by the app. Reflection information (`ReflectionInfo`) is a data
150+
structure that stores information that Angular uses for locating DI factories
151+
and for generating change detectors and other code related to a
152+
given type.
111153

112154
#### Use code coverage to find dead code
113155

@@ -216,34 +258,45 @@ transformers:
216258
### Change detection profiler
217259

218260
If your application is janky (it misses frames) or is slow according to other
219-
metrics it is important to find the root cause of the issue. Change detection
220-
is a phase in Angular's lifecycle that detects changes in values that are
221-
bound to UI, and if it finds a change it performs the corresponding UI update.
222-
However, sometimes it is hard to tell if the slowness is due to the act of
223-
computing the changes being slow, or due to the act of applying those changes
224-
to the UI. For your application to be performant it is important that the
225-
process of computing changes is very fast. For best results it should be under
226-
3 milliseconds in order to leave room for the application logic, the UI updates
227-
and browser's rendering pipeline to fit withing the 16 millisecond frame
228-
(assuming the 60 FPS target frame rate).
229-
230-
Change detection profiler repeatedly performs change detection without invoking
231-
any user actions, such as clicking buttons or entering text in input fields. It
232-
then computes the average amount of time it took to perform a single cycle of
233-
change detection in milliseconds and prints it to the console. This number
234-
depends on the current state of the UI. You are likely to see different numbers
261+
metrics, you need to find out why. This tool helps by measuring the average
262+
speed of _change detection_, a phase in Angular's
263+
lifecycle that detects changes in values that are bound to the UI.
264+
Janky UI updates can result from slowness either in _computing_ the changes or
265+
in _applying_ those changes to the UI.
266+
267+
For your app to be performant, the process of _computing_ changes must be very
268+
fast—preferably **under 3 milliseconds**.
269+
Fast change computation leaves room for
270+
the application logic, UI updates, and browser rendering pipeline
271+
to fit within a 16 ms frame (assuming a target frame rate of 60 FPS).
272+
273+
The change detection profiler repeatedly performs change detection
274+
without invoking any user actions, such as clicking buttons or entering
275+
text in input fields. It then computes the average amount of time
276+
(in milliseconds) to perform a single cycle of change detection and
277+
prints that to the console. This number depends on the current state of the UI. You are likely to see different numbers
235278
as you go from one screen in your application to another.
236279

237280
#### Running the profiler
238281

239-
Enable the debugging tools (see above),
240-
then in the dev console enter the following:
282+
Before running the profiler, enable the debugging tools
283+
and put the app into the state you want to measure:
284+
285+
1. If you haven't already done so,
286+
[enable the debugging tools](#enabling-the-debugging-tools).
287+
2. Navigate the app to a screen whose performance you want to profile.
288+
3. Make sure the screen is in a state that you want to measure.
289+
For example, you might want to profile the screen several times,
290+
with different amounts and kinds of data.
291+
292+
To run the profiler, enter the following in the dev console:
241293

242294
```javascript
243295
ng.profiler.timeChangeDetection();
244296
```
245297

246-
The results are printed to the console.
298+
The results are visible in the console.
299+
247300

248301
#### Recording CPU profiles
249302

@@ -257,16 +310,15 @@ Then open the **Profiles** tab. The recorded profile has the title
257310
**Change Detection**. In Chrome, if you record the profile repeatedly, all the
258311
profiles are nested under Change Detection.
259312

313+
260314
#### Interpreting the numbers
261315

262316
In a properly designed application, repeated attempts to detect changes without
263317
any user actions result in no changes to the UI. It is
264318
also desirable to have the cost of a user action be proportional to the amount
265319
of UI changes required. For example, popping up a menu with 5 items should be
266320
vastly faster than rendering a table of 500 rows and 10 columns. Therefore,
267-
change detection with no UI updates should be as fast as possible. Ideally the
268-
number printed by the profiler should be well below the length of a single
269-
animation frame (16ms). A good rule of thumb is to keep it under 3ms.
321+
change detection with no UI updates should be as fast as possible.
270322

271323
#### Investigating slow change detection
272324

0 commit comments

Comments
 (0)