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: messages deepCopy mutates src arguments #1947

Merged

Conversation

BobbieGoede
Copy link
Member

@BobbieGoede BobbieGoede commented Sep 13, 2024

Important

This has not been thoroughly tested but should demonstrate the issue with deepCopy

Currently during deepCopy if a key is unset, it will be assigned to the value of the src[key], this is an issue for objects as they will be assigned by reference, meaning following calls to deepCopy will assign properties on these objects.

So far a simple solution seems to be to set the property value to an empty object/array, this ensures both src[key] and des[key] are objects meaning deepCopy will merge these as such instead of direct assignment.

I've had this saved in a git stash for some time so it has been a while since debugging this issue, I'm opening this PR so I won't forget about it again 😅

nuxt-modules/i18n#3089
nuxt-modules/i18n#2803

Copy link

pkg-pr-new bot commented Sep 13, 2024

Open in Stackblitz

@intlify/core

pnpm add https://pkg.pr.new/@intlify/core@1947

@intlify/devtools-types

pnpm add https://pkg.pr.new/@intlify/devtools-types@1947

@intlify/core-base

pnpm add https://pkg.pr.new/@intlify/core-base@1947

@intlify/message-compiler

pnpm add https://pkg.pr.new/@intlify/message-compiler@1947

@intlify/shared

pnpm add https://pkg.pr.new/@intlify/shared@1947

petite-vue-i18n

pnpm add https://pkg.pr.new/petite-vue-i18n@1947

vue-i18n

pnpm add https://pkg.pr.new/vue-i18n@1947

@intlify/vue-i18n-core

pnpm add https://pkg.pr.new/@intlify/vue-i18n-core@1947

commit: 05b2353

Copy link
Member

@kazupon kazupon left a comment

Choose a reason for hiding this comment

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

Looks good to me!
Thank you!

@BobbieGoede BobbieGoede marked this pull request as ready for review September 13, 2024 15:54
@BobbieGoede
Copy link
Member Author

I'm curious what the impact of this change is on performance since previously the objects assigned by reference were not looped over by key.

@kazupon
Copy link
Member

kazupon commented Sep 13, 2024

Yeah, performance is certainly a concern, and I need to look at the cost of isObject checks and object key refer in the if condition.
It's worth doing a simple performance benchmark using deepCopy.

@BobbieGoede
Copy link
Member Author

BobbieGoede commented Sep 13, 2024

Somewhat related to performance and object checks (but not really related to this PR), I feel like #1379 should be reverted.

The change was made to support Safari 10 which is not receiving security updates (Apple doesn't issue EOL dates), the original code seems to work fine for Vue core https://github.com/vuejs/core/blob/main/packages/shared/src/general.ts#L74-L75 and is likely more performant as well.

@kazupon
Copy link
Member

kazupon commented Sep 15, 2024

Somewhat related to performance and object checks (but not really related to this PR), I feel like #1379 should be reverted.

Let's do it!

@BobbieGoede BobbieGoede mentioned this pull request Sep 15, 2024
@BobbieGoede BobbieGoede force-pushed the fix/message-deep-copy-merging-mutation branch from 9f3bb87 to 05b2353 Compare September 15, 2024 10:37
@BobbieGoede
Copy link
Member Author

Yeah, performance is certainly a concern, and I need to look at the cost of isObject checks and object key refer in the if condition. It's worth doing a simple performance benchmark using deepCopy.

I tried bench testing the change and it looks like it does impact performance but not excessively so, it's worth the trade-off. The way I tested it is not pretty so I pushed it to a separate branch, you can check out the changes and results in this PR BobbieGoede#5, I'm assuming we don't want to add a bench like this.

@kazupon
Copy link
Member

kazupon commented Sep 16, 2024

Thanks!
I've checked your benchmark.
I think the performance is still acceptable too.
I'll merge this PR!

@kazupon kazupon merged commit 19054e0 into intlify:master Sep 16, 2024
24 checks passed
@kazupon kazupon added the Type: Improvement Includes backwards-compatible fixes label Sep 16, 2024
BobbieGoede added a commit to BobbieGoede/vue-i18n-next that referenced this pull request Sep 26, 2024
* fix: messages `deepCopy` mutates `src` arguments

* fix: `deepCopy` should never merge arrays
kazupon pushed a commit that referenced this pull request Sep 26, 2024
* fix: messages `deepCopy` mutates `src` arguments

* fix: `deepCopy` should never merge arrays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Improvement Includes backwards-compatible fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants