Opened 9 minutes ago

#37546 new Cleanup/optimization

Avoid repeated list scans when sorting serialization dependencies

Reported by: ChuaYuJien Owned by:
Component: Core (Serialization) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When dumpdata --natural-foreign runs on a project with many models, django.core.serializers.sort_dependencies() checks whether each dependency has already been sorted by scanning the growing output list. On a chain in which each model references the previous model, that makes the sorting work grow quadratically even when the input is already in dependency order.

The sorter already builds a set of the input models. Removing each model from that set when it is added to the output makes dependency membership checks constant time on average while preserving iteration order and the current cycle fallback.

On CPython 3.14.10 on Windows, the median sorting time for 1,000 linked Django models dropped from 9.562 ms to 2.297 ms. This benchmark measured dependency sorting alone. The reproduction script, regression test, and test results are in ​draft PR #22360.

OpenAI Codex (GPT-6) helped identify the bottleneck, prepare the patch and test, run the benchmark, and draft this report.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top