Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PERF] optimise notifications when has-many array is changed #4583

Closed
wants to merge 16 commits 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
60 changes: 60 additions & 0 deletions addon/-private/system/diff-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
@namespace
@method diff-array
@for DS
@param {Array} oldArray the old array
@param {Array} newArray the new array
@return {hash} {
firstChangeIndex: <integer>, // null if no change
addedCount: <integer>, // 0 if no change
removedCount: <integer> // 0 if no change
}
*/
const diffArray = function (oldArray, newArray) {
const oldLength = oldArray.length;
const newLength = newArray.length;

const shortestLength = Math.min(oldLength, newLength);
let firstChangeIndex = null; // null signifies no changes

// find the first change
for (let i=0; i<shortestLength; i++) {
// compare each item in the array
if (oldArray[i] !== newArray[i]) {
firstChangeIndex = i;
break;
}
}

if (firstChangeIndex === null && newLength !== oldLength) {
// no change found in the overlapping block
// and array lengths differ,
// so change starts at end of overlap
firstChangeIndex = shortestLength;
}

let addedCount = 0;
let removedCount = 0;
if (firstChangeIndex !== null) {
// we found a change, find the end of the change
let unchangedEndBlockLength = shortestLength - firstChangeIndex;
// walk back from the end of both arrays until we find a change
for (let i=1; i<=shortestLength; i++) {
// compare each item in the array
if (oldArray[oldLength-i] !== newArray[newLength-i]) {
unchangedEndBlockLength = i-1;
break;
}
}
addedCount = newLength - unchangedEndBlockLength - firstChangeIndex;
removedCount = oldLength - unchangedEndBlockLength - firstChangeIndex;
}

return {
firstChangeIndex,
addedCount,
removedCount
};
}

export default diffArray;
39 changes: 22 additions & 17 deletions addon/-private/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*/
import Ember from 'ember';
import { assert } from "ember-data/-private/debug";
import { PromiseArray } from "ember-data/-private/system/promise-proxies";
import { _objectIsAlive } from "ember-data/-private/system/store/common";
import { PromiseArray } from "./promise-proxies";
import { _objectIsAlive } from "./store/common";
import diffArray from './diff-array';

const { get, set } = Ember;

Expand Down Expand Up @@ -146,26 +147,31 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {

//a hack for not removing new records
//TODO remove once we have proper diffing
let newRecords = this.currentState.filter(
const newRecords = this.currentState.filter(
// only add new records which are not yet in the canonical state of this
// relationship (a new record can be in the canonical state if it has
// been 'acknowleged' to be in the relationship via a store.push)
(internalModel) => internalModel.isNew() && toSet.indexOf(internalModel) === -1
);
toSet = toSet.concat(newRecords);
let oldLength = this.length;
this.arrayContentWillChange(0, this.length, toSet.length);
// It’s possible the parent side of the relationship may have been unloaded by this point
if (_objectIsAlive(this)) {
this.set('length', toSet.length);
}
this.currentState = toSet;
this.arrayContentDidChange(0, oldLength, this.length);

if (isInitialized) {
//TODO Figure out to notify only on additions and maybe only if unloaded
// diff to find changes
const diff = diffArray(this.currentState, toSet);

if (diff.firstChangeIndex !== null) { // it's null if no change found
// we found a change
this.arrayContentWillChange(diff.firstChangeIndex, diff.removedCount, diff.addedCount);
// It’s possible the parent side of the relationship may have been unloaded by this point
if (_objectIsAlive(this)) {
this.set('length', toSet.length);
}
this.currentState = toSet;
this.arrayContentDidChange(diff.firstChangeIndex, diff.removedCount, diff.addedCount);
this.relationship.notifyHasManyChanged();
}
if (isInitialized) {
this.record.updateRecordArrays();
}
},

internalReplace(idx, amt, objects) {
Expand Down Expand Up @@ -290,12 +296,11 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
@return {DS.Model} record
*/
createRecord(hash) {
let store = get(this, 'store');
let type = get(this, 'type');
let record;
const store = get(this, 'store');
const type = get(this, 'type');

assert(`You cannot add '${type.modelName}' records to this polymorphic relationship.`, !get(this, 'isPolymorphic'));
record = store.createRecord(type.modelName, hash);
const record = store.createRecord(type.modelName, hash);
this.pushObject(record);

return record;
Expand Down
Loading