-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby): use V8.serialize instead of JSON.stringify if available #10732
Conversation
Oh nifty. Didn't know about this. We could check the node version being used and use one or the other. |
or just straight check if |
I'll add a fallback next year, @pieh no memory usage numbers yet, but it seems that v8 serialization happens out-of-process, i.e. |
Some memory usage numbers: markdown benchmark is at 73%. Also did a benchmark with a 100.000 entries Map (10 run average): v8 json-stringify-safe Two questions:
|
@stefanprobst whoa! That is looking encouraging! Nice work! |
"Like JSON.stringify, but doesn't throw on circular references." - there were issues with those in the past, so this package was taking care of them.
We shouldn't be blocking main thread here. Are there any advantage to using sync version? |
Where are they coming from? I'm asking because we cannot recreate the references with JSON.parse? And also because easiest would be to just use
From the docs: It is unsafe to use fs.writeFile() multiple times on the same file without waiting for the callback. |
Has anyone had a chance to test this? |
@stefanprobst We didn't. This will need to wait at least a week, because most of us will be offline next week - so we wouldn't be able to deal with any potential bugs it might introduce |
One issue: EDIT: This is also an issue with |
This looks really promising. We could use it on large (18K+ pages) site that is not building because our cache is too big (can't serialize redux index saveSate) here. How could we help with this? |
@ivanoats Would be awesome if you could test this with your site and report back your findings! |
Not sure how to proceed with this, but another idea is to break the node store out of app state, i.e. keep using |
We (@rsstdd and I) tested this just now. Our site looks fine in develop, and is no longer crashing on build ... but it looks like many files are missing from the build. Some images, JSON files, and JS files. I think this is a good direction to go in, not sure how best to help with @stefanprobst 's code right now. It looks like he was looking for feedback from other contributors. |
@ivanoats Thanks so much for testing this! To quickly clarify: on your site this works without issues with |
@stefanprobst We were able to build ~30k pages after removing Thank you for your work! |
@rsstdd That's great!! Do you still see issues with a populated |
The Serialization API is currently experimental and the stability is 1. Generally I would not recommend to use this in production or in general as this could change at any time in the future (Node 11, 12, ...). |
@DanielRuf Well it has been added in Node 8 and is e.g. used by |
Well, Jest supports https://github.com/facebook/jest/blob/master/packages/jest-serializer/src/index.ts Still, I would not recommend this without proper fallbacks. |
|
@rsstdd thanks for testing & @stefanprobst thanks for working on this! I'll ping the @gatsbyjs/core to see if we can get this merged. |
@wardpeet Cool! One open issue is how to deal with nodes that contain functions, e.g. |
Kinda. Since these already weren't being serialized, they were already broken. |
Possible issues:
(e.g. things that can't be serialized with v8) What we need to do now is make sure that we're not breaking unrelated sites--it does appear to fix the issue though. |
I just finished verifying
So I'm feeling fairly confident in going forward with this change without doing any feature flags |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some bikeshed comments but this looks nice! @pieh lets ship this? or you rather want to do it under a tag first?
I forgot to mention this before. I published this under dist-tag, so if anyone want to try - install |
Here's also some chart showing memory usage improvements - https://lucid-galileo-3c3f54.netlify.com/ Things to check - in first graph (using JSON.stringify) there are huge memory spikes when we persist state (marked by green areas), and eventually the process crashes (around ~84s when memory usage "flatlines"). Second chart (when using those changes) memory barely spikes when persisting state |
Use
v8.serialize
instead ofjson-stringify-safe
for persisting redux state for a small performance gain (from ~250ms to ~150ms in the markdown benchmark). Useful?NOTE: Could only be merged once we switch minimum Node version to 8.0.