Skip to content

Commit 91e12ce

Browse files
committed
Add documentation for how we use existentials in SwiftSyntax
1 parent ee01a3a commit 91e12ce

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SwiftSyntaxDoccIndexTemplate.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
2727
These articles are intended for developers wishing to contribute to SwiftSyntax
2828

2929
- <doc:ChangingSwiftSyntax>
30+
- <doc:Existentials>
3031

3132
### Syntax
3233

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Usage of protocol types in SwiftSyntax
2+
3+
SwiftSyntax tries to minimize the use of existentials (aka. protocols spelled with `any` or protocols spelled without `some`) wherever possible. This is because when the stored value is more than 3 words (a word is the size of a pointer) large, these existentials store their data on the heap. The data stored inside `RawSyntax` is bigger than 3 words and thus every time you pass a value around as a e.g. an `ExprSyntaxProtocol`, a new heap allocation will be made and that data needs to be reference-counted, which causes a very noticable performance overhead.
4+
5+
There are two more performant alternatives:
6+
- When passing a single node around, use `some ExprSyntaxProtocol`. This allows the concrete expression node (e.g. an `IntegerLiteralExprSyntax`) to be passed directly without the need to wrap it in an existential and thus avoid the performance overhead.
7+
- When multiple expression nodes need to be represented that might be of different types, eg. in an array of expressions, use the `ExprSyntax` type. `ExprSyntax` is a struct and can thus be allocated on the stack. The downside is that specific expression nodes need to explicitly be upcast to `ExprSyntax`, eg. as `ExprSyntax(integerLiteral)`. `ExprSyntax` can be cast to more specific types using the `as` method, e.g. `expr.as(IntegerLiteralExprSyntax.self)`.

Sources/SwiftSyntax/Documentation.docc/generated/SwiftSyntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
2727
These articles are intended for developers wishing to contribute to SwiftSyntax
2828

2929
- <doc:ChangingSwiftSyntax>
30+
- <doc:Existentials>
3031

3132
### Syntax
3233

0 commit comments

Comments
 (0)