Content-Length: 345097 | pFad | http://redirect.github.com/babel/babel/pull/17142

20 fix: "Map maximum size exceeded" in deepClone by liuxingbaoyu · Pull Request #17142 · babel/babel · GitHub
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: "Map maximum size exceeded" in deepClone #17142

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

liuxingbaoyu
Copy link
Member

Q                       A
Fixed Issues? Fixes #16967
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In Babel 7, only a workaround was applied, and in Babel 8, a complete fix was made.

@babel-bot
Copy link
Collaborator

babel-bot commented Feb 20, 2025

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58779

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: core labels Feb 20, 2025
try {
return deepClone(value, new Map(), true);
} catch (_) {
return structuredClone(value);
Copy link
Member Author

@liuxingbaoyu liuxingbaoyu Feb 20, 2025

Choose a reason for hiding this comment

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

This only works in node v17+, but v8.serialize doesn't work in browsers and other runtimes.

Copy link
Member

Choose a reason for hiding this comment

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

Can we use structuredClone in Babel 8?

Copy link
Member Author

Choose a reason for hiding this comment

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

#16967 (comment)
Unfortunately it is slow, less than half the performance of current implementations.🤦‍♂️

function deepClone(
value: any,
cache: Map<any, any>,
allowCircle: boolean,
Copy link
Member

Choose a reason for hiding this comment

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

The allowCircle is false but the object actually contains a cycle, this function is now going to cause an infinite loop, right? Can we throw in that case instead? We'd only need to keep track of the current "stack", rather than of all nodes in the map.

try {
return deepClone(value, new Map(), true);
} catch (_) {
return structuredClone(value);
Copy link
Member

Choose a reason for hiding this comment

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

Can we use structuredClone in Babel 8?

circleSet.clear();
throw new Error("Babel-deepClone: Cycles are not allowed in AST");
}
circleSet.add(value);
Copy link
Member

Choose a reason for hiding this comment

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

How big is the performance impact of using this set compared to:

  • the old behavior with the map
  • the behavior with no protection against infinite loops

If it's significantly expensive, we could keep track of the depth we are at in the cloning and only start using the cycle detection logic if it's, for example, >100. It means that we'll be slower in error cases, but faster in the (common) successful case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: core PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: "Map maximum size exceeded" in deepClone
4 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://redirect.github.com/babel/babel/pull/17142

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy