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

Commit

Permalink
perf(various): Avoid putIfAbsent
Browse files Browse the repository at this point in the history
This code was originally authored by @mhevery in the
DirectiveInjector change.
  • Loading branch information
jbdeboer committed Jul 9, 2014
1 parent 7d75678 commit 57da29d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
7 changes: 6 additions & 1 deletion lib/change_detection/dirty_checking_change_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,12 @@ class DuplicateMap {
final map = new HashMap<dynamic, _DuplicateItemRecordList>();

void put(ItemRecord record, [ItemRecord insertBefore = null]) {
map.putIfAbsent(record.item, () => new _DuplicateItemRecordList()).add(record, insertBefore);
var key = record.item;
_DuplicateItemRecordList duplicates = map[key];
if (duplicates == null) {
duplicates = map[key] = new _DuplicateItemRecordList();
}
duplicates.add(record, insertBefore);
}

/**
Expand Down
37 changes: 24 additions & 13 deletions lib/change_detection/watch_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ class WatchGroup implements _EvalWatchList, _WatchGroupList {
}

Watch watch(AST expression, ReactionFn reactionFn) {
WatchRecord<_Handler> watchRecord =
_cache.putIfAbsent(expression.expression,
() => expression.setupWatch(this));
WatchRecord<_Handler> watchRecord = _cache[expression.expression];
if (watchRecord == null) {
_cache[expression.expression] = watchRecord = expression.setupWatch(this);
}
return watchRecord.handler.addReactionFn(reactionFn);
}

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

WatchRecord<_Handler> lhsWR = _cache.putIfAbsent(lhs.expression,
() => lhs.setupWatch(this));
WatchRecord<_Handler> lhsWR = _cache[lhs.expression];
if (lhsWR == null) {
lhsWR = _cache[lhs.expression] = lhs.setupWatch(this);
}

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

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

if (lhsAST != null) {
var lhsWR = _cache.putIfAbsent(lhsAST.expression,
() => lhsAST.setupWatch(this));
var lhsWR = _cache[lhsAST.expression];
if (lhsWR == null) {
lhsWR = _cache[lhsAST.expression] = lhsAST.setupWatch(this);
}
lhsWR.handler.addForwardHandler(invokeHandler);
invokeHandler.acceptValue(lhsWR.currentValue);
}

// Convert the args from AST to WatchRecords
for (var i = 0; i < argsAST.length; i++) {
var ast = argsAST[i];
WatchRecord<_Handler> record =
_cache.putIfAbsent(ast.expression, () => ast.setupWatch(this));
WatchRecord<_Handler> record = _cache[ast.expression];
if (record == null) {
record = _cache[ast.expression] = ast.setupWatch(this);
}
_ArgHandler handler = new _PositionalArgHandler(this, evalWatchRecord, i);
_ArgHandlerList._add(invokeHandler, handler);
record.handler.addForwardHandler(handler);
handler.acceptValue(record.currentValue);
}

namedArgsAST.forEach((Symbol name, AST ast) {
WatchRecord<_Handler> record = _cache.putIfAbsent(ast.expression,
() => ast.setupWatch(this));
WatchRecord<_Handler> record = _cache[ast.expression];
if (record == null) {
record = _cache[ast.expression] = ast.setupWatch(this);
}
_ArgHandler handler = new _NamedArgHandler(this, evalWatchRecord, name);
_ArgHandlerList._add(invokeHandler, handler);
record.handler.addForwardHandler(handler);
Expand Down
16 changes: 8 additions & 8 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class ElementBinder {

void _createAttrMappings(directive, scope, List<MappingParts> mappings, nodeAttrs, tasks) {
Scope directiveScope; // Only created if there is a two-way binding in the element.
mappings.forEach((MappingParts p) {
for(var i = 0; i < mappings.length; i++) {
MappingParts p = mappings[i];
var attrName = p.attrName;
var attrValueAST = p.attrValueAST;
AST dstAST = p.dstAST;
Expand All @@ -142,7 +143,7 @@ class ElementBinder {
} else {
_bindOneWay(tasks, bindAttr, scope, dstAST, directive);
}
return;
continue;
}

switch (p.mode) {
Expand All @@ -155,7 +156,7 @@ class ElementBinder {
break;

case '<=>': // two-way
if (nodeAttrs[attrName] == null) return;
if (nodeAttrs[attrName] == null) continue;
if (directiveScope == null) {
directiveScope = scope.createChild(directive);
}
Expand All @@ -164,13 +165,12 @@ class ElementBinder {
break;

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

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

var watch;
var lastOneTimeValue;
Expand All @@ -193,7 +193,7 @@ class ElementBinder {
_bindCallback(dstAST.parsedExp, directive, nodeAttrs[attrName], scope);
break;
}
});
}
}

void _link(nodeInjector, probe, scope, nodeAttrs) {
Expand Down

0 comments on commit 57da29d

Please sign in to comment.