Avoid a second graph build when getting assets#1511
Conversation
44e064c to
08e7f76
Compare
|
Hi @tido64 - thanks for spotting this. Indeed the transform results would normally be cached (where a cache is configured), but resolutions wouldn't, so this is a useful optimisation. Is it particularly the use of the Does it still work for you if I pull this in and make those changes? |
Yes, please! As long as we implement the optimization, please feel free to make any changes. |
|
@robhogan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
|
@robhogan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Summary: dannyvv recently discovered that Metro calls [`buildSubgraph()`](https://github.com/facebook/metro/blob/15fef8ebcf5ae0a13e7f0925a22d4211dde95e02/packages/metro/src/DeltaBundler/buildSubgraph.js#L105) twice during bundling. Once during `build()`: https://github.com/facebook/metro/blob/15fef8ebcf5ae0a13e7f0925a22d4211dde95e02/packages/metro/src/index.flow.js#L431 And again, shortly after, in `getAssets()`: https://github.com/facebook/metro/blob/15fef8ebcf5ae0a13e7f0925a22d4211dde95e02/packages/metro/src/index.flow.js#L435 I *think* the second build should be mostly cached, but it would be better if we can avoid it altogether if possible. Digging deeper down, it looks like we have the full graph inside `build()`, but throw away the work when returning. I've changed things so that it also returns the graph and made it possible to pass it to `getAssets()` to avoid the second graph build. I don't know if this is the best way to solve this, but it seems to work on my machine™️. Changelog: [Performance] `runBuild`: Avoid a second graph build when getting assets Pull Request resolved: react#1511 Test Plan: I've added `console.log` inside `buildSubgraph` to make sure it only gets called once per unique input. Reviewed By: huntie Differential Revision: D76342685 Pulled By: robhogan fbshipit-source-id: d66b6dbe73b4130c140b0a78fd4ed7e53d7a18c3
Summary
@dannyvv recently discovered that Metro calls
buildSubgraph()twice during bundling. Once duringbuild(): https://github.com/facebook/metro/blob/15fef8ebcf5ae0a13e7f0925a22d4211dde95e02/packages/metro/src/index.flow.js#L431And again, shortly after, in
getAssets(): https://github.com/facebook/metro/blob/15fef8ebcf5ae0a13e7f0925a22d4211dde95e02/packages/metro/src/index.flow.js#L435I think the second build should be mostly cached, but it would be better if we can avoid it altogether if possible.
Digging deeper down, it looks like we have the full graph inside
build(), but throw away the work when returning. I've changed things so that it also returns the graph and made it possible to pass it togetAssets()to avoid the second graph build. I don't know if this is the best way to solve this, but it seems to work on my machine™️.Changelog: [Performance] Avoid a second graph build when getting assets
Test plan
I've added
console.loginsidebuildSubgraphto make sure it only gets called once per unique input.