Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't render when code ahead #6069

Merged
merged 5 commits into from
Jul 12, 2024
Merged

Conversation

Rheeseyb
Copy link
Contributor

@Rheeseyb Rheeseyb commented Jul 11, 2024

Problem:
When making code changes, we'll cause the canvas to re-render twice: once when the code is changed; and then a second time once the parsed model has caught up.

Fix:
Specifically check if any code is ahead before triggering a canvas render. To do this I used the existing shouldRerender in editor.tsx, but realised that was actually a bit of a misnomer, and was gating logic that it shouldn't be. So I have explicitly used the relevant boolean checks around the individual parts of the code called after a dispatch.

Note that I have completely removed the check around editorDispatchClosingOut. I'm very much certain that we should never have been gating that with a check that something had changed, as the internal logic of that function is already checking that something actually changed before e.g. triggering a save.

My only concern with this was that if you are typing fast enough it would prevent the canvas from rendering until you paused briefly, because we don't change the revisions state following a parser-printer update if the update is stale here:

if (updateIsStale) {
// if the received file is older than the existing, we still allow it to update the other side,
// but we don't bump the revision state.
updatedFile = textFile(
textFileContents(code, updatedContents, existing.fileContents.revisionsState),
existing.lastSavedContents,
isParseSuccess(updatedContents) ? updatedContents : existing.lastParseSuccess,
existing.versionNumber,
)
} else {

However, in practice, even on the hydrogen project this never happened, because the IndexedDB polling part which forms the code-editor-to-utopia communication channel is slower than parsing the code. So I then added a 600ms pause in the parsing to force the code updates to come through faster, which meant updating the canvas only happened when you stopped typing, and even in that forced case it actually didn't feel bad.

Manual Tests:
I hereby swear that:

  • I opened a hydrogen project and it loaded
  • I could navigate to various routes in Preview mode

@Rheeseyb Rheeseyb changed the title Fix/dont render when code ahead (DRAFT) Don't render when code ahead Jul 11, 2024
Copy link
Contributor

github-actions bot commented Jul 11, 2024

Try me

Copy link

relativeci bot commented Jul 11, 2024

#13351 Bundle Size — 62.64MiB (-0.02%).

aa79ac5(current) vs 6339ece master#13350(baseline)

Warning

Bundle contains 70 duplicate packages – View duplicate packages

Bundle metrics  Change 7 changes Regression 1 regression Improvement 1 improvement
                 Current
#13351
     Baseline
#13350
Regression  Initial JS 45.71MiB(~+0.01%) 45.71MiB
No change  Initial CSS 0B 0B
Change  Cache Invalidation 19.24% 21.6%
Change  Chunks 31(+3.33%) 30
Change  Assets 34(+3.03%) 33
Change  Modules 4371(-0.07%) 4374
Improvement  Duplicate Modules 521(-0.57%) 524
Change  Duplicate Code 31.68%(-0.06%) 31.7%
No change  Packages 469 469
No change  Duplicate Packages 70 70
Bundle size by type  Change 2 changes Improvement 2 improvements
                 Current
#13351
     Baseline
#13350
Improvement  JS 62.63MiB (-0.02%) 62.65MiB
Improvement  HTML 11.16KiB (-0.33%) 11.2KiB

Bundle analysis reportBranch fix/dont-render-when-code-aheadProject dashboard

@@ -450,7 +451,12 @@ export class Editor {
const reactRouterErrorPreviouslyLogged = hasReactRouterErrorBeenLogged()

const runDomWalker = shouldRunDOMWalker(dispatchedActions, dispatchResult)
const shouldRerender = !dispatchResult.nothingChanged || runDomWalker
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed that shouldRerender was actually very badly named here, and so changed some of the gating of the code below accordingly. It's worth expanding the diff below to understand what is actually happening now

@@ -467,89 +473,90 @@ export class Editor {
})
})
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

:narrows_eyes: the original code assumes that it only makes sense to run the dom-walker if there was a canvas render. is that assumption false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shouldRerender was a bit of a misnomer, especially as it was gating logic that should run regardless of if that canvas rendered (e.g. the editorDispatchClosingOut at the end). There are cases where it makes sense to run the dom-walker without a canvas render (e.g. navigating to a different route). But that being said, the below condition should actually be somethingChanged || runDomWalker so I'll fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated it to shouldRerender || runDomWalker in the end

Copy link
Contributor

Choose a reason for hiding this comment

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

hmmm, but shouldRerender is already const shouldRerender = (somethingChanged || runDomWalker) && !anyCodeAhead(dispatchResult.unpatchedEditor.projectContents), aren't you defeating the anyCodeAhead check then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The dom walker should run whenever the canvas is rendered, hence the shouldRerender check, but it should also run when we explicitly ask it to (e.g. when the editor is in play mode and the user has navigated to a different route), hence the || runDomWalker.

@Rheeseyb Rheeseyb changed the title (DRAFT) Don't render when code ahead Don't render when code ahead Jul 12, 2024
@Rheeseyb Rheeseyb marked this pull request as ready for review July 12, 2024 11:52
@Rheeseyb Rheeseyb merged commit 8b29bf0 into master Jul 12, 2024
23 checks passed
@Rheeseyb Rheeseyb deleted the fix/dont-render-when-code-ahead branch July 12, 2024 13:57
liady pushed a commit that referenced this pull request Dec 13, 2024
**Problem:**
When making code changes, we'll cause the canvas to re-render twice:
once when the code is changed; and then a second time once the parsed
model has caught up.

**Fix:**
Specifically check if any code is ahead before triggering a canvas
render. To do this I used the existing `shouldRerender` in `editor.tsx`,
but realised that was actually a bit of a misnomer, and was gating logic
that it shouldn't be. So I have explicitly used the relevant boolean
checks around the individual parts of the code called after a dispatch.

Note that I have completely removed the check around
`editorDispatchClosingOut`. I'm very much certain that we should never
have been gating that with a check that something had changed, as the
internal logic of that function is already checking that something
actually changed before e.g. triggering a save.

My only concern with this was that if you are typing fast enough it
would prevent the canvas from rendering until you paused briefly,
because we don't change the revisions state following a parser-printer
update if the update is stale here:
<https://github.com/concrete-utopia/utopia/blob/67abcdd1d9fa11c0bf42be255cf655bba45a5a38/editor/src/core/shared/parser-projectcontents-utils.ts#L100-L109>
However, in practice, even on the hydrogen project this never happened,
because the IndexedDB polling part which forms the code-editor-to-utopia
communication channel is slower than parsing the code. So I then added a
600ms pause in the parsing to force the code updates to come through
faster, which meant updating the canvas only happened when you stopped
typing, and even in that forced case it actually didn't feel bad.

**Manual Tests:**
I hereby swear that:
- [x] I opened a hydrogen project and it loaded
- [x] I could navigate to various routes in Preview mode

---------

Co-authored-by: Sean Parsons <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants