Skip to content

Performance tweak for RawXXXSyntax initializers #603

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 3 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions Sources/SwiftSyntax/RawSyntaxNodeProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public struct RawTokenSyntax: RawSyntaxNodeProtocol {
kind: RawTokenKind,
wholeText: SyntaxText,
textRange: Range<SyntaxText.Index>,
arena: SyntaxArena
arena: __shared SyntaxArena
) {
assert(arena.contains(text: wholeText),
"token text must be managed by the arena")
Expand All @@ -128,7 +128,7 @@ public struct RawTokenSyntax: RawSyntaxNodeProtocol {
text: SyntaxText,
leadingTriviaPieces: [RawTriviaPiece],
trailingTriviaPieces: [RawTriviaPiece],
arena: SyntaxArena
arena: __shared SyntaxArena
) {
assert(arena.contains(text: text), "token text must be managed by the arena")
let totalTriviaCount = leadingTriviaPieces.count + trailingTriviaPieces.count
Expand All @@ -153,7 +153,7 @@ public struct RawTokenSyntax: RawSyntaxNodeProtocol {
}

/// Creates a missing `TokenSyntax` with the specified kind.
public init(missing kind: RawTokenKind, arena: SyntaxArena) {
public init(missing kind: RawTokenKind, arena: __shared SyntaxArena) {
self.init(
kind: kind, text: "", leadingTriviaPieces: [], trailingTriviaPieces: [],
arena: arena)
Expand Down
13 changes: 10 additions & 3 deletions Sources/SwiftSyntax/RawSyntaxNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ public struct Raw${node.name}: Raw${node.name if node.is_base() else node.base_t
% end
%
% if node.is_syntax_collection():
public init<C: Collection>(elements: C, arena: SyntaxArena) where C.Element == Raw${node.collection_element_type} {
let raw = RawSyntax.makeLayout(kind: .${node.swift_syntax_kind}, from: elements.map { $0.raw }, arena: arena)
public init(elements: [Raw${node.collection_element_type}], arena: __shared SyntaxArena) {
Copy link
Member

Choose a reason for hiding this comment

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

Could we get the same performance improvement by adding

@_specialize(where C == Array<Raw${node.collection_element_type}>)

to the declaration while still having the convenience of being able to pass other collections in?

Copy link
Member Author

@rintaro rintaro Aug 18, 2022

Choose a reason for hiding this comment

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

Probably yes. But it will increase the code size. Since most clients should never use these RawSyntax APIs, I don't think it's worth the bother.

let raw = RawSyntax.makeLayout(
kind: .${node.swift_syntax_kind}, uninitializedCount: elements.count, arena: arena) { layout in
guard var ptr = layout.baseAddress else { return }
for elem in elements {
ptr.initialize(to: elem.raw)
ptr += 1
}
}
self.init(raw: raw)
}

Expand All @@ -78,7 +85,7 @@ public struct Raw${node.name}: Raw${node.name if node.is_base() else node.base_t
% param_default = " = nil" if child.is_unexpected_nodes() else ""
${param_label}${child.swift_name}: ${param_type}${param_default},
% end
arena: SyntaxArena
arena: __shared SyntaxArena
) {
% if node.children:
let raw = RawSyntax.makeLayout(
Expand Down
Loading