Skip to content

Add a benchmark for bridging String(contentsOfFile:...), now with ava… #24377

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 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
35 changes: 35 additions & 0 deletions benchmark/single-source/ObjectiveCBridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public let ObjectiveCBridging = [
BenchmarkInfo(name: "ObjectiveCBridgeFromNSDateComponents",
runFunction: run_ObjectiveCBridgeFromNSDateComponents, tags: t,
setUpFunction: setup_dateComponents),
BenchmarkInfo(name: "ObjectiveCBridgeASCIIStringFromFile",
runFunction: run_ASCIIStringFromFile, tags: ts,
setUpFunction: setup_ASCIIStringFromFile),
]

#if _runtime(_ObjC)
Expand Down Expand Up @@ -709,3 +712,35 @@ public func run_ObjectiveCBridgeFromNSDateComponents(_ N: Int) {
}
#endif
}

var ASCIIStringFromFile:String? = nil
public func setup_ASCIIStringFromFile() {
#if _runtime(_ObjC)
let url:URL
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
url = FileManager.default.temporaryDirectory.appendingPathComponent(
"sphinx.txt"
)
} else {
url = URL(fileURLWithPath: "/tmp/sphinx.txt")
}
var str = "Sphinx of black quartz judge my vow"
str = Array(repeating: str, count: 100).joined()
try? str.write(
to: url,
atomically: true,
encoding: .ascii
)
ASCIIStringFromFile = try! String(contentsOf: url, encoding: .ascii)
#endif
}

@inline(never)
public func run_ASCIIStringFromFile(_ N: Int) {
#if _runtime(_ObjC)
for _ in 0 ..< N {
blackHole((ASCIIStringFromFile! + "").utf8.count)
}
#endif
}