Skip to content

Commit

Permalink
Optimize visiting the relating records (#7096)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieter-v authored Jul 2, 2020
1 parent aa860a4 commit da70e6c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2013,14 +2013,12 @@ module('integration/relationship/belongs_to Belongs-To Relationships', function(

_destroyRelationships() {}

_allRelatedRecordDatas() {}
_allRelatedRecordDatas() {
return [this];
}

_cleanupOrphanedRecordDatas() {}

_directlyRelatedRecordDatas() {
return [];
}

destroy() {
this.isDestroyed = true;
this.storeWrapper.disconnectRecord(this.modelName, this.id, this.clientId);
Expand Down
59 changes: 39 additions & 20 deletions packages/record-data/addon/-private/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,23 +465,44 @@ export default class RecordDataDefault implements RelationshipRecordData {
}

/**
Computes the set of internal models reachable from `this` across exactly one
Iterates over the set of internal models reachable from `this` across exactly one
relationship.
@return {Array} An array containing the internal models that `this` belongs
to or has many.
*/
_directlyRelatedRecordDatas(): RecordData[] {
let array = [];

this._relationships.forEach((name, rel) => {
let members = rel.members.list;
let canonicalMembers = rel.canonicalMembers.list;
array = array.concat(members, canonicalMembers);
});
return array;
}
_directlyRelatedRecordDatasIterable = () => {
const initializedRelationships = this._relationships.initializedRelationships;
const relationships = Object.keys(initializedRelationships).map(key => initializedRelationships[key]);

let i = 0;
let j = 0;
let k = 0;

const findNext = () => {
while (i < relationships.length) {
while (j < 2) {
let members = j === 0 ? relationships[i].members.list : relationships[i].canonicalMembers.list;
while (k < members.length) {
return members[k++];
}
k = 0;
j++;
}
j = 0;
i++;
}
return undefined;
};

return {
iterator() {
return {
next: () => {
const value = findNext();
return { value, done: value === undefined };
},
};
},
};
};

/**
Computes the set of internal models reachable from this internal model.
Expand All @@ -503,11 +524,9 @@ export default class RecordDataDefault implements RelationshipRecordData {
let node = queue.shift() as RecordDataDefault;
array.push(node);

let related = node._directlyRelatedRecordDatas();

for (let i = 0; i < related.length; ++i) {
let recordData = related[i];

const iterator = this._directlyRelatedRecordDatasIterable().iterator();
for (let obj = iterator.next(); !obj.done; obj = iterator.next()) {
const recordData = obj.value;
if (recordData instanceof RecordDataDefault) {
assert('Internal Error: seen a future bfs iteration', recordData._bfsId <= bfsId);
if (recordData._bfsId < bfsId) {
Expand Down

0 comments on commit da70e6c

Please sign in to comment.