Skip to content

Commit 19a9cd7

Browse files
committed
[Macros] Cache SourceLocationConverter in ExportedSourceFile
We need a `SourceLocationConverter` every time we create a `PluginMessage.Syntax` to know the source location of that syntax node within the source file. This means that we needed to re-build the line table of the entire source file multiple times for every macro that we expand. Cache it to improve performance. rdar://119047550
1 parent bf102d2 commit 19a9cd7

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/ASTGen/Sources/ASTGen/PluginHost.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ extension PluginMessage.Syntax {
389389
let sourceStr = String(decoding: sourceFilePtr.pointee.buffer, as: UTF8.self)
390390
let fileName = sourceFilePtr.pointee.fileName
391391
let fileID = "\(sourceFilePtr.pointee.moduleName)/\(sourceFilePtr.pointee.fileName.basename)"
392-
let converter = SourceLocationConverter(file: fileName, source: sourceStr)
393-
let loc = converter.location(for: syntax.position)
392+
let loc = sourceFilePtr.pointee.sourceLocationConverter.location(for: syntax.position)
394393

395394
self.init(
396395
kind: kind,

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public struct ExportedSourceFile {
3232
/// The syntax tree for the complete source file.
3333
public let syntax: SourceFileSyntax
3434

35+
/// A source location converter to convert `AbsolutePosition`s in `syntax` to line/column locations.
36+
///
37+
/// Cached so we don't need to re-build the line table every time we need to convert a position.
38+
let sourceLocationConverter: SourceLocationConverter
39+
3540
public func position(of location: BridgedSourceLoc) -> AbsolutePosition? {
3641
let sourceFileBaseAddress = UnsafeRawPointer(buffer.baseAddress!)
3742
guard let opaqueValue = location.getOpaquePointerValue() else {
@@ -80,7 +85,8 @@ public func parseSourceFile(
8085
buffer: buffer,
8186
moduleName: String(cString: moduleName),
8287
fileName: String(cString: filename),
83-
syntax: sourceFile
88+
syntax: sourceFile,
89+
sourceLocationConverter: SourceLocationConverter(fileName: String(cString: filename), tree: sourceFile)
8490
)
8591
)
8692

0 commit comments

Comments
 (0)