Even with all these fields boxed, so they're 8 bytes each, that's still 24 bytes out of 104 that are TS-specific.
Many other AST types similarly have various TS-specific fields.
The problems with this
Unnecessarily larger memory use when the source code is JS (the majority of code that Oxc will be used on, as when e.g. bundling majority of code is from node_modules).
Many more fields to visit when using Visit / VisitMut / Traverse - slower.
Possible solutions
1. Separate types
We could have 2 separate types Function and FunctionWithTS.
As the AST is now #[repr(C)] and we control type memory layouts, we could make Function and FunctionWithTS have identical layouts for the JS fields, and have the TS fields at end of FunctionWithTS. Then when stripping types in transformer, to turn a FunctionWithTS into a plain Function, all you have to do is change the discriminant of the Expression (1 byte write).
Many AST types contain multiple fields for TS-related data e.g.
Function
:Even with all these fields boxed, so they're 8 bytes each, that's still 24 bytes out of 104 that are TS-specific.
Many other AST types similarly have various TS-specific fields.
The problems with this
node_modules
).Visit
/VisitMut
/Traverse
- slower.Possible solutions
1. Separate types
We could have 2 separate types
Function
andFunctionWithTS
.As the AST is now
#[repr(C)]
and we control type memory layouts, we could makeFunction
andFunctionWithTS
have identical layouts for the JS fields, and have the TS fields at end ofFunctionWithTS
. Then when stripping types in transformer, to turn aFunctionWithTS
into a plainFunction
, all you have to do is change the discriminant of theExpression
(1 byte write).2. Store TS fields in a separate struct
Then the TS-related fields in
Function
could be replaced with just 1 fieldts: Option<Box<FunctionTS>>
(8 bytes).3. Store TS fields in side array
Store TS-related fields in a side array indexed by
AstNodeId
.Last thoughts
Maybe this is not a good idea. It would be more performant, but perhaps that's outweighed by ergonomics.
Anyway, this is not important or urgent. Just writing it up now as it occurred to me.
Related to #34.