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

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Aug 18, 2022

  • RawXXXListSyntax.init now only accepts [Element] instead of Collection<Element> to avoid potential cross module generics performance issue
  • Avoid temporary Array creation in RawXXXListSyntax.init. Use a low-level initializer and directly initialize the elements buffer
  • Add __shared to arena: SyntaxArena parameters for RawXXXSyntax initializers.

`.map { $0.raw }` creates a temporary Array which causes heap
allocations.
Use `RawSyntax.init(kind:unintializedCount:...)` initializer and
directly initialize the storage with the mapped value.
To avoid unnecessary ref-counting traffic.
@rintaro rintaro requested a review from ahoppen as a code owner August 18, 2022 04:20
@rintaro
Copy link
Member Author

rintaro commented Aug 18, 2022

@swift-ci Please test

@@ -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.

@rintaro rintaro merged commit 3f83d98 into swiftlang:main Aug 18, 2022
@rintaro rintaro deleted the rawsyntax-initperf branch August 18, 2022 16:57
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