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

Commit 57da29d

Browse files
committed
perf(various): Avoid putIfAbsent
This code was originally authored by @mhevery in the DirectiveInjector change.
1 parent 7d75678 commit 57da29d

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

lib/change_detection/dirty_checking_change_detector.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,12 @@ class DuplicateMap {
14171417
final map = new HashMap<dynamic, _DuplicateItemRecordList>();
14181418

14191419
void put(ItemRecord record, [ItemRecord insertBefore = null]) {
1420-
map.putIfAbsent(record.item, () => new _DuplicateItemRecordList()).add(record, insertBefore);
1420+
var key = record.item;
1421+
_DuplicateItemRecordList duplicates = map[key];
1422+
if (duplicates == null) {
1423+
duplicates = map[key] = new _DuplicateItemRecordList();
1424+
}
1425+
duplicates.add(record, insertBefore);
14211426
}
14221427

14231428
/**

lib/change_detection/watch_group.dart

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList {
139139
}
140140

141141
Watch watch(AST expression, ReactionFn reactionFn) {
142-
WatchRecord<_Handler> watchRecord =
143-
_cache.putIfAbsent(expression.expression,
144-
() => expression.setupWatch(this));
142+
WatchRecord<_Handler> watchRecord = _cache[expression.expression];
143+
if (watchRecord == null) {
144+
_cache[expression.expression] = watchRecord = expression.setupWatch(this);
145+
}
145146
return watchRecord.handler.addReactionFn(reactionFn);
146147
}
147148

@@ -160,8 +161,10 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList {
160161
_fieldCost++;
161162
fieldHandler.watchRecord = watchRecord;
162163

163-
WatchRecord<_Handler> lhsWR = _cache.putIfAbsent(lhs.expression,
164-
() => lhs.setupWatch(this));
164+
WatchRecord<_Handler> lhsWR = _cache[lhs.expression];
165+
if (lhsWR == null) {
166+
lhsWR = _cache[lhs.expression] = lhs.setupWatch(this);
167+
}
165168

166169
// We set a field forwarding handler on LHS. This will allow the change
167170
// objects to propagate to the current WatchRecord.
@@ -177,8 +180,10 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList {
177180
var watchRecord = _changeDetector.watch(null, null, collectionHandler);
178181
_collectionCost++;
179182
collectionHandler.watchRecord = watchRecord;
180-
WatchRecord<_Handler> astWR = _cache.putIfAbsent(ast.expression,
181-
() => ast.setupWatch(this));
183+
WatchRecord<_Handler> astWR = _cache[ast.expression];
184+
if (astWR == null) {
185+
astWR = _cache[ast.expression] = ast.setupWatch(this);
186+
}
182187

183188
// We set a field forwarding handler on LHS. This will allow the change
184189
// objects to propagate to the current WatchRecord.
@@ -229,26 +234,32 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList {
229234
invokeHandler.watchRecord = evalWatchRecord;
230235

231236
if (lhsAST != null) {
232-
var lhsWR = _cache.putIfAbsent(lhsAST.expression,
233-
() => lhsAST.setupWatch(this));
237+
var lhsWR = _cache[lhsAST.expression];
238+
if (lhsWR == null) {
239+
lhsWR = _cache[lhsAST.expression] = lhsAST.setupWatch(this);
240+
}
234241
lhsWR.handler.addForwardHandler(invokeHandler);
235242
invokeHandler.acceptValue(lhsWR.currentValue);
236243
}
237244

238245
// Convert the args from AST to WatchRecords
239246
for (var i = 0; i < argsAST.length; i++) {
240247
var ast = argsAST[i];
241-
WatchRecord<_Handler> record =
242-
_cache.putIfAbsent(ast.expression, () => ast.setupWatch(this));
248+
WatchRecord<_Handler> record = _cache[ast.expression];
249+
if (record == null) {
250+
record = _cache[ast.expression] = ast.setupWatch(this);
251+
}
243252
_ArgHandler handler = new _PositionalArgHandler(this, evalWatchRecord, i);
244253
_ArgHandlerList._add(invokeHandler, handler);
245254
record.handler.addForwardHandler(handler);
246255
handler.acceptValue(record.currentValue);
247256
}
248257

249258
namedArgsAST.forEach((Symbol name, AST ast) {
250-
WatchRecord<_Handler> record = _cache.putIfAbsent(ast.expression,
251-
() => ast.setupWatch(this));
259+
WatchRecord<_Handler> record = _cache[ast.expression];
260+
if (record == null) {
261+
record = _cache[ast.expression] = ast.setupWatch(this);
262+
}
252263
_ArgHandler handler = new _NamedArgHandler(this, evalWatchRecord, name);
253264
_ArgHandlerList._add(invokeHandler, handler);
254265
record.handler.addForwardHandler(handler);

lib/core_dom/element_binder.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ class ElementBinder {
118118

119119
void _createAttrMappings(directive, scope, List<MappingParts> mappings, nodeAttrs, tasks) {
120120
Scope directiveScope; // Only created if there is a two-way binding in the element.
121-
mappings.forEach((MappingParts p) {
121+
for(var i = 0; i < mappings.length; i++) {
122+
MappingParts p = mappings[i];
122123
var attrName = p.attrName;
123124
var attrValueAST = p.attrValueAST;
124125
AST dstAST = p.dstAST;
@@ -142,7 +143,7 @@ class ElementBinder {
142143
} else {
143144
_bindOneWay(tasks, bindAttr, scope, dstAST, directive);
144145
}
145-
return;
146+
continue;
146147
}
147148

148149
switch (p.mode) {
@@ -155,7 +156,7 @@ class ElementBinder {
155156
break;
156157

157158
case '<=>': // two-way
158-
if (nodeAttrs[attrName] == null) return;
159+
if (nodeAttrs[attrName] == null) continue;
159160
if (directiveScope == null) {
160161
directiveScope = scope.createChild(directive);
161162
}
@@ -164,13 +165,12 @@ class ElementBinder {
164165
break;
165166

166167
case '=>': // one-way
167-
if (nodeAttrs[attrName] == null) return;
168-
_bindOneWay(tasks, attrValueAST, scope,
169-
dstAST, directive);
168+
if (nodeAttrs[attrName] == null) continue;
169+
_bindOneWay(tasks, attrValueAST, scope, dstAST, directive);
170170
break;
171171

172172
case '=>!': // one-way, one-time
173-
if (nodeAttrs[attrName] == null) return;
173+
if (nodeAttrs[attrName] == null) continue;
174174

175175
var watch;
176176
var lastOneTimeValue;
@@ -193,7 +193,7 @@ class ElementBinder {
193193
_bindCallback(dstAST.parsedExp, directive, nodeAttrs[attrName], scope);
194194
break;
195195
}
196-
});
196+
}
197197
}
198198

199199
void _link(nodeInjector, probe, scope, nodeAttrs) {

0 commit comments

Comments
 (0)