Repository navigation
perf(persistence): speed up deepCompare for json and array columns - #13000
ElwinErnst wants to merge 1 commit into
Conversation
Code Review by Qodo
1.
|
da9a5f4 to
2ab1dc8
Compare
OrmUtils.deepCompare runs on every save() for each json/jsonb and array column to detect changes. On unchanged values (the common case when saving a loaded entity) it did two full passes over every object's keys and ran per-key hasOwnProperty/typeof checks twice, making it ~8-14x slower than a straightforward deep-equal. Rewrite compare2Objects to a single pass over the keys with a lighter second pass that only rejects keys unique to the right object, plus a positional fast path for arrays. Element-level semantics (.equals, Uint8Array, Map/Set, Date/RegExp, NaN, cycle detection) are preserved exactly, verified by a 200k-case differential fuzz against the previous implementation. Measured on the issue's benchmark (Node 24, array of N objects): 20000 objects 63.8ms -> 7.7ms (~8.3x). Closes typeorm#12995
2ab1dc8 to
2153c11
Compare
|
Thanks for the automated review — I went through the array fast-path findings: Array-like object slipping through ( Holes, non-index enumerable properties, sparse-array iteration, and getter read order: these are left as deliberate 200k-case differential fuzz against the previous implementation still reports 0 divergences, and the unit suite passes. |
commit: |
Description of change
OrmUtils.deepCompare(compare2Objects) runs on everysave()for eachjson/jsonbcolumn and each array column, viaSubjectChangedColumnsComputer, to decide whether the column changed. On unchanged values — the common case when saving a loaded entity — it traverses the whole value, and it did so ~8–14x slower than a straightforward deep-equal (see #12995 for the benchmark and analysis).What changed
compare2Objectsis rewritten to:hasOwnPropertyand twotypeofchecks per key), with a lighter second pass that only rejects keys present on the right object but not the left;iterableX[p]/typeoflookups into locals.Semantics are preserved exactly —
.equals()(Buffer, #3654),Uint8Array,Map/Set,Date/RegExp, theNaNhandling for primitive members, and cycle detection all behave as before.How it was verified
Map/Set,Date/RegExp,NaN,null/undefined, near-equal clones): 0 divergences.test/unit/util/orm-utils.test.ts(arrays, nested, extra keys,Date/RegExp, cycles, null/undefined).Pull-Request Checklist
masterbranchFixes #00000tests/**.test.ts)docs/docs/**.md) — N/A (internal behavior, no public API change)Fixes #12995