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

Performance improvement on (pattern-matching) callbacks #3009

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion dash/dash-renderer/src/actions/dependencies_ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,27 @@ export const getReadyCallbacks = (
}
}

// Ramda.JS `difference` function is slow because it compares objects entirely
// This cause the following `filter` to be exponentially slow as the number of inputs or outputs grow
// We can optimize this by comparing only the `id+prop` part of the inputs & outputs.
// Original difference takes 380ms on average to compute difference between 200 inputs and 1 output.
// The following function takes 1-2ms on average.
const differenceBasedOnId = (inputs: any[], outputs: any[]): any[] =>
inputs.filter(
input =>
!outputs.some(
output =>
combineIdAndProp(input) === combineIdAndProp(output)
)
);

// Find `requested` callbacks that do not depend on a outstanding output (as either input or state)
// Outputs which overlap an input do not count as an outstanding output
return filter(
cb =>
all<ILayoutCallbackProperty>(
cbp => !outputsMap[combineIdAndProp(cbp)],
difference(
differenceBasedOnId(
flatten(cb.getInputs(paths)),
flatten(cb.getOutputs(paths))
)
Expand Down
19 changes: 11 additions & 8 deletions dash/dash-renderer/src/observers/prioritizedCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {find, flatten, map, partition, pluck, sort, uniq} from 'ramda';
import {find, flatten, map, partition, sort} from 'ramda';

import {IStoreState} from '../store';

Expand Down Expand Up @@ -53,13 +53,16 @@ const getStash = (
return {allOutputs, allPropIds};
};

const getIds = (cb: ICallback, paths: any) =>
uniq(
pluck('id', [
...flatten(cb.getInputs(paths)),
...flatten(cb.getState(paths))
])
);
const getIds = (cb: ICallback, paths: any) => {
const items = [
...flatten(cb.getInputs(paths)),
...flatten(cb.getState(paths))
];

const uniqueIds = new Map(items.map(item => [stringifyId(item.id), item]));
const uniqueItems = Array.from(uniqueIds.values());
return uniqueItems;
};

const observer: IStoreObserverDefinition<IStoreState> = {
observer: async ({dispatch, getState}) => {
Expand Down