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: no data duplication in reactive Set/Map #11200

Merged
merged 2 commits into from Apr 17, 2024
Merged

Conversation

Azarattum
Copy link
Contributor

Svelte 5 rewrite

Data in reactive Set/Map was unnecessarily passed back to the super class. That would mean storing the same thing twice. This PR addresses this problem and adds a couple of tests to ensure the implementation is correct.

We have discussed this earlier on Discord: https://discord.com/channels/457912077277855764/1153350350158450758/1227898085087248434, where @trueadm suggested I made a PR.

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 Apr 17, 2024

⚠️ No Changeset found

Latest commit: 7405041

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@Azarattum Azarattum changed the title fix: No data duplication in reactive Set/Map fix: no data duplication in reactive Set/Map Apr 17, 2024
Copy link
Contributor

@trueadm trueadm left a comment

Choose a reason for hiding this comment

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

Thanks for this!

@trueadm trueadm merged commit e7301af into sveltejs:main Apr 17, 2024
8 checks passed
@harrisi
Copy link

harrisi commented Apr 18, 2024

This breaks Set prototype methods that are added to the ReactiveSet prototype dynamically. See also #11222.

@paoloricciuti
Copy link
Contributor

I was trying to see how #11222 could be fixed without reintroducing data duplication but unless we want to reimplement each one of the methods by hand we need the data in the "faux" Set since we are invoking methods on it.

The simple fix would be to revert this and introduce data duplication again.

@harrisi
Copy link

harrisi commented Apr 18, 2024

Yes, all methods need to be implemented. The problem with that is that it will also break if the prototypes of any of the builtins are changed.

@Azarattum
Copy link
Contributor Author

I'll take look at this. We do already redefine the methods in #init. I think there has to be a way to adapt this without reintroducing data duplication.

@paoloricciuti
Copy link
Contributor

I'll take look at this. We do already redefine the methods in #init. I think there has to be a way to adapt this without reintroducing data duplication.

The point is that that's exactly the bug: in init we are redefining the methods to actually call super.methodname in the set instance but since that instance has no values they are empty.

We could reimplement those methods but that adds code to maintain, probably less performant code in those operations (since is not native) and no forward compatibility for a small memory gain.

@paoloricciuti
Copy link
Contributor

I'll take look at this. We do already redefine the methods in #init. I think there has to be a way to adapt this without reintroducing data duplication.

The point is that that's exactly the bug: in init we are redefining the methods to actually call super.methodname in the set instance but since that instance has no values they are empty.

We could reimplement those methods but that adds code to maintain, probably less performant code in those operations (since is not native) and no forward compatibility for a small memory gain.

But obviously feel free to try and report back if you succeed 😁

@Azarattum
Copy link
Contributor Author

Azarattum commented Apr 19, 2024

Yeah, I get that. I feel like there is a way to use native implementation without duplication. Also this is only applicable to Set, right? The Map isn't affected (since it doesn't have those methods). Which means we don't have to revert the entire PR in the worst case.

By the way, I think map not having those methods is actually the main issue here. Since we currently model set with map keys and there are no such methods for map keys. Maybe we can find a different approach to model the data?

@paoloricciuti
Copy link
Contributor

Yeah, I get that. I feel like there is a way to use native implementation without duplication. Also this is only applicable to Set, right? The Map isn't affected (since it doesn't have those methods). Which means we don't have to revert the entire PR in the worst case.

By the way, I think map not having those methods is actually the main issue here. Since we currently model set with map keys and there are no such methods for map keys. Maybe we can find a different approach to model the data?

I've to investigate a bit further as I didn't check if Map was also "duplicating data". I guess if you can find a way to use the data inside Set that could work. Let's think about it

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.

None yet

4 participants