Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

fix(vscode): Adding a step in Kaoto VSCode takes several seconds #1338

Merged
merged 1 commit into from
Mar 3, 2023
Merged

fix(vscode): Adding a step in Kaoto VSCode takes several seconds #1338

merged 1 commit into from
Mar 3, 2023

Conversation

lordrip
Copy link
Collaborator

@lordrip lordrip commented Feb 28, 2023

Description

Currently, adding a new step from the VSCode extension takes more than 12 seconds.

This is because, in VSCode, the MiniCatalog component gets rerendered many times blocking the UI thread.

Examples

  • KaotoUI - Firefox Developer edition
    Screenshot from 2023-02-28 15-02-26

  • KaotoUI - VSCode
    Screenshot from 2023-02-28 15-04-50

Notice how in VSCode there are many more render passes hence blocking the UI thread.

Part of the solution is to avoid recreating functions inside of components that later on are passed to child components. A more robust solution would include removing many other points that trigger extra renders.

Changes

  • Move the onNodeClick function inside of a useCallback() hook
  • Given that stepsService it's a dependency of onNodeClick, it needed to be moved inside a useMemo() hook.
  • Remove setReactFlowInstance since seems not used
  • Remove reactFlowWrapper since is not linked with anything
  • Move inline styles to a dedicated CSS class

Fixes KaotoIO/vscode-kaoto#132

@lordrip lordrip requested a review from a team February 28, 2023 16:35
@@ -25,8 +25,6 @@ const Visualization = () => {
zoom: 1.2,
};
const [isPanelExpanded, setIsPanelExpanded] = useState(false);
const [, setReactFlowInstance] = useState(null);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kahboom is this needed? I think that it forces a render but I don't know if there another use case for it 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confession time--I have no idea. I think that might have been from an older version of React Flow, but I can't be sure. 😅 We can remove it, just be sure to check rendering still happens as needed with the basic actions (for example replacing from big catalog, deleting steps, editing config, etc.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm, I think that you're right, I checked here and it seems that it's also deprecated.

>
<div
className="reactflow-wrapper"
data-testid={'react-flow-wrapper'}
ref={reactFlowWrapper}
style={{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the corresponding Visualization.css file

@codecov
Copy link

codecov bot commented Feb 28, 2023

Codecov Report

Merging #1338 (30c4427) into main (2ae665e) will decrease coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1338      +/-   ##
==========================================
- Coverage   52.93%   52.88%   -0.05%     
==========================================
  Files          56       56              
  Lines        1910     1908       -2     
  Branches      435      435              
==========================================
- Hits         1011     1009       -2     
  Misses        851      851              
  Partials       48       48              
Impacted Files Coverage Δ
src/components/MiniCatalog.tsx 56.14% <ø> (ø)
src/components/Visualization.tsx 66.66% <100.00%> (ø)
src/services/stepsService.ts 32.72% <0.00%> (-0.15%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

igarashitm
igarashitm previously approved these changes Feb 28, 2023
@@ -40,7 +38,10 @@ const Visualization = () => {
const previousLayout = useRef(visualizationStore.layout);
const previousIntegrationJson = useRef(integrationJsonStore.integrationJson);
const visualizationService = new VisualizationService(integrationJsonStore, visualizationStore);
const stepsService = new StepsService(integrationJsonStore, nestedStepsStore, visualizationStore);
const stepsService = useMemo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 just curious, should useMemo() be the default way of instantiating (and caching) service instances? a "memo" for #1233

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very interesting question 😃

From my point of view, I don't think that this is the definite way to go but a transition point.

At first, the onNodeClick function was being recreated on every render pass, together with the stepService, so as I wrapped onNodeClick function into a useCallback() hook to avoid recreating it, stepService now it's a required dependency of it, so I wrapped the service with useMemo() to recreate it only if something change.

I think that a more appropriate fix would be to have this service as a context, this way we wouldn't need to create the service in different places but just call the useContext() hook where needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha! makes sense, thanks 👍

Copy link
Contributor

@apupier apupier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great in VS Code. Not detected issues. I really do not know the potential of the code changes so I only provide a comment not approve.

kahboom
kahboom previously approved these changes Mar 2, 2023
Copy link
Contributor

@kahboom kahboom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lordrip It looks good to me. Did some basic testing locally. Will wait on you before merging, since I'm not sure if you still wanted to remove the setReactFlowInstance code, particularly if it is causing an extra render we don't need.

@lordrip
Copy link
Collaborator Author

lordrip commented Mar 3, 2023

@lordrip It looks good to me. Did some basic testing locally. Will wait on you before merging, since I'm not sure if you still wanted to remove the setReactFlowInstance code, particularly if it is causing an extra render we don't need.

Yes, it seems that onLoad is deprecated and renamed to onInit instead, and seems that we're not using it either.

Currently, adding a new step from the `VSCode` extension takes more than 12 seconds.

This is because, in `VSCode`, the `MiniCatalog` component gets rerendered many times
blocking the UI thread.

Part of the solution is to avoid recreating functions inside of components that
later on are passed to child components. A more robust solution would include
removing many other points that trigger extra renders.

Changes
* Move the `onNodeClick` function inside of a `useCallback()` hook
* Given that stepsService it's a dependency of `onNodeClick`, it needed to be moved inside a `useMemo()` hook.
* Remove `setReactFlowInstance` since seems not used
* Remove `reactFlowWrapper` since is not linked with anything
* Move inline styles to a dedicated CSS class

Fixes KaotoIO/vscode-kaoto#132
@lordrip lordrip dismissed stale reviews from kahboom and igarashitm via 30c4427 March 3, 2023 14:04
@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 3, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@lordrip lordrip merged commit 68077c3 into kaoto-archive:main Mar 3, 2023
@lordrip lordrip deleted the fix/add-step-in-vscode-slow branch May 6, 2023 17:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding a step can take more than 12 seconds
4 participants