Skip to content

[SwiftParser] Move typed raw syntax nodes into SwiftParser - #3381

Open
rintaro wants to merge 1 commit into
swiftlang:mainfrom
rintaro:productize-raw-x-syntax
Open

rintaro wants to merge 1 commit into
swiftlang:mainfrom
rintaro:productize-raw-x-syntax

Conversation

@rintaro

@rintaro rintaro commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

To reduce the SwifitSyntax's code size. Move RawXXXSyntax, RawSyntaxNodeProtocol, and RawSyntaxNodeList to SwiftParser module and make them internal.

Keep the node builders in SwiftSyntax: each layout node now has RawSyntax.make<Kind>(...) factory method e.g. makeFunctionDecl(...). This is important for inlining RawSyntax.makeLayout, and the same-shape factories are merged together.
Similarly, collections are built through RawSyntax.makeCollection(...).

validateLayout(layout:as:) stays in SwiftSyntax but no longer depends on the typed raw nodes: it matches each child's RawSyntax.kind against the kinds allowed for the expected syntax type by calling that type's internal isKindOf(_ kind: SyntaxKind), generated on every syntax node, base node, collection and child-choice enum, so it needs neither a protocol requirement nor any public surface.

Code size:

main this PR change
SwiftSyntax.o 5.98 MB 5.04 MB −914 KB (−15.6%)
↳ __text 2.86 MB 2.50 MB −348 KB (−12.5%)
↳ __DATA 874 KB 661 KB −208 KB (−24.4%)
SwiftParser.o 1.10 MB 1.16 MB +58 KB (+5.4%)
↳ __text 798 KB 814 KB +15 KB (+1.9%)
both modules 7.08 MB 6.21 MB −856 KB (−12.4%)

Parsing perf is neutral (−0.12% to +0.08% instructions)

For toolchain (macOS, arm64, --no-assert)

file main branch change
lib_CompilerSwiftSyntax.dylib 16.39 MB 13.14 MB −3.25 MB
lib_CompilerSwiftParser.dylib 2.62 MB 2.68 MB +59 KB
libSwiftSyntax.dylib 16.44 MB 13.18 MB −3.26 MB
libSwiftParser.dylib 2.49 MB 2.54 MB +53 KB
SwiftSyntax.swiftmodule/…private.swiftinterface 1.64 MB each 1.23 MB each −0.41 MB

@rintaro
rintaro force-pushed the productize-raw-x-syntax branch 5 times, most recently from d14adae to f48659f Compare July 18, 2026 22:05
@rintaro
rintaro force-pushed the productize-raw-x-syntax branch from f48659f to ff4b8f4 Compare July 28, 2026 18:31
@rintaro
rintaro force-pushed the productize-raw-x-syntax branch from ff4b8f4 to 2b71b19 Compare August 30, 2026 21:49
@rintaro
rintaro marked this pull request as draft September 23, 2026 03:11
@rintaro
rintaro force-pushed the productize-raw-x-syntax branch 2 times, most recently from f29ccf9 to b0cbb15 Compare October 13, 2026 07:40
@rintaro
rintaro force-pushed the productize-raw-x-syntax branch 2 times, most recently from 56d0889 to b5e198c Compare October 20, 2026 07:32
@rintaro
rintaro marked this pull request as ready for review October 20, 2026 20:23
The typed `RawXXXSyntax` nodes, `RawSyntaxNodeProtocol` and
`RawSyntaxNodeList` are only used while parsing, so move them from
SwiftSyntax into SwiftParser as internal types. Making them internal
narrows SwiftSyntax's API and lets the optimizer drop the metadata and
protocol witness tables that public conformances must emit.

Building a node stays in SwiftSyntax. Each layout node has a
`RawSyntax.make<Kind>(...)` factory, e.g. `makeFunctionDecl(...)`,
that takes the children as `RawSyntax` and inlines
`RawSyntax.makeLayout`, whose storage mode and child count are
constants there, so identically shaped factories still merge.
Collections are built through `RawSyntax.makeCollection`, whose
closure writes each element into its slot. A typed raw node's
initializer in SwiftParser only unwraps its children and calls the
factory. `RawTokenSyntax` stays in SwiftSyntax; it conforms to
`RawSyntaxNodeProtocol` in SwiftParser.

`validateLayout(layout:as:)` stays in SwiftSyntax but no longer
depends on the typed raw nodes. It matches each child's
`RawSyntax.kind` against the kinds allowed for the expected syntax
type by calling that type's `isKindOf(_ kind: SyntaxKind)`, an
internal static method generated on every syntax node, base node,
collection, and child-choice enum, which the validator is handed for
the concrete type.
@rintaro
rintaro force-pushed the productize-raw-x-syntax branch from b5e198c to 5f7ea02 Compare October 21, 2026 23:53
@rintaro

rintaro commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

@swift-ci Please test

@rintaro

rintaro commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

@swift-ci Please test Windows

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.

2 participants