Skip to content

Commit b08255b

Browse files
committed
b1a9e44 fix(perf): don’t use try/catch in production mode
1 parent 805e007 commit b08255b

File tree

7 files changed

+132
-104
lines changed

7 files changed

+132
-104
lines changed

BUILD_INFO

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Fri Apr 29 08:54:29 UTC 2016
2-
5297c9d9ccc6f8831d1656915e3d78e767022517
1+
Fri Apr 29 17:38:25 UTC 2016
2+
b1a9e445b3ee4fa1cf9a7d65c920158a447256f1

lib/src/compiler/identifiers.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ library angular2.src.compiler.identifiers;
22

33
import "compile_metadata.dart"
44
show CompileIdentifierMetadata, CompileTokenMetadata;
5-
import "package:angular2/src/core/linker/view.dart" show AppView;
5+
import "package:angular2/src/core/linker/view.dart" show AppView, DebugAppView;
66
import "package:angular2/src/core/linker/debug_context.dart"
77
show StaticNodeDebugInfo, DebugContext;
88
import "package:angular2/src/core/linker/view_utils.dart"
@@ -59,6 +59,7 @@ var CD_MODULE_URL =
5959
// (only needed for Dart).
6060
var impViewUtils = ViewUtils;
6161
var impAppView = AppView;
62+
var impDebugAppView = DebugAppView;
6263
var impDebugContext = DebugContext;
6364
var impAppElement = AppElement;
6465
var impElementRef = ElementRef;
@@ -92,6 +93,10 @@ class Identifiers {
9293
runtime: impViewUtils);
9394
static var AppView = new CompileIdentifierMetadata(
9495
name: "AppView", moduleUrl: APP_VIEW_MODULE_URL, runtime: impAppView);
96+
static var DebugAppView = new CompileIdentifierMetadata(
97+
name: "DebugAppView",
98+
moduleUrl: APP_VIEW_MODULE_URL,
99+
runtime: impDebugAppView);
95100
static var AppElement = new CompileIdentifierMetadata(
96101
name: "AppElement",
97102
moduleUrl: "asset:angular2/lib/src/core/linker/element" + MODULE_SUFFIX,

lib/src/compiler/output/interpretive_view.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
library angular2.src.compiler.output.interpretive_view;
22

33
import "package:angular2/src/facade/lang.dart" show isPresent;
4-
import "package:angular2/src/core/linker/view.dart" show AppView;
4+
import "package:angular2/src/core/linker/view.dart" show AppView, DebugAppView;
55
import "package:angular2/src/core/linker/element.dart" show AppElement;
66
import "package:angular2/src/facade/exceptions.dart" show BaseException;
77
import "output_interpreter.dart" show InstanceFactory, DynamicInstance;
@@ -15,14 +15,23 @@ class InterpretiveAppViewInstanceFactory implements InstanceFactory {
1515
Map<String, Function> getters,
1616
Map<String, Function> methods) {
1717
if (identical(superClass, AppView)) {
18+
// We are always using DebugAppView as parent.
19+
20+
// However, in prod mode we generate a constructor call that does
21+
22+
// not have the argument for the debugNodeInfos.
23+
args = (new List.from(args)..addAll([null]));
24+
return new _InterpretiveAppView(args, props, getters, methods);
25+
} else if (identical(superClass, DebugAppView)) {
1826
return new _InterpretiveAppView(args, props, getters, methods);
1927
}
2028
throw new BaseException(
2129
'''Can\'t instantiate class ${ superClass} in interpretative mode''');
2230
}
2331
}
2432

25-
class _InterpretiveAppView extends AppView<dynamic> implements DynamicInstance {
33+
class _InterpretiveAppView extends DebugAppView<dynamic>
34+
implements DynamicInstance {
2635
Map<String, dynamic> props;
2736
Map<String, Function> getters;
2837
Map<String, Function> methods;

lib/src/compiler/view_compiler/view_builder.dart

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -481,18 +481,20 @@ o.ClassStmt createViewClass(CompileView view, o.ReadVarExpr renderCompTypeVar,
481481
new o.FnParam(ViewConstructorVars.declarationEl.name,
482482
o.importType(Identifiers.AppElement))
483483
];
484-
var viewConstructor = new o.ClassMethod(null, viewConstructorArgs, [
485-
o.SUPER_EXPR.callFn([
486-
o.variable(view.className),
487-
renderCompTypeVar,
488-
ViewTypeEnum.fromValue(view.viewType),
489-
ViewConstructorVars.viewUtils,
490-
ViewConstructorVars.parentInjector,
491-
ViewConstructorVars.declarationEl,
492-
ChangeDetectionStrategyEnum.fromValue(getChangeDetectionMode(view)),
493-
nodeDebugInfosVar
494-
]).toStmt()
495-
]);
484+
var superConstructorArgs = [
485+
o.variable(view.className),
486+
renderCompTypeVar,
487+
ViewTypeEnum.fromValue(view.viewType),
488+
ViewConstructorVars.viewUtils,
489+
ViewConstructorVars.parentInjector,
490+
ViewConstructorVars.declarationEl,
491+
ChangeDetectionStrategyEnum.fromValue(getChangeDetectionMode(view))
492+
];
493+
if (view.genConfig.genDebugInfo) {
494+
superConstructorArgs.add(nodeDebugInfosVar);
495+
}
496+
var viewConstructor = new o.ClassMethod(null, viewConstructorArgs,
497+
[o.SUPER_EXPR.callFn(superConstructorArgs).toStmt()]);
496498
var viewMethods = (new List.from([
497499
new o.ClassMethod(
498500
"createInternal",
@@ -518,9 +520,12 @@ o.ClassStmt createViewClass(CompileView view, o.ReadVarExpr renderCompTypeVar,
518520
view.dirtyParentQueriesMethod.finish()),
519521
new o.ClassMethod("destroyInternal", [], view.destroyMethod.finish())
520522
])..addAll(view.eventHandlerMethods));
523+
var superClass = view.genConfig.genDebugInfo
524+
? Identifiers.DebugAppView
525+
: Identifiers.AppView;
521526
var viewClass = new o.ClassStmt(
522527
view.className,
523-
o.importExpr(Identifiers.AppView, [getContextType(view)]),
528+
o.importExpr(superClass, [getContextType(view)]),
524529
view.fields,
525530
view.getters,
526531
viewConstructor,

lib/src/core/linker/debug_context.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "package:angular2/src/facade/collection.dart"
55
show ListWrapper, StringMapWrapper;
66
import "package:angular2/src/core/di.dart" show Injector;
77
import "package:angular2/src/core/render/api.dart" show RenderDebugInfo;
8-
import "view.dart" show AppView;
8+
import "view.dart" show DebugAppView;
99
import "view_type.dart" show ViewType;
1010

1111
class StaticNodeDebugInfo {
@@ -17,7 +17,7 @@ class StaticNodeDebugInfo {
1717
}
1818

1919
class DebugContext implements RenderDebugInfo {
20-
AppView<dynamic> _view;
20+
DebugAppView<dynamic> _view;
2121
num _nodeIndex;
2222
num _tplRow;
2323
num _tplCol;
@@ -44,7 +44,8 @@ class DebugContext implements RenderDebugInfo {
4444
var componentView = this._view;
4545
while (isPresent(componentView.declarationAppElement) &&
4646
!identical(componentView.type, ViewType.COMPONENT)) {
47-
componentView = componentView.declarationAppElement.parentView;
47+
componentView = (componentView.declarationAppElement.parentView
48+
as DebugAppView<dynamic>);
4849
}
4950
return isPresent(componentView.declarationAppElement)
5051
? componentView.declarationAppElement.nativeElement

lib/src/core/linker/view.dart

Lines changed: 89 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import "package:angular2/src/facade/lang.dart"
2323
isString;
2424
import "package:angular2/src/facade/async.dart" show ObservableWrapper;
2525
import "package:angular2/src/core/render/api.dart"
26-
show Renderer, RootRenderer, RenderComponentType;
26+
show Renderer, RootRenderer, RenderComponentType, RenderDebugInfo;
2727
import "view_ref.dart" show ViewRef_;
2828
import "view_type.dart" show ViewType;
2929
import "view_utils.dart"
@@ -63,7 +63,6 @@ abstract class AppView<T> {
6363
Injector parentInjector;
6464
AppElement declarationAppElement;
6565
ChangeDetectionStrategy cdMode;
66-
List<StaticNodeDebugInfo> staticNodeDebugInfos;
6766
ViewRef_<T> ref;
6867
List<dynamic> rootNodesOrAppElements;
6968
List<dynamic> allNodes;
@@ -80,18 +79,10 @@ abstract class AppView<T> {
8079
List<dynamic /* dynamic | List < dynamic > */ > projectableNodes;
8180
bool destroyed = false;
8281
Renderer renderer;
83-
DebugContext _currentDebugContext = null;
8482
bool _hasExternalHostElement;
8583
T context;
86-
AppView(
87-
this.clazz,
88-
this.componentType,
89-
this.type,
90-
this.viewUtils,
91-
this.parentInjector,
92-
this.declarationAppElement,
93-
this.cdMode,
94-
this.staticNodeDebugInfos) {
84+
AppView(this.clazz, this.componentType, this.type, this.viewUtils,
85+
this.parentInjector, this.declarationAppElement, this.cdMode) {
9586
this.ref = new ViewRef_(this);
9687
if (identical(type, ViewType.COMPONENT) || identical(type, ViewType.HOST)) {
9788
this.renderer = viewUtils.renderComponent(componentType);
@@ -123,17 +114,7 @@ abstract class AppView<T> {
123114
}
124115
this._hasExternalHostElement = isPresent(rootSelectorOrNode);
125116
this.projectableNodes = projectableNodes;
126-
if (this.debugMode) {
127-
this._resetDebug();
128-
try {
129-
return this.createInternal(rootSelectorOrNode);
130-
} catch (e, e_stack) {
131-
this._rethrowWithContext(e, e_stack);
132-
rethrow;
133-
}
134-
} else {
135-
return this.createInternal(rootSelectorOrNode);
136-
}
117+
return this.createInternal(rootSelectorOrNode);
137118
}
138119

139120
/**
@@ -163,29 +144,19 @@ abstract class AppView<T> {
163144
dynamic selectOrCreateHostElement(
164145
String elementName,
165146
dynamic /* String | dynamic */ rootSelectorOrNode,
166-
DebugContext debugCtx) {
147+
RenderDebugInfo debugInfo) {
167148
var hostElement;
168149
if (isPresent(rootSelectorOrNode)) {
169150
hostElement =
170-
this.renderer.selectRootElement(rootSelectorOrNode, debugCtx);
151+
this.renderer.selectRootElement(rootSelectorOrNode, debugInfo);
171152
} else {
172-
hostElement = this.renderer.createElement(null, elementName, debugCtx);
153+
hostElement = this.renderer.createElement(null, elementName, debugInfo);
173154
}
174155
return hostElement;
175156
}
176157

177158
dynamic injectorGet(dynamic token, num nodeIndex, dynamic notFoundResult) {
178-
if (this.debugMode) {
179-
this._resetDebug();
180-
try {
181-
return this.injectorGetInternal(token, nodeIndex, notFoundResult);
182-
} catch (e, e_stack) {
183-
this._rethrowWithContext(e, e_stack);
184-
rethrow;
185-
}
186-
} else {
187-
return this.injectorGetInternal(token, nodeIndex, notFoundResult);
188-
}
159+
return this.injectorGetInternal(token, nodeIndex, notFoundResult);
189160
}
190161

191162
/**
@@ -227,21 +198,11 @@ abstract class AppView<T> {
227198
for (var i = 0; i < children.length; i++) {
228199
children[i]._destroyRecurse();
229200
}
230-
if (this.debugMode) {
231-
this._resetDebug();
232-
try {
233-
this._destroyLocal();
234-
} catch (e, e_stack) {
235-
this._rethrowWithContext(e, e_stack);
236-
rethrow;
237-
}
238-
} else {
239-
this._destroyLocal();
240-
}
201+
this.destroyLocal();
241202
this.destroyed = true;
242203
}
243204

244-
_destroyLocal() {
205+
destroyLocal() {
245206
var hostElement = identical(this.type, ViewType.COMPONENT)
246207
? this.declarationAppElement.nativeElement
247208
: null;
@@ -268,10 +229,6 @@ abstract class AppView<T> {
268229
* Overwritten by implementations
269230
*/
270231
void destroyInternal() {}
271-
bool get debugMode {
272-
return isPresent(this.staticNodeDebugInfos);
273-
}
274-
275232
ChangeDetectorRef get changeDetectorRef {
276233
return this.ref;
277234
}
@@ -317,17 +274,7 @@ abstract class AppView<T> {
317274
if (this.destroyed) {
318275
this.throwDestroyedError("detectChanges");
319276
}
320-
if (this.debugMode) {
321-
this._resetDebug();
322-
try {
323-
this.detectChangesInternal(throwOnChange);
324-
} catch (e, e_stack) {
325-
this._rethrowWithContext(e, e_stack);
326-
rethrow;
327-
}
328-
} else {
329-
this.detectChangesInternal(throwOnChange);
330-
}
277+
this.detectChangesInternal(throwOnChange);
331278
if (identical(this.cdMode, ChangeDetectionStrategy.CheckOnce))
332279
this.cdMode = ChangeDetectionStrategy.Checked;
333280
this.cdState = ChangeDetectorState.CheckedBefore;
@@ -381,6 +328,74 @@ abstract class AppView<T> {
381328
}
382329
}
383330

331+
Function eventHandler(Function cb) {
332+
return cb;
333+
}
334+
335+
void throwDestroyedError(String details) {
336+
throw new ViewDestroyedException(details);
337+
}
338+
}
339+
340+
class DebugAppView<T> extends AppView<T> {
341+
List<StaticNodeDebugInfo> staticNodeDebugInfos;
342+
DebugContext _currentDebugContext = null;
343+
DebugAppView(
344+
dynamic clazz,
345+
RenderComponentType componentType,
346+
ViewType type,
347+
ViewUtils viewUtils,
348+
Injector parentInjector,
349+
AppElement declarationAppElement,
350+
ChangeDetectionStrategy cdMode,
351+
this.staticNodeDebugInfos)
352+
: super(clazz, componentType, type, viewUtils, parentInjector,
353+
declarationAppElement, cdMode) {
354+
/* super call moved to initializer */;
355+
}
356+
AppElement create(
357+
T context,
358+
List<dynamic /* dynamic | List < dynamic > */ > givenProjectableNodes,
359+
dynamic /* String | dynamic */ rootSelectorOrNode) {
360+
this._resetDebug();
361+
try {
362+
return super.create(context, givenProjectableNodes, rootSelectorOrNode);
363+
} catch (e, e_stack) {
364+
this._rethrowWithContext(e, e_stack);
365+
rethrow;
366+
}
367+
}
368+
369+
dynamic injectorGet(dynamic token, num nodeIndex, dynamic notFoundResult) {
370+
this._resetDebug();
371+
try {
372+
return super.injectorGet(token, nodeIndex, notFoundResult);
373+
} catch (e, e_stack) {
374+
this._rethrowWithContext(e, e_stack);
375+
rethrow;
376+
}
377+
}
378+
379+
destroyLocal() {
380+
this._resetDebug();
381+
try {
382+
super.destroyLocal();
383+
} catch (e, e_stack) {
384+
this._rethrowWithContext(e, e_stack);
385+
rethrow;
386+
}
387+
}
388+
389+
void detectChanges(bool throwOnChange) {
390+
this._resetDebug();
391+
try {
392+
super.detectChanges(throwOnChange);
393+
} catch (e, e_stack) {
394+
this._rethrowWithContext(e, e_stack);
395+
rethrow;
396+
}
397+
}
398+
384399
_resetDebug() {
385400
this._currentDebugContext = null;
386401
}
@@ -402,23 +417,16 @@ abstract class AppView<T> {
402417
}
403418

404419
Function eventHandler(Function cb) {
405-
if (this.debugMode) {
406-
return (event) {
407-
this._resetDebug();
408-
try {
409-
return cb(event);
410-
} catch (e, e_stack) {
411-
this._rethrowWithContext(e, e_stack);
412-
rethrow;
413-
}
414-
};
415-
} else {
416-
return cb;
417-
}
418-
}
419-
420-
void throwDestroyedError(String details) {
421-
throw new ViewDestroyedException(details);
420+
var superHandler = super.eventHandler(cb);
421+
return (event) {
422+
this._resetDebug();
423+
try {
424+
return superHandler(event);
425+
} catch (e, e_stack) {
426+
this._rethrowWithContext(e, e_stack);
427+
rethrow;
428+
}
429+
};
422430
}
423431
}
424432

0 commit comments

Comments
 (0)