Improve performance of pagerank decompose (#1007)

When I implemented this function, I incorrectly assumed that
`lodash.sortBy` only calls subsequent accessor functions if there is a
tie from the first accessor. Actually, it calls it every time. We can
avoid lots of wasteful JSON.serialization by just grabbing the exact
properties of interest.

Test plan:

For correctness: `yarn test` suffices, as this functionality is already
tested.

For performance improvement: I ran the full load+analyze workflow, in
Chrome, on twbs/bootstrap. Before this change, decompose took 6.9s;
after this change, it takes 1.3s, for a 5.3x speedup.

Close #943.
This commit is contained in:
Dandelion Mané 2018-11-16 22:26:55 -08:00 committed by GitHub
parent 80b458d719
commit 794b93e397
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,8 +44,19 @@ export function decompose(
} }
), ),
(x) => -x.connectionScore, (x) => -x.connectionScore,
// The following should be called rarely and on small objects. (x) => x.connection.adjacency.type,
(x) => JSON.stringify(x.connection.adjacency) (x) => {
switch (x.connection.adjacency.type) {
case "IN_EDGE":
return x.connection.adjacency.edge.address;
case "OUT_EDGE":
return x.connection.adjacency.edge.address;
case "SYNTHETIC_LOOP":
return "";
default:
throw new Error((x.connection.adjacency.type: empty));
}
}
); );
return {score, scoredConnections}; return {score, scoredConnections};
}); });