Skip to content

[BUG] Replacing pattern-matching-id components is O(new × old): computePaths deep-compares each new id with every old one #4192

Backlog

Description

@biyani701

Thank you so much for helping improve the quality of Dash!

We do our best to catch bugs during the release process, but we rely on your help to find the ones that slip through.

Describe your context

  • dash 4.5.1 (also on dev as of 2026-11-25), React 19, Chromium/Edge.
  • A page whose callback replaces a list of components that carry dict (pattern-matching) ids with a different list
    (for example, a grid of cells re-rendered for another team).

Describe the bug

Replacing N components that have pattern-matching ids with N different ones takes time proportional to N², while
the first render of the same N is linear. The time is spent in computePaths (dash-renderer/src/actions/paths.js):

const index = indexOf(item, oldie);
if (index === -1) {
    paths.push(item);
} else {
    objs[keyStr] = insert(index, item, paths);
}

For every new component with a dict id, R.indexOf scans oldie (every previous id with the same key set) with
R.equals on {values, path}. When nothing matches (new ids), each new id is compared with all old ones. When matches
are found, insert copies the array each time. #3954 made getPath O(1) with objIndex, but this scan in the crawl
remains.

Minimal reproduction

from dash import Dash, html, Input, Output, ALL

app = Dash(__name__)
app.layout = html.Div([html.Button("render", id="go"), html.Div(id="out"), html.Div(id="dummy")])

N = 2000

@app.callback(Output("out", "children"), Input("go", "n_clicks"), prevent_initial_call=True)
def render(n):
    return [html.Div(f"item {n}-{i}", id={"type": "cell", "batch": n, "i": i}) for i in range(N)]

@app.callback(Output("dummy", "children"),
              Input({"type": "cell", "batch": ALL, "i": ALL}, "n_clicks"), prevent_initial_call=True)
def clicked(_):
    return ""

if __name__ == "__main__":
    app.run()

Click "render" once (first render), then again (replace). Measured with Playwright, headless Chromium, until the new
first item is shown and the main thread is idle:

N First render Replace Replace again
500 0.27 s 1.46 s 1.35 s
1,000 0.45 s 5.44 s 4.97 s
2,000 1.06 s 19.3 s 18.7 s

In a real app (about 2,800 pattern-matching ids on the page, ~2,300 of one shape replaced by a team switch), a CPU
profile of the switch shows 43 of 54 s in computePaths → crawlLayout, with 37 s in indexOf → _equals.
Replacing that one indexOf with a hash lookup (below) took the same switch from 69 s to 25 s, and the longest main
thread block from 48 s to 8.9 s.

Expected behavior

Replacing N pattern-matching components costs about the same as rendering N for the first time.

Suggested fix

Look the item up in a map instead of scanning: build, once per keyStr, a Map from a key of {values, path} (for
example JSON.stringify([values, path]), matching valuesKey) to its index in oldie, and use it in place of
indexOf(item, oldie). Also consider replacing insert(index, item, paths) with a push, so a match doesn't copy the
array each time (and doesn't insert at an index that belongs to the old list).

This is the second quadratic path we've hit on the same page after #4121/#4124 (fixed in 4.5.1, thank you).

Activity

  1. biyani701 commented on Nov 25, 2026

    @biyani701
    Author

    Tested a fix locally on 4.5.1. Fixing the indexOf in computePaths alone is not enough: the same replace then hits
    a second O(removed × current) loop, in getUnfilteredLayoutCallbacks → addCallbackIfArray (actions/dependencies_ts
    in the bundle). For every removed component that was an input of an ALL callback, it calls cb.getInputs(paths),
    which resolves the callback's whole ALL input list, and stringifies every id in it to compare with the removed id.

    With both changed, replacing N components costs the same as rendering them the first time (repro from the issue,
    headless Chromium):

    N First render Replace, 4.5.1 Replace, computePaths fixed Replace, both fixed
    1,000 0.5 s 6.3 s 1.3 s 0.5 s
    2,000 1.3 s 23 s 5.7 s 1.1 s
    4,000 3.7 s — — 3.7 s

    @@APP_NUMBERS@@

    The two changes, as tested (written against the built bundle; the source equivalents are small):

    1. computePaths: look the old entry up instead of scanning, e.g. a Map from JSON.stringify(entry) to its index in
      oldie, built once per key set:
      const oldIndex = oldIndexByKey[keyStr] ||= new Map(oldie.map((e, i) => [JSON.stringify(e), i]));
      const index = oldIndex.get(JSON.stringify(item)) ?? -1;
    2. addCallbackIfArray: compute the set of stringified ids in a callback's array inputs once per callback (per
      paths), then test membership:
      let ids = arrayInputIds[cb.resolvedId];
      if (!ids) {
          ids = new Set();
          cb.getInputs(paths).forEach(ini => Array.isArray(ini) && ini.forEach(inij => ids.add(stringifyId(inij.id))));
          arrayInputIds[cb.resolvedId] = ids;
      }
      if (!ids.has(idStr)) return false;
      // ...unchanged: add the callback with initialCall when it still has outputs
      return true;
      (arrayInputIds scoped to one getUnfilteredLayoutCallbacks call.)

    Happy to open a PR with these and the benchmark if that helps.

  2. added
    bug
    performance
    dash-renderer
    dash-callbacks
    P2
    sev-2
    on Nov 25, 2026
  3. self-assigned this
    on Nov 25, 2026
  4. added theissue type on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2bugdash-callbacksdash-rendererperformancesev-2size: 3

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions