You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SourceLocationConverter has 2 initializers, one uses a String and the other uses a SourceFileSyntax. Based on my testing, the String based initializer is noticeably faster than using a SourceFileSyntax.
For a test case, I selected a relatively large file from swift-syntax (~ 10k LOC). The initializer that accepts a string takes ~ 2ms while the other initializer takes ~ 70 ms.
The text was updated successfully, but these errors were encountered:
I was able to reproduce this to some degree with the following test:
import SwiftSyntax
import SwiftParser
import Foundation
letstr=try!String(contentsOfFile:"/Users/alex/Downloads/SR-12972/SyntaxBuilders.swift")lettree=try!Parser.parse(source: str)// Warm up caches
for _ in 0..<10{
_ =SourceLocationConverter(file:"", source: str)}
for _ in 0..<10{
_ =SourceLocationConverter(file:"", tree: tree)}do{letstart=Date()
for _ in 0..<100{
_ =SourceLocationConverter(file:"", source: str)}print("String based initializer took: \(Date().timeIntervalSince(start)/100*1000) ms")}do{letstart=Date()
for _ in 0..<100{
_ =SourceLocationConverter(file:"", tree: tree)}print("Tree-based initializer took: \(Date().timeIntervalSince(start)/100*1000) ms")}
The output was as follows:
String based initializer took: 11.692179441452026 ms
Tree-based initializer took: 35.53478002548218 ms
This matches my expectations because the tree needs to first be converted into a source string before SourceLocationConverter can build up its line table. I suspect the original discrepancy was larger because the reporter was using a debug build of SwiftSyntax.
I’m closing this as correct behavior. If the performance of SourceLocationConverter is a bottleneck for someone in release builds, we can re-consider creating an optimized initializer for SourceLocationConverter that doesn’t convert the tree into a string first.
Attachment: Download
Additional Detail from JIRA
md5: 4656fb002ec2b67ed48c8aa22d56eebf
Issue Description:
SourceLocationConverter has 2 initializers, one uses a String and the other uses a SourceFileSyntax. Based on my testing, the String based initializer is noticeably faster than using a SourceFileSyntax.
For a test case, I selected a relatively large file from swift-syntax (~ 10k LOC). The initializer that accepts a string takes ~ 2ms while the other initializer takes ~ 70 ms.
The text was updated successfully, but these errors were encountered: