Skip to content

Commit

Permalink
Merge pull request #4661 from runspired/perf-peek-all
Browse files Browse the repository at this point in the history
dirty check to prevent liveRecordArrays being rebuilt too often
  • Loading branch information
bmac authored Nov 16, 2016
2 parents 758ebe7 + 06c216d commit c16ae77
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
38 changes: 28 additions & 10 deletions addon/-private/system/record-array-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default Ember.Object.extend({
});

this.liveRecordArrays = MapWithDefault.create({
defaultValue: typeClass => this.createRecordArray(typeClass)
defaultValue: modelClass => this.createRecordArray(modelClass)
});

this.changedRecords = [];
Expand Down Expand Up @@ -187,9 +187,27 @@ export default Ember.Object.extend({
}
},

populateLiveRecordArray(array, modelName) {
syncLiveRecordArray(array, modelClass) {
let hasNoPotentialDeletions = this.changedRecords.length === 0;
let typeMap = this.store.typeMapFor(modelClass);
let hasNoInsertionsOrRemovals = typeMap.records.length === array.length;

/*
Ideally the recordArrayManager has knowledge of the changes to be applied to
liveRecordArrays, and is capable of strategically flushing those changes and applying
small diffs if desired. However, until we've refactored recordArrayManager, this dirty
check prevents us from unnecessarily wiping out live record arrays returned by peekAll.
*/
if (hasNoPotentialDeletions && hasNoInsertionsOrRemovals) {
return;
}

this.populateLiveRecordArray(array, modelClass);
},

populateLiveRecordArray(array, modelClass) {
heimdall.increment(populateLiveRecordArray);
let typeMap = this.store.typeMapFor(modelName);
let typeMap = this.store.typeMapFor(modelClass);
let records = typeMap.records;
let record;

Expand All @@ -211,20 +229,20 @@ export default Ember.Object.extend({
@method updateFilter
@param {Array} array
@param {String} modelName
@param {Class} modelClass
@param {Function} filter
*/
updateFilter(array, modelName, filter) {
updateFilter(array, modelClass, filter) {
heimdall.increment(updateFilter);
let typeMap = this.store.typeMapFor(modelName);
let typeMap = this.store.typeMapFor(modelClass);
let records = typeMap.records;
let record;

for (let i = 0; i < records.length; i++) {
record = records[i];

if (!record.isDeleted() && !record.isEmpty()) {
this.updateFilterRecordArray(array, filter, modelName, record);
this.updateFilterRecordArray(array, filter, modelClass, record);
}
}
},
Expand All @@ -246,13 +264,13 @@ export default Ember.Object.extend({
Create a `DS.RecordArray` for a type.
@method createRecordArray
@param {Class} typeClass
@param {Class} modelClass
@return {DS.RecordArray}
*/
createRecordArray(typeClass) {
createRecordArray(modelClass) {
heimdall.increment(createRecordArray);
return RecordArray.create({
type: typeClass,
type: modelClass,
content: Ember.A(),
store: this.store,
isLoaded: true,
Expand Down
6 changes: 3 additions & 3 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1596,10 +1596,10 @@ Store = Service.extend({
heimdall.increment(peekAll);
assert("You need to pass a model name to the store's peekAll method", isPresent(modelName));
assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ Ember.inspect(modelName), typeof modelName === 'string');
var typeClass = this.modelFor(modelName);
let modelClass = this.modelFor(modelName);
let liveRecordArray = this.recordArrayManager.liveRecordArrayFor(modelClass);

var liveRecordArray = this.recordArrayManager.liveRecordArrayFor(typeClass);
this.recordArrayManager.populateLiveRecordArray(liveRecordArray, typeClass);
this.recordArrayManager.syncLiveRecordArray(liveRecordArray, modelClass);

return liveRecordArray;
},
Expand Down

0 comments on commit c16ae77

Please sign in to comment.