feat(compiler): add an optimize option for constant folding and DCE - #3231
Open
lxsmnsyc wants to merge 4 commits into
Open
feat(compiler): add an optimize option for constant folding and DCE#3231lxsmnsyc wants to merge 4 commits into
lxsmnsyc wants to merge 4 commits into
Conversation
🦋 Changeset detectedLatest commit: 0ce6be7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add an
optimizeoption to@solidjs/compileroptimize: true(defaultfalse) runs a constant-folding and dead-code-elimination pass over the parsed program before JSX is lowered. Anything it resolves never reaches a generate at all: the element pays for no component call, memo, or insert hole, and its markup joins the surrounding template.What folds
const, andletthat nothing ever writes to. References resolve throughoxc_semantic, so aconst DEBUG = falseinside a component folds at its use sites while a same-named binding in another scope is a different symbol and is left alone.varis excluded, since a read before its declaration seesundefinedrather than throwing.if/elsebranches,while (false)loops, and anything after areturn,throw,break, orcontinue.[effect()],{ k: effect() }, a class with a static block), the branch still folds and the condition is resequenced in front of the result:[b()] ? c() : d()becomes([b()], c()). Component props are the exception and stay unfolded, since Solid reads them inside a memo.Control-flow components
<Show when>fallback<For each>fallbackwheneachis[]or statically falsy<Repeat count>fallbackwhencountis below one<Switch>/<Match when><Dynamic component>componentis a static intrinsic tag name<Portal>,<Loading>,<Errored>, and<Reveal>deliberately never fold. Each gates on runtime state (a mount target, a pending read, a thrown error, a reveal order) that no static analysis can decide.Rules that keep a fold from changing behavior
builtInsonly makes a name a candidate. A tag folds only when it resolves to Solid's own component: either nothing declares the name and the compiler auto-imports it, or it is a top-level value import frommoduleNameorsolid-js. A localShow, or one imported from an unrelated module, is a different component and is left alone.<Cond>fromimport { Show as Cond } from "solid-js"folds asShow.import { Show as Cond } from "./my-show"does not fold.varor function declaration is never removed.Consistency requirement
Folding changes the shape of the rendered tree, and with it hydration ids. A server build and its client build must be compiled with the same
optimizevalue. This is called out in the README, the TypeScript types, and the changeset.Files
src/optimize/mod.rssrc/optimize/value.rssrc/shared/ast_builder.rs<Dynamic>foldsrc/compiler.rs,src/config.rs,src/node_adapter.rs,src/lib.rsindex.js,types.d.ts,README.mdTesting
NaN,-0,ToInt32wrapping,1 ** Infinity), scope resolution, and every built-in identity case.--no-default-features, and--features tsrx.rustfmtclean, andclippyproduces no new warnings.Scope
Compiler only.
@solidjs/babel-plugindoes not gain the option, so the option-matrix parity suite is untouched. The pass is off by default, so existing output is unchanged.