From 1ed9cd148d944e7fef5f85e629c24b395cda0744 Mon Sep 17 00:00:00 2001 From: Favour Ohanekwu Date: Thu, 17 Aug 2023 06:16:02 +0100 Subject: [PATCH] fix: improve performance of updateDependency flow (#26391) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Cause of issue Previously, dependencyMap.addNode() had a O(n^2) complexity. #### PR fixes following issue(s) Fixes https://github.com/appsmithorg/appsmith/issues/26400 #### Media > A video or a GIF is preferred. when using Loom, don’t embed because it looks like it’s a GIF. instead, just link to the video > > #### Type of change > Please delete options that are not relevant. - Bug fix (non-breaking change which fixes an issue) - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) - Chore (housekeeping or task changes that don't impact user perception) - This change requires a documentation update > > > ## Testing > #### How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Also list any relevant details for your test configuration. > Delete anything that is not relevant - [ ] Manual - [ ] Jest - [ ] Cypress > > #### Test Plan > Add Testsmith test cases links that relate to this PR > > #### Issues raised during DP testing > Link issues raised during DP testing for better visiblity and tracking (copy link from comments dropped on this PR) > > > ## Checklist: #### Dev activity - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag #### QA activity: - [ ] [Speedbreak features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-) have been covered - [ ] Test plan covers all impacted features and [areas of interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-) - [ ] Test plan has been peer reviewed by project stakeholders and other QA members - [ ] Manually tested functionality on DP - [ ] We had an implementation alignment call with stakeholders post QA Round 2 - [ ] Cypress test cases have been added and approved by SDET/manual QA - [ ] Added `Test Plan Approved` label after Cypress tests were reviewed - [ ] Added `Test Plan Approved` label after JUnit tests were reviewed --- app/client/package.json | 1 + app/client/src/entities/DependencyMap/index.ts | 18 ++++++++---------- app/client/yarn.lock | 8 ++++++++ 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/client/package.json b/app/client/package.json index cf49dc7cc1..d0dd0c0896 100644 --- a/app/client/package.json +++ b/app/client/package.json @@ -98,6 +98,7 @@ "downloadjs": "^1.4.7", "echarts": "^5.4.2", "fast-deep-equal": "^3.1.3", + "fast-sort": "^3.4.0", "fast-xml-parser": "^3.17.5", "fastdom": "^1.0.11", "focus-trap-react": "^8.9.2", diff --git a/app/client/src/entities/DependencyMap/index.ts b/app/client/src/entities/DependencyMap/index.ts index ee500789e4..cd6363bba7 100644 --- a/app/client/src/entities/DependencyMap/index.ts +++ b/app/client/src/entities/DependencyMap/index.ts @@ -1,5 +1,6 @@ -import { difference, some } from "lodash"; +import { difference } from "lodash"; import { isChildPropertyPath } from "utils/DynamicBindingUtils"; +import { sort } from "fast-sort"; export type TDependencies = Map>; export default class DependencyMap { @@ -150,7 +151,10 @@ export default class DependencyMap { */ addNodes = (nodes: Record, strict = true) => { - const nodesToAdd = Object.keys(nodes); + const nodesToAdd = strict + ? Object.keys(nodes) + : sort(Object.keys(nodes)).desc((node) => node.split(".").length); + let didUpdateGraph = false; for (const newNode of nodesToAdd) { if (this.#nodes.has(newNode)) continue; @@ -168,14 +172,8 @@ export default class DependencyMap { for (const [invalidNode, dependants] of this .#invalidDependenciesInverse) { if ( - !nodesToAdd.includes(invalidNode) && - isChildPropertyPath(newNode, invalidNode, true) && - !some( - nodesToAdd, - (node) => - isChildPropertyPath(newNode, node, true) && - isChildPropertyPath(node, invalidNode, true), - ) + !nodes.hasOwnProperty(invalidNode) && + isChildPropertyPath(newNode, invalidNode, true) ) { dependants.forEach((dependant) => { nodesThatAlreadyDependedOnThis.add(dependant); diff --git a/app/client/yarn.lock b/app/client/yarn.lock index 4199b9cb70..c351e27e8f 100644 --- a/app/client/yarn.lock +++ b/app/client/yarn.lock @@ -9458,6 +9458,7 @@ __metadata: esm: ^3.2.25 factory.ts: ^0.5.1 fast-deep-equal: ^3.1.3 + fast-sort: ^3.4.0 fast-xml-parser: ^3.17.5 fastdom: ^1.0.11 focus-trap-react: ^8.9.2 @@ -15217,6 +15218,13 @@ __metadata: languageName: node linkType: hard +"fast-sort@npm:^3.4.0": + version: 3.4.0 + resolution: "fast-sort@npm:3.4.0" + checksum: b925d68438629b7176dc52c6f5bb88162675e88286b6e06041c6fb1095ec44af6aac3edd5b7e09ada044b90bced078a013683d2d75337ae6f5e7224f7e073019 + languageName: node + linkType: hard + "fast-xml-parser@npm:^3.17.5": version: 3.17.5 resolution: "fast-xml-parser@npm:3.17.5"