Skip to content

Add a documentation article for @_spi and when to use existentials #1696

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

Merged
merged 4 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
### Articles

- <doc:Working-with-SwiftSyntax>
- <doc:ChangingSwiftSyntax>
- <doc:Create-SwiftSyntax-Release>

### Tutorials

- <doc:Tutorial-Table-of-Contents>

### Contributing

These articles are intended for developers wishing to contribute to SwiftSyntax

- <doc:ChangingSwiftSyntax>
- <doc:Existentials>

### Syntax

- <doc:SwiftSyntax/Syntax>
Expand Down

This file was deleted.

10 changes: 10 additions & 0 deletions Sources/SwiftSyntax/Documentation.docc/Existentials.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe usage of double ticks would make sense?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Updated

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# When to use protocols in SwiftSyntax

Learn when to use protocols value types like ``ExprSyntax`` over protocols like ``ExprSyntaxProtocol``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel like this line (and the one in the other file) add much beyond the title itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But docc renders this as the abstract of the article and without this the entire next paragraph becomes the abstract, which looks ugly.

See https://swiftpackageindex.com/apple/swift-syntax/508.0.1/documentation/swiftsyntax/changingswiftsyntax

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good to know, thanks.



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.

There are two more performant alternatives:
- 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.
- 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)`.
9 changes: 9 additions & 0 deletions Sources/SwiftSyntax/Documentation.docc/SPI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# `@_spi` attribute

Learn when SwiftSyntax exposes declaration annotated as `@_spi`.

Functions marked as `@_spi(RawSyntax)` (where ``RawSyntax`` can be any name) are considered *SPI* (System Programming Interface) and are only accessible if the module that declares them is imported as `@_spi(RawSyntax)`.

Since functions marked as SPI are not part of the public API, swift-syntax makes no guarantee to their source stability. swift-syntax makes no effort to keep its SPI stable.

Declarations are typically marked as SPI because they have some kind of caveat that makes them unsafe to use in general. For example, when accessing ``RawSyntax`` nodes, you need to manually guarantee that the ``SyntaxArena`` they live in doesn’t get de-allocated. Other declarations have an `@_spi` to share them between different modudules within the swift-syntax package but those APIs shouldn’t be accessed publicly.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
### Articles

- <doc:Working-with-SwiftSyntax>
- <doc:ChangingSwiftSyntax>
- <doc:Create-SwiftSyntax-Release>

### Tutorials

- <doc:Tutorial-Table-of-Contents>

### Contributing

These articles are intended for developers wishing to contribute to SwiftSyntax

- <doc:ChangingSwiftSyntax>
- <doc:Existentials>

### Syntax

- <doc:SwiftSyntax/Syntax>
Expand Down