-
Notifications
You must be signed in to change notification settings - Fork 447
[Perf] Improve SyntaxRewriter visitation performance #2726
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6be8e8d
[SyntaxRewriter] Optimize SyntaxRewriter visitation
rintaro b6898d1
[SyntaxRewriter] Introduce SyntaxNodeFactory
rintaro e664473
[SyntaxRewriter] Improve new layout node creation
rintaro e2d4423
[SyntaxRewriter] Make 'visitChildren()' a non-generic mehod
rintaro b67e899
[SyntaxRewriter] Iterate RawSyntaxChildren using pattern matching
rintaro 7298fe8
[SyntaxVisitor] Adopt SyntaxNodeFactory
rintaro ef8bb05
[SyntaxRewriter] Return the node as-is
rintaro ec96b4d
[CMake] Split 'touch' workaround into two commands
rintaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[SyntaxRewriter] Introduce SyntaxNodeFactory
Factor out 'Syntax.Info' reusing into 'SyntaxNodeFactory' and 'SyntaxInfoRepository' as the backing storage. The underlying storage now uses a fixed length 'ManagedBuffer' to avoid 'ContiguousArray' overhead.
- Loading branch information
commit b6898d1d653296506824aa684493e7a19f46073f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,19 +44,8 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { | |
|
||
DeclSyntax( | ||
""" | ||
/// `Syntax.Info` salvaged from the node being deinitialized in 'visitChildren'. | ||
/// | ||
/// Instead of deallocating them and allocating memory for new syntax nodes, store the allocated memory in an array. | ||
/// We can then re-use them to create new syntax nodes. | ||
/// | ||
/// The array's size should be a typical nesting depth of a Swift file. That way we can store all allocated syntax | ||
/// nodes when unwinding the visitation stack. | ||
/// | ||
/// The actual `info` stored in the `Syntax.Info` objects is garbage. It needs to be set when any of the `Syntax.Info` | ||
/// objects get re-used. | ||
/// | ||
/// Note: making the element non-nil causes 'swift::runtime::SwiftTLSContext::get()' traffic somehow. | ||
private var recyclableNodeInfos: ContiguousArray<Syntax.Info?> | ||
/// 'Syntax' object factory recycling 'Syntax.Info' instances. | ||
private let nodeFactory: SyntaxNodeFactory = SyntaxNodeFactory() | ||
""" | ||
) | ||
|
||
|
@@ -65,8 +54,6 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { | |
public init(viewMode: SyntaxTreeViewMode = .sourceAccurate) { | ||
self.viewMode = viewMode | ||
self.arena = nil | ||
self.recyclableNodeInfos = [] | ||
self.recyclableNodeInfos.reserveCapacity(64) | ||
} | ||
""" | ||
) | ||
|
@@ -77,8 +64,6 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { | |
public init(viewMode: SyntaxTreeViewMode = .sourceAccurate, arena: SyntaxArena? = nil) { | ||
self.viewMode = viewMode | ||
self.arena = arena | ||
self.recyclableNodeInfos = [] | ||
self.recyclableNodeInfos.reserveCapacity(64) | ||
} | ||
""" | ||
) | ||
|
@@ -353,15 +338,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { | |
} | ||
|
||
// Build the Syntax node to rewrite | ||
var childNode: Syntax | ||
if !recyclableNodeInfos.isEmpty { | ||
let recycledInfo: Syntax.Info = recyclableNodeInfos.removeLast()! | ||
recycledInfo.info = .nonRoot(.init(parent: Syntax(node), absoluteInfo: info)) | ||
childNode = Syntax(child, info: recycledInfo) | ||
} else { | ||
let absoluteRaw = AbsoluteRawSyntax(raw: child, info: info) | ||
childNode = Syntax(absoluteRaw, parent: syntaxNode) | ||
} | ||
var childNode = nodeFactory.create(parent: syntaxNode, raw: child, absoluteInfo: info) | ||
|
||
dispatchVisit(&childNode) | ||
if childNode.raw.id != child.id { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously it compared I.e. returning |
||
|
@@ -392,14 +369,8 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) { | |
} | ||
} | ||
|
||
if recyclableNodeInfos.capacity > recyclableNodeInfos.count, | ||
isKnownUniquelyReferenced(&childNode.info) { | ||
var info: Syntax.Info! = nil | ||
// 'swap' to avoid ref-counting traffic. | ||
swap(&childNode.info, &info) | ||
info.info = nil | ||
recyclableNodeInfos.append(info) | ||
} | ||
// Recycle 'childNode.info' | ||
nodeFactory.dispose(&childNode) | ||
} | ||
|
||
if let newLayout { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Reusable 'Syntax.Info' storage. | ||
private struct SyntaxInfoRepository { | ||
private final class _Buffer: ManagedBuffer<Int, Syntax.Info?> { | ||
deinit { | ||
self.withUnsafeMutablePointers { headerPtr, elementPtr in | ||
_ = elementPtr.deinitialize(count: headerPtr.pointee) | ||
} | ||
} | ||
} | ||
|
||
/// Fixed capacity which is enough for most use cases. | ||
static var capacity: Int { 64 } | ||
|
||
private let buffer: _Buffer | ||
|
||
init() { | ||
let buffer = _Buffer.create(minimumCapacity: Self.capacity, makingHeaderWith: { _ in 0 }) | ||
self.buffer = buffer as! _Buffer | ||
} | ||
|
||
/// Take the 'Syntax.Info' object from the address. | ||
func push(_ info: UnsafeMutablePointer<Syntax.Info?>) { | ||
buffer.withUnsafeMutablePointers { headerPtr, elementPtr in | ||
guard headerPtr.pointee < Self.capacity else { | ||
return | ||
} | ||
assert(info.pointee != nil, "tried to push 'nil' info") | ||
elementPtr.advanced(by: headerPtr.pointee).moveInitialize(from: info, count: 1) | ||
info.initialize(to: nil) | ||
headerPtr.pointee += 1 | ||
} | ||
} | ||
|
||
/// Vend a 'Swift.Info' object if available. | ||
func pop() -> Syntax.Info? { | ||
return buffer.withUnsafeMutablePointers { headerPtr, elementPtr in | ||
guard headerPtr.pointee > 0 else { | ||
return nil | ||
} | ||
headerPtr.pointee -= 1 | ||
return elementPtr.advanced(by: headerPtr.pointee).move() | ||
} | ||
} | ||
} | ||
|
||
/// 'Syntax' object factory. This may hold some stocks of recycled 'Syntax.Info'. | ||
struct SyntaxNodeFactory { | ||
private let syntaxInfoRepo: SyntaxInfoRepository = SyntaxInfoRepository() | ||
|
||
/// Create a 'Syntax' instance using the supplied info. | ||
/// | ||
/// If this factory has a recycled 'Syntax.Info', use one of them. Otherwise, just create a instance by allocating a new one. | ||
@inline(__always) | ||
func create(parent: Syntax, raw: RawSyntax, absoluteInfo: AbsoluteSyntaxInfo) -> Syntax { | ||
if let info = syntaxInfoRepo.pop() { | ||
info.info = .nonRoot(.init(parent: parent, absoluteInfo: absoluteInfo)) | ||
return Syntax(raw, info: info) | ||
} else { | ||
return Syntax(raw, parent: parent, absoluteInfo: absoluteInfo) | ||
} | ||
} | ||
|
||
/// Dispose a 'Syntax' object. | ||
/// | ||
/// 'node.info' is collected for future reuse. 'node' is not usable after calling this. | ||
@inline(__always) | ||
func dispose(_ node: inout Syntax) { | ||
if isKnownUniquelyReferenced(&node.info) { | ||
node.info.unsafelyUnwrapped.info = nil | ||
syntaxInfoRepo.push(&node.info) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.