Skip to content

ref(utils): Simplify value normalization #4666

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

Merged
merged 4 commits into from
Mar 3, 2022

Conversation

lobsterkatie
Copy link
Member

This PR makes a few changes to the internals of utils.normalize:

  • Instead of using JSON.stringify to handle the recursive visiting of nodes in an object tree (and then giving it a visit function (walk) which is itself a recursive visitor of nodes), forcing us then to call JSON.parse on the results, it simply uses said recursive visitor to recursively visit the object tree nodes.

  • It renames normalizeValue to makeSerializable (since that's more accurately and specifically what it's doing) and updates its out-of-date docstring to match its behavior.

  • It refers to the methods in our memoizer by name rather than number, splits out an inlined value, and moves a typecast, all to increase readability.

Note: This was a branch I had lying around, about which I was reminded while reading #4579. I don't think this will help with that a ton, but I think it can't hurt.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2022

size-limit report

Path Base Size (0b2221d) Current Size Change
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.79 KB 19.8 KB +0.01% 🔺
@sentry/browser - ES5 CDN Bundle (minified) 63.39 KB 63.37 KB -0.04% 🔽
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.44 KB 18.44 KB +0.01% 🔺
@sentry/browser - ES6 CDN Bundle (minified) 56.42 KB 56.41 KB -0.02% 🔽
@sentry/browser - Webpack (gzipped + minified) 22.25 KB 22.27 KB +0.07% 🔺
@sentry/browser - Webpack (minified) 76.34 KB 76.33 KB -0.02% 🔽
@sentry/react - Webpack (gzipped + minified) 22.28 KB 22.3 KB +0.07% 🔺
@sentry/nextjs Client - Webpack (gzipped + minified) 46.43 KB 46.44 KB +0.03% 🔺
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 26.68 KB 26.69 KB +0.03% 🔺

@@ -40,5 +40,5 @@ export function memoBuilder(): MemoFunc {
}
}
}
return [memoize, unmemoize];
return { memoize, unmemoize };
Copy link
Member

Choose a reason for hiding this comment

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

The tuple return was done for bundle size reasons btw - I think it's reasonable enough to leave as is as long as we comment it properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

I know, but IMHO we've gone too far with bundle size when we let it make our code confusing to read, which it was when I first was looking into this.

Copy link
Member

Choose a reason for hiding this comment

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

Thats a fair point. Can we perhaps use array destructuring to avoid the memo[0] calls?

const [memoize, unmemoize] = memo;

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, here I think that's fine. I'll make the change. In general, though, I'm of the mind that bundle size is important but that I also don't want to get to a point where we feel like we can't use objects when they make sense.

Help me remember, though, please. As I understand it, the reason a destructured array is better than an object (or even a destructured object) is that terser won't mangle the property names because they count as public API, whereas with an array the local names used in the destructuring will get mangled? Would it still count as public API if memoBuilder were to be defined in the same file where it's used (and were not to be exported, since it's not used anywhere else)? No, right?

(I'm asking partly just so I understand what we're talking about. I recognize that making that change would also remove memoBuilder from @sentry/utils's public API, and so would have to wait for v7 if it were to happen at all. But if I'm right, it might be worth looking at what else we export from utils which we don't actually need to.)

Copy link
Member

Choose a reason for hiding this comment

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

Regardless of if memoBuilder is exported or not, { memoize, unmemoize } means that memoize and unmemoize are unminifiable. The array destructuring just helps with that.

@AbhiPrasad
Copy link
Member

This might help with #4579

@timfish mind taking a look here?

@timfish
Copy link
Collaborator

timfish commented Mar 2, 2022

Yep, I think this is in the right direction.

I still need to try and reproduce some of the things that currently trip it up. My best guess is that it's something to do with DOM elements. Another thing that randomly popped into my head while laying bed the other night was... arrays. Do we do anything to limit their length?

@lobsterkatie lobsterkatie force-pushed the kmclb-simplify-normalize branch from ceaf57e to 07af2a4 Compare March 3, 2022 04:33
@AbhiPrasad
Copy link
Member

AbhiPrasad commented Mar 3, 2022

I opened #4676 - I think we should explore fuzzing the serialization logic.

@lobsterkatie lobsterkatie force-pushed the kmclb-simplify-normalize branch from 07af2a4 to e2c27d6 Compare March 3, 2022 14:13
@lobsterkatie lobsterkatie merged commit f715e87 into master Mar 3, 2022
@lobsterkatie lobsterkatie deleted the kmclb-simplify-normalize branch March 3, 2022 15:00
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.

3 participants