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

fix: always assign text.nodeValue #11944

Merged
merged 3 commits into from
Jun 7, 2024
Merged

fix: always assign text.nodeValue #11944

merged 3 commits into from
Jun 7, 2024

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Jun 6, 2024

alternative to #11937
Closes #11932

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jun 6, 2024

🦋 Changeset detected

Latest commit: 699caa6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

dom.__nodeValue = next_node_value;
}
export function set_text(text, value) {
text.nodeValue = value;
Copy link
Member

Choose a reason for hiding this comment

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

Are you removing skipping during hydration because reading nodeValue to eventually skip if eventually more expensive than just set the value?

Copy link
Member Author

Choose a reason for hiding this comment

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

there's just nothing to do differently in the hydration case any more

Copy link
Member

Choose a reason for hiding this comment

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

there's just nothing to do differently in the hydration case any more

Wouldn't be possible to skip the assignment since the value is already there from SSR?

Copy link
Member Author

Choose a reason for hiding this comment

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

it could have changed

@trueadm
Copy link
Contributor

trueadm commented Jun 6, 2024

This results in DOM mutations even when the content is the same unfortunately

@Rich-Harris
Copy link
Member Author

fixed with a if (text.nodeValue != value) wrapper

@trueadm
Copy link
Contributor

trueadm commented Jun 7, 2024

This is slower than what we have on main:

This PR:
Screenshot 2024-06-07 at 14 38 40

Main:
Screenshot 2024-06-07 at 14 38 15

benchmark

I wrapped process_deferred in console.times locally to capture it with 6x slowdown.

@trueadm
Copy link
Contributor

trueadm commented Jun 7, 2024

My proposed fix is:

export function set_text(dom, value) {
	// @ts-expect-error need to add __value to patched prototype
	const prev_node_value = dom.__nodeValue ??= dom.nodeValue;
	const next_node_value = stringify(value);
	if (prev_node_value !== next_node_value) {
		dom.nodeValue = next_node_value;
		// @ts-expect-error need to add __className to patched prototype
		dom.__nodeValue = next_node_value;
	}
}
Text.prototype.__nodeValue = undefined;

@Rich-Harris
Copy link
Member Author

Tweaked the benchmark to use flushSync so that it's not dependent on local changes, and I'm actually seeing the opposite, strangely enough

@trueadm
Copy link
Contributor

trueadm commented Jun 7, 2024

@Rich-Harris If you put in stringify(value) back into your PR (otherwise tests fail), it's definitely showing as slower for me.

@trueadm
Copy link
Contributor

trueadm commented Jun 7, 2024

Here's another benchmark that shows it even clearer.

@dummdidumm
Copy link
Member

Even if it's a tiny bit slower, does that really matter? It's roughly 1 milisecond judging from the benchmark, and that's for 4000 text nodes which is a lot.
Before coming to a conclusion we should also bench what happens if the value turns out to be the same. Like, is text.nodeValue substantially slower than text.__nodeValue?

@Rich-Harris
Copy link
Member Author

I found implicit coercion to be faster than explicit. Just pushed a tweak

@trueadm
Copy link
Contributor

trueadm commented Jun 7, 2024

@dummdidumm My last comment shows that actually, pretty well too. Update: Rich just patched it, so the overhead from reading nodeValue is now gone when the value doesn't change.

@Rich-Harris Rich-Harris merged commit 76388d0 into main Jun 7, 2024
8 checks passed
@Rich-Harris Rich-Harris deleted the gh-11932 branch June 7, 2024 15:49
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.

" " (space) in text nodes replaced with "" (null string) in each blocks (svelte@5.0.0-next.97+)
4 participants