Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
perf(WTF): extracted scopes to separate file, add documentation
Browse files Browse the repository at this point in the history
Closes #1361
  • Loading branch information
mhevery authored and chirayuk committed Aug 19, 2014
1 parent cc61dda commit ef3fb7b
Show file tree
Hide file tree
Showing 19 changed files with 436 additions and 166 deletions.
6 changes: 3 additions & 3 deletions benchmark/web/wtf.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
library wtf_test_app;

import 'package:angular/wtf.dart';
import 'package:angular/tracing.dart';
import 'dart:html';
import 'dart:js' show context;

main() {
traceInit(context);
traceDetectWTF(context);
var _main = traceCreateScope('main()');
var _querySelector = traceCreateScope('Node#querySelector()');
var _DivElement = traceCreateScope('DivElement()');
Expand All @@ -21,7 +21,7 @@ main() {
traceLeave(s);

s = traceEnter(_ElementText);
div.text = 'Hello WTF! (enabled: ${wtfEnabled})';
div.text = 'Hello WTF! (enabled: ${traceEnabled})';
traceLeave(s);

s = traceEnter(_NodeAppend);
Expand Down
1 change: 1 addition & 0 deletions lib/angular.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export 'package:angular/application.dart';
export 'package:angular/core/module.dart';
export 'package:angular/directive/module.dart';
export 'package:angular/core/annotation.dart';
export 'package:angular/tracing.dart';
export 'package:angular/introspection.dart' hide
elementExpando, publishToJavaScript;
export 'package:angular/formatter/module.dart';
Expand Down
7 changes: 3 additions & 4 deletions lib/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import 'package:angular/directive/module.dart';
import 'package:angular/formatter/module_internal.dart';
import 'package:angular/routing/module.dart';
import 'package:angular/introspection.dart';
import 'package:angular/wtf.dart';
import 'package:angular/ng_tracing.dart';

import 'package:angular/core_dom/static_keys.dart';
import 'package:angular/core_dom/directive_injector.dart';
Expand Down Expand Up @@ -130,7 +130,6 @@ class AngularModule extends Module {
* applicationFactory to bootstrap your Angular application.
*
*/
var _Application_run = traceCreateScope('Application#run()');
abstract class Application {
static _find(String selector, [dom.Element defaultElement]) {
var element = dom.document.querySelector(selector);
Expand All @@ -152,7 +151,7 @@ abstract class Application {
dom.Element selector(String selector) => element = _find(selector);

Application(): element = _find('[ng-app]', dom.window.document.documentElement) {
traceInit(context);
traceDetectWTF(context);
modules.add(ngModule);
ngModule..bind(VmTurnZone, toValue: zone)
..bind(Application, toValue: this)
Expand All @@ -175,7 +174,7 @@ abstract class Application {
}

Injector run() {
var scope = traceEnter(_Application_run);
var scope = traceEnter(Application_bootstrap);
try {
publishToJavaScript();
return zone.run(() {
Expand Down
31 changes: 14 additions & 17 deletions lib/change_detection/watch_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ library angular.watch_group;

import 'package:angular/change_detection/change_detection.dart';
import 'dart:collection';
import 'package:angular/wtf.dart';
import 'package:angular/ng_tracing.dart';

part 'linked_list.dart';
part 'ast.dart';
part 'prototype_map.dart';

var _WatchGroup_detect = traceCreateScope('WatchGroup#detect()');
var _WatchGroup_fields = traceCreateScope('WatchGroup#field()');
var _WatchGroup_field_handler = traceCreateScope('WatchGroup#field_handler()');
var _WatchGroup_eval = traceCreateScope('WatchGroup#eval()');
var _WatchGroup_reaction = traceCreateScope('WatchGroup#reaction()');

/**
* A function that is notified of changes to the model.
*
Expand Down Expand Up @@ -396,29 +390,27 @@ class RootWatchGroup extends WatchGroup {
AvgStopwatch evalStopwatch,
AvgStopwatch processStopwatch}) {
// Process the Records from the change detector
var sDetect = traceEnter(_WatchGroup_detect);
var s = traceEnter(_WatchGroup_fields);
var sDetect = traceEnter(ChangeDetector_check);
var sFields = traceEnter(ChangeDetector_fields);
Iterator<Record<_Handler>> changedRecordIterator =
(_changeDetector as ChangeDetector<_Handler>).collectChanges(
exceptionHandler:exceptionHandler,
stopwatch: fieldStopwatch);
traceLeave(s);
if (processStopwatch != null) processStopwatch.start();
s = traceEnter(_WatchGroup_field_handler);
while (changedRecordIterator.moveNext()) {
var record = changedRecordIterator.current;
if (changeLog != null) changeLog(record.handler.expression,
record.currentValue,
record.previousValue);
record.handler.onChange(record);
}
traceLeave(s);
traceLeave(sFields);
if (processStopwatch != null) processStopwatch.stop();

if (evalStopwatch != null) evalStopwatch.start();
// Process our own function evaluations
_EvalWatchRecord evalRecord = _evalWatchHead;
s = traceEnter(_WatchGroup_eval);
var sEval = traceEnter(ChangeDetector_eval);
int evalCount = 0;
while (evalRecord != null) {
try {
Expand All @@ -434,14 +426,14 @@ class RootWatchGroup extends WatchGroup {
evalRecord = evalRecord._nextEvalWatch;
}

traceLeave(s);
traceLeave(sEval);
traceLeave(sDetect);
if (evalStopwatch != null) evalStopwatch..stop()..increment(evalCount);

// Because the handler can forward changes between each other synchronously
// We need to call reaction functions asynchronously. This processes the
// asynchronous reaction function queue.
s = traceEnter(_WatchGroup_reaction);
var sReaction = traceEnter(ChangeDetector_reaction);
int count = 0;
if (processStopwatch != null) processStopwatch.start();
Watch dirtyWatch = _dirtyWatchHead;
Expand All @@ -465,7 +457,7 @@ class RootWatchGroup extends WatchGroup {
_dirtyWatchTail = null;
root._removeCount = 0;
}
traceLeave(s);
traceLeaveVal(sReaction, count);
if (processStopwatch != null) processStopwatch..stop()..increment(count);
return count;
}
Expand Down Expand Up @@ -511,7 +503,12 @@ class Watch {
void invoke() {
if (_deleted || !_dirty) return;
_dirty = false;
reactionFn(_record.currentValue, _record.previousValue);
var s = traceEnabled ? traceEnter1(ChangeDetector_invoke, expression) : null;
try {
reactionFn(_record.currentValue, _record.previousValue);
} finally {
if (traceEnabled) traceLeave(s);
}
}

void remove() {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/module_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:di/annotations.dart';
import 'package:angular/core/parser/parser.dart';
import 'package:angular/core/parser/lexer.dart';
import 'package:angular/utils.dart';
import 'package:angular/wtf.dart';
import 'package:angular/ng_tracing.dart';

import 'package:angular/core/annotation_src.dart';

Expand Down
24 changes: 8 additions & 16 deletions lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ part of angular.core_internal;
typedef EvalFunction0();
typedef EvalFunction1(context);

var _Scope_apply = traceCreateScope('Scope#apply()');
var _Scope_digest = traceCreateScope('Scope#digest()');
var _Scope_flush = traceCreateScope('Scope#flush()');
var _Scope_domWrite = traceCreateScope('Scope#domWrite()');
var _Scope_domRead = traceCreateScope('Scope#domRead()');
var _Scope_assert = traceCreateScope('Scope#assert()');
var _Scope_runAsync = traceCreateScope('Scope#runAsync()');
var _Scope_createChild = traceCreateScope('Scope#createChild()');
/**
* Injected into the listener function within [Scope.on] to provide event-specific details to the
* scope listener.
Expand Down Expand Up @@ -338,7 +330,7 @@ class Scope {

/// Creates a child [Scope] with the given [childContext]
Scope createChild(Object childContext) {
var s = traceEnter(_Scope_createChild);
var s = traceEnter(Scope_createChild);
assert(isAttached);
var child = new Scope(childContext, rootScope, this,
_readWriteGroup.newGroup(childContext),
Expand Down Expand Up @@ -746,7 +738,7 @@ class RootScope extends Scope {
try {
do {
if (_domWriteHead != null) _stats.domWriteStart();
var s = traceEnter(_Scope_domWrite);
var s = traceEnter(Scope_domWrite);
while (_domWriteHead != null) {
try {
_domWriteHead.fn();
Expand All @@ -766,7 +758,7 @@ class RootScope extends Scope {
processStopwatch: _scopeStats.processStopwatch);
}
if (_domReadHead != null) _stats.domReadStart();
s = traceEnter(_Scope_domRead);
s = traceEnter(Scope_domRead);
while (_domReadHead != null) {
try {
_domReadHead.fn();
Expand Down Expand Up @@ -823,7 +815,7 @@ class RootScope extends Scope {
}

_runAsyncFns() {
var s = traceEnter(_Scope_runAsync);
var s = traceEnter(Scope_execAsync);
var count = 0;
while (_runAsyncHead != null) {
try {
Expand Down Expand Up @@ -865,10 +857,10 @@ class RootScope extends Scope {
_state = to;
if (_state_wtf_scope != null) traceLeave(_state_wtf_scope);
var wtfScope = null;
if (to == STATE_APPLY) wtfScope = _Scope_apply;
else if (to == STATE_DIGEST) wtfScope = _Scope_digest;
else if (to == STATE_FLUSH) wtfScope = _Scope_flush;
else if (to == STATE_FLUSH_ASSERT) wtfScope = _Scope_assert;
if (to == STATE_APPLY) wtfScope = Scope_apply;
else if (to == STATE_DIGEST) wtfScope = Scope_digest;
else if (to == STATE_FLUSH) wtfScope = Scope_flush;
else if (to == STATE_FLUSH_ASSERT) wtfScope = Scope_assert;
_state_wtf_scope = wtfScope == null ? null : traceEnter(wtfScope);
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/core/zone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class LongStackTrace {
}
}

var _VmTurnZone_onRunBase = traceCreateScope('VmTurnZone#onRun()');
var _VmTurnZone_onScheduleMicrotask = traceCreateScope('VmTurnZone#onScheduleMicrotask()');

/**
* A [Zone] wrapper that lets you schedule tasks after its private microtask
* queue is exhausted but before the next "turn", i.e. event loop iteration.
Expand Down Expand Up @@ -92,7 +89,7 @@ class VmTurnZone {
var _currentlyInTurn = false;

dynamic _onRunBase(async.Zone self, async.ZoneDelegate delegate, async.Zone zone, fn()) {
var scope = traceEnter(_VmTurnZone_onRunBase);
var scope = traceEnter(VmTurnZone_run);
_runningInTurn++;
try {
if (!_currentlyInTurn) {
Expand Down Expand Up @@ -120,7 +117,7 @@ class VmTurnZone {
_onRunBase(self, delegate, zone, () => delegate.runUnary(zone, fn, args));

void _onScheduleMicrotask(async.Zone self, async.ZoneDelegate delegate, async.Zone zone, fn()) {
var s = traceEnter(_VmTurnZone_onScheduleMicrotask);
var s = traceEnter(VmTurnZone_scheduleMicrotask);
try {
onScheduleMicrotask(() => delegate.run(zone, fn));
if (_runningInTurn == 0 && !_inFinishTurn) _finishTurn(zone, delegate);
Expand Down
6 changes: 2 additions & 4 deletions lib/core_dom/compiler.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
part of angular.core.dom_internal;

var _Compiler_call = traceCreateScope('Compiler#call()');
var _Compiler_subTemplate = traceCreateScope('Compiler#subTemplate()');

@Injectable()
class Compiler implements Function {
Expand All @@ -11,7 +9,7 @@ class Compiler implements Function {
Compiler(this._perf, this._expando);

ViewFactory call(List<dom.Node> elements, DirectiveMap directives) {
var s = traceEnter(_Compiler_call);
var s = traceEnter(Compiler_compile);
var timerId;
assert((timerId = _perf.startTimer('ng.compile', _html(elements))) != false);
final elementBinders = <TaggedElementBinder>[];
Expand Down Expand Up @@ -141,7 +139,7 @@ class Compiler implements Function {
DirectiveRef directiveRef,
ElementBinder transcludedElementBinder,
DirectiveMap directives) {
var s = traceEnter(_Compiler_subTemplate);
var s = traceEnter(Compiler_template);
var anchorName = directiveRef.annotation.selector +
(directiveRef.value != null ? '=' + directiveRef.value : '');

Expand Down
12 changes: 2 additions & 10 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
part of angular.core.dom_internal;

var _ElementBinder_directive = traceCreateScope('ElementBinder#createDirective(ascii name)');
var _ElementBinder_setupBindings = traceCreateScope('ElementBinder#setupBindings(ascii name)');

class TemplateElementBinder extends ElementBinder {
final DirectiveRef template;
Expand Down Expand Up @@ -193,19 +191,13 @@ class ElementBinder {
for(var i = 0; i < _usableDirectiveRefs.length; i++) {
DirectiveRef ref = _usableDirectiveRefs[i];
var key = ref.typeKey;
var wtfArgs = wtfEnabled ? [ref.typeKey.toString()] : null;
var directiveName = traceEnabled ? ref.typeKey.toString() : null;
if (identical(key, TEXT_MUSTACHE_KEY) || identical(key, ATTR_MUSTACHE_KEY)) continue;

s = traceEnter(_ElementBinder_directive, wtfArgs);
s = traceEnter1(Directive_create, directiveName);
var directive;
try {
directive = directiveInjector.getByKey(ref.typeKey);
} finally {
traceLeave(s);
}

s = traceEnter(_ElementBinder_setupBindings, wtfArgs);
try {
if (ref.annotation is Controller) {
scope.parentScope.context[(ref.annotation as Controller).publishAs] = directive;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/core_dom/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ class Http {
cache,
timeout
}) {
var range = wtfEnabled ? traceAsyncStart('http:$method', url) : null;
var range = traceEnabled ? traceAsyncStart('http:$method', url) : null;
if (timeout != null) {
throw ['timeout not implemented'];
}
Expand Down Expand Up @@ -545,7 +545,7 @@ class Http {
var result = chainResult is async.Future
? chainResult
: new async.Future.value(chainResult);
if (wtfEnabled) {
if (traceEnabled) {
return new async.Future(() {
traceAsyncEnd(range);
return result;
Expand Down
2 changes: 1 addition & 1 deletion lib/core_dom/module_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export 'package:angular/core_dom/directive_injector.dart' show DirectiveInjector
import 'package:angular/change_detection/watch_group.dart' show Watch, PrototypeMap;
import 'package:angular/change_detection/ast_parser.dart';
import 'package:angular/core/registry.dart';
import 'package:angular/wtf.dart';
import 'package:angular/ng_tracing.dart';

import 'package:angular/directive/module.dart' show NgBaseCss;
import 'dart:collection';
Expand Down
7 changes: 2 additions & 5 deletions lib/core_dom/shadow_dom_component_factory.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
part of angular.core.dom_internal;

var _ComponentFactory_call = traceCreateScope('ComponentFactory#call()');
var _ComponentFactory_styles = traceCreateScope('ComponentFactory#styles()');

abstract class ComponentFactory {
BoundComponentFactory bind(DirectiveRef ref, directives);
}
Expand Down Expand Up @@ -134,7 +131,7 @@ class BoundShadowDomComponentFactory implements BoundComponentFactory {
Function call(dom.Element element) {
return (DirectiveInjector injector, Scope scope, NgBaseCss baseCss,
EventHandler eventHandler) {
var s = traceEnter(_ComponentFactory_call);
var s = traceEnter(View_createComponent);
try {
var shadowScope = scope.createChild(new HashMap()); // Isolate
ComponentDirectiveInjector shadowInjector;
Expand Down Expand Up @@ -203,7 +200,7 @@ class BoundShadowDomComponentFactory implements BoundComponentFactory {
_insertCss(List<dom.StyleElement> cssList,
dom.ShadowRoot shadowRoot,
[dom.Node insertBefore = null]) {
var s = traceEnter(_ComponentFactory_styles);
var s = traceEnter(View_styles);
for(int i = 0; i < cssList.length; i++) {
var styleElement = cssList[i];
if (styleElement != null) {
Expand Down
Loading

0 comments on commit ef3fb7b

Please sign in to comment.