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

feat(change_detection): treat List properties as pure #763

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/change_detection/change_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ abstract class ChangeDetector<H> extends ChangeDetectorGroup<H> {
* same order as they were registered.
*/
Iterator<Record<H>> collectChanges({ EvalExceptionHandler exceptionHandler,
AvgStopwatch stopwatch });
AvgStopwatch stopwatch,
bool reMemoizeForPureFunc });
}

abstract class Record<H> {
Expand Down
6 changes: 5 additions & 1 deletion lib/change_detection/dirty_checking_change_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,14 @@ class DirtyCheckingChangeDetector<H> extends DirtyCheckingChangeDetectorGroup<H>
}

Iterator<Record<H>> collectChanges({EvalExceptionHandler exceptionHandler,
AvgStopwatch stopwatch}) {
AvgStopwatch stopwatch, bool reMemoizeForPureFunc}) {
if (stopwatch != null) stopwatch.start();
DirtyCheckingRecord changeTail = _fakeHead;
DirtyCheckingRecord current = _recordHead; // current index

int count = 0;
while (current != null) {
if (reMemoizeForPureFunc == null || reMemoizeForPureFunc) current.dirtyArgs = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line will significantly impact the dirty checking speed. This is the core loop and it has to have minimal set of property accesses.

try {
if (current.check()) changeTail = changeTail._nextChange = current;
count++;
Expand Down Expand Up @@ -373,6 +374,7 @@ class DirtyCheckingRecord<H> implements Record<H>, WatchRecord<H> {
Record<H> _nextChange;
var _object;
InstanceMirror _instanceMirror;
bool dirtyArgs = true;

DirtyCheckingRecord(this._group, object, fieldName, this._getter, this.handler)
: field = fieldName,
Expand Down Expand Up @@ -443,7 +445,9 @@ class DirtyCheckingRecord<H> implements Record<H>, WatchRecord<H> {
case _MODE_MARKER_:
return false;
case _MODE_REFLECT_:
if (!dirtyArgs) return false;
current = _instanceMirror.getField(_symbol).reflectee;
if (object is List) dirtyArgs = false;
break;
case _MODE_GETTER_:
current = _getter(object);
Expand Down
5 changes: 3 additions & 2 deletions lib/change_detection/watch_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,13 @@ class RootWatchGroup extends WatchGroup {
ChangeLog changeLog,
AvgStopwatch fieldStopwatch,
AvgStopwatch evalStopwatch,
AvgStopwatch processStopwatch}) {
AvgStopwatch processStopwatch, bool reMemoizeForPureFunc : true}) {
// Process the Records from the change detector
Iterator<Record<_Handler>> changedRecordIterator =
(_changeDetector as ChangeDetector<_Handler>).collectChanges(
exceptionHandler:exceptionHandler,
stopwatch: fieldStopwatch);
stopwatch: fieldStopwatch,
reMemoizeForPureFunc: reMemoizeForPureFunc);
if (processStopwatch != null) processStopwatch.start();
while (changedRecordIterator.moveNext()) {
var record = changedRecordIterator.current;
Expand Down
3 changes: 2 additions & 1 deletion lib/core/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ class RootScope extends Scope {
changeLog: changeLog,
fieldStopwatch: _scopeStats.digestFieldStopwatch,
evalStopwatch: _scopeStats.digestEvalStopwatch,
processStopwatch: _scopeStats.digestProcessStopwatch);
processStopwatch: _scopeStats.digestProcessStopwatch,
reMemoizeForPureFunc: count == null);

if (digestTTL <= LOG_COUNT) {
if (changeLog == null) {
Expand Down
13 changes: 13 additions & 0 deletions test/core/scope_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ void main() {
rootScope.digest();
expect(logger).toEqual([true]);
});

it('should watch list property as pure', (Logger logger, Map context, RootScope rootScope) {
var list = [true, 2, 'abc'];
final logVal = (value, _) => logger(value);
context['list'] = list;
rootScope.watch('list.reversed', logVal);
rootScope.digest();
expect(logger).toEqual([['abc', 2, true]]);
logger.clear();
context['list'][2] = 'def';
rootScope.digest();
expect(logger).toEqual([['def', 2, true]]);
});

it('should support filters', (Logger logger, Map context,
RootScope rootScope, AstParser parser,
Expand Down