-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Optimizing Compiler: Component Folding #7323
Comments
Access to other files source code can be established by populating the full dependency graph. Tools like browserify, webpack, systemjs or assetgraph give you that |
@Munter This is not currently available by default nor set up in people's build systems. At any kind of large code base it is also too slow to just read/parse these files randomly. It needs to be smarter, parallelize and cache info. It's certainly not impossible to build but it just isn't integrated. We'd have to build it and get adoption before this particular transform is possible. |
To illustrate what @sebmarkbage is talking about rollup limitations, here is a pseudo code of the example above: Then, running the output of rollup thru closure compiler, you will get this: module.exports = = function(b) {
b = {data:{type:"img", src:b.src}, className:"bar"};
return "img" === b.data.type ?
Img({src:b.data.src, className:b.className, alt:b.alt}) :
Span(b.data.type);
}; In other words, the precedent is there, and certainly, rollup's tree-shaking capabilities can be improved to cover other primitives, but we will need much more optimizations in terms of how react uses the components to achieve what you described. /cc @Rich-Harris |
This reminds me a bit of @jeffmo's talk about using Flow for tree-shaking at React Europe. |
I thought about it a lot and there would probably be a need of some sort of cursor to edit what to fold and what not to fold regarding code size no ? After a lot of folding, there would logically be the original component definition (if we couldn't fold everything ?) + the number of time it was folded * the size of what was folded. Depending on what was done; the bundled size could jump a lot. Unless I'm missing something, this component folding should only fold one liners for webApp, and high to no limitations for server side rendering (if you build your backend) and desktop apps (electron & stuff). |
Great |
@sebmarkbage isn't this only half of the final frontier? In my mind the final frontier of React includes (in addition to component folding) compiling away any virtual DOM diff when possible. For example: import React, { useState } from 'react'
const ChristmasButton = () => {
const [color, setColor] = useState('green')
return (
<button
style={{backgroundColor: color}}
onClick={() => setColor(color === 'green' ? 'red' : 'green')}
/>
)
} What if React could compile the
|
@jedwards1211 That seems to contradict React's design philosophy of preserving control over scheduling, if I understand correctly. That thin layer allows prioritizing more critical updates over less important ones that may otherwise noticeably degrade performance. |
@coetry that's true...I guess such an update probably isn't possible if the scheduler relies heavily on the virtual DOM, but maybe it could request an update from React, and when the update runs all it would actually do is |
This is like the final frontier for React but I never really wrote anything about it so I figured I'd create an issue.
Basically, the idea is to utilize information about how React works to do constant folding and inlining of components under certain conditions.
Example Source:
By knowing what Foo and Classes is made up of, we can turn the Bar component into this:
Dead-code elimination then strips it down to just:
Now there are a bunch of different cases where this needs to bail out. For example, we need to know that the
CSSClasses
object and thedefaultProps
object is immutable, or we need to infer that it is immutable using Escape analysis.With classes these bail out cases are even more complex.
The problem is that current JS infrastructure is particularly bad at this kind of whole program or whole package linking. Node doesn't have a notion of per package private modules so anything can mutate anything by default. Transpilers such as Babel are set up to work on a single file at a time. They don't have access to the source of other files to do this analysis. Rollup is closer but is limited to a small set of static primitives.
However, once smarter compilers become more prevalent in the JS world or we find ways to hack around the limitations, we can start building out more of these smarter compiler optimizations.
The text was updated successfully, but these errors were encountered: