Skip to content

Commit d64f5cb

Browse files
jbdeboerDiana Salsbury
authored and
Diana Salsbury
committed
perf(view cache): Avoid http.get
This change makes templateUrl component creation 100% faster. It is a stop-gap until dart-archive#1107 is fixed. Closes dart-archive#1108
1 parent bbd2afa commit d64f5cb

File tree

6 files changed

+26
-9
lines changed

6 files changed

+26
-9
lines changed

benchmark/web/tree-tmpl.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<span> {{ctrl.data.value}}
2+
<span ng-if="ctrl.data.right != null"><tree-url data=ctrl.data.right></span>
3+
<span ng-if="ctrl.data.left != null"><tree-url data=ctrl.data.left></span>
4+
</span>

benchmark/web/tree.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class TreeComponent {
1919
var data;
2020
}
2121

22+
@Component(
23+
selector: 'tree-url',
24+
templateUrl: 'tree-tmpl.html',
25+
publishAs: 'ctrl')
26+
class TreeUrlComponent {
27+
@NgOneWay('data')
28+
var data;
29+
}
30+
2231

2332
// This is a baseline implementation of TreeComponent.
2433
// It assumes the data never changes and simply throws elements on the DOM
@@ -239,6 +248,7 @@ main() {
239248

240249
var module = new Module()
241250
..type(TreeComponent)
251+
..type(TreeUrlComponent)
242252
..type(NgFreeTree)
243253
..type(NgFreeTreeScoped)
244254
..type(NgFreeTreeClass)

benchmark/web/tree.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
</p>
1515

1616
<div>Default: <input type=checkbox ng-model="useDefault"></div>
17+
<div>From URL: <input type=checkbox ng-model="useUrl"></div>
1718
<div>Baseline: <input type=checkbox ng-model="useBaseline"></div>
1819
<div>Baseline + scope: <input type=checkbox ng-model="useBaselineScoped"></div>
1920
<div>Baseline + class: <input type=checkbox ng-model="useBaselineClass"></div>
2021

2122

2223
<tree ng-if="useDefault" data=initData></tree>
24+
<tree-url ng-if="useUrl" data=initData></tree-url>
2325

2426
<ng-free-tree ng-if="useBaseline" data=initData></ng-free-tree>
2527
<ng-free-tree-scoped ng-if="useBaselineScoped" data=initData></ng-free-tree-scoped>

lib/core_dom/view_factory.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,4 @@ class ElementProbe {
216216
final directives = [];
217217

218218
ElementProbe(this.parent, this.element, this.injector, this.scope);
219-
}
219+
}

lib/core_dom/web_platform.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@ class PlatformViewCache implements ViewCache {
9191
}
9292

9393
async.Future<ViewFactory> fromUrl(String url, DirectiveMap directives) {
94-
return http.get(url, cache: templateCache).then(
95-
(resp) => fromHtml(resp.responseText, directives));
94+
ViewFactory viewFactory = viewFactoryCache.get(url);
95+
if (viewFactory == null) {
96+
return http.get(url, cache: templateCache).then((resp) {
97+
var viewFactoryFromHttp = fromHtml(resp.responseText, directives);
98+
viewFactoryCache.put(url, viewFactoryFromHttp);
99+
return viewFactoryFromHttp;
100+
});
101+
}
102+
return new async.Future.value(viewFactory);
96103
}
97104
}

test/core/templateurl_spec.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,9 @@ void main() {
314314
'<style>.hello{}</style><div log="SIMPLE">Simple!</div>'
315315
);
316316

317-
// Since the template cache is disabled, we expect a 'simple.html' call.
318-
backend
319-
..expectGET('simple.html').respond(200, '<div log="SIMPLE">Simple!</div>');
320-
321317
var element2 = e('<div><html-and-css>ignore</html-and-css><div>');
322318
compile([element2], directives)(injector, [element2]);
323319

324-
microLeap();
325-
backend.flush();
326320
microLeap();
327321

328322
expect(element2.children[0].shadowRoot).toHaveHtml(

0 commit comments

Comments
 (0)