Skip to content

Add new benchmarks for NSString bridging to cover non-tagged cases #23433

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
Show file tree
Hide file tree
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
57 changes: 51 additions & 6 deletions benchmark/single-source/NSStringConversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,64 @@
//===----------------------------------------------------------------------===//

// <rdar://problem/19003201>
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)

import TestsUtils
import Foundation

public let NSStringConversion = BenchmarkInfo(
name: "NSStringConversion",
runFunction: run_NSStringConversion,
tags: [.validation, .api, .String, .bridging])
fileprivate var test:NSString = ""

public let NSStringConversion = [
BenchmarkInfo(name: "NSStringConversion",
runFunction: run_NSStringConversion,
tags: [.validation, .api, .String, .bridging]),
BenchmarkInfo(name: "NSStringConversion.UTF8",
runFunction: run_NSStringConversion_nonASCII,
tags: [.validation, .api, .String, .bridging],
setUpFunction: { test = NSString(cString: "tëst", encoding: String.Encoding.utf8.rawValue)! }),
BenchmarkInfo(name: "NSStringConversion.Mutable",
runFunction: run_NSMutableStringConversion,
tags: [.validation, .api, .String, .bridging],
setUpFunction: { test = NSMutableString(cString: "test", encoding: String.Encoding.ascii.rawValue)! }),
BenchmarkInfo(name: "NSStringConversion.Long",
runFunction: run_NSStringConversion_long,
tags: [.validation, .api, .String, .bridging],
setUpFunction: { test = NSString(cString: "The quick brown fox jumps over the lazy dog", encoding: String.Encoding.ascii.rawValue)! } ),
BenchmarkInfo(name: "NSStringConversion.LongUTF8",
runFunction: run_NSStringConversion_longNonASCII,
tags: [.validation, .api, .String, .bridging],
setUpFunction: { test = NSString(cString: "Thë qüick bröwn föx jumps over the lazy dög", encoding: String.Encoding.utf8.rawValue)! })]

public func run_NSStringConversion(_ N: Int) {
#if _runtime(_ObjC)
let test:NSString = NSString(cString: "test", encoding: String.Encoding.ascii.rawValue)!
for _ in 1...N * 10000 {
//Doesn't test accessing the String contents to avoid changing historical benchmark numbers
blackHole(identity(test) as String)
}
#endif
}

fileprivate func innerLoop(_ str: NSString, _ N: Int, _ scale: Int = 5000) {
for _ in 1...N * scale {
for char in (identity(str) as String).utf8 {
blackHole(char)
}
}
}

public func run_NSStringConversion_nonASCII(_ N: Int) {
innerLoop(test, N, 2500)
}

public func run_NSMutableStringConversion(_ N: Int) {
innerLoop(test, N)
}

public func run_NSStringConversion_long(_ N: Int) {
innerLoop(test, N, 1000)
}

public func run_NSStringConversion_longNonASCII(_ N: Int) {
innerLoop(test, N, 300)
}

#endif
4 changes: 4 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ import MonteCarloPi
import NibbleSort
import NSDictionaryCastToSwift
import NSError
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import NSStringConversion
#endif
import NopDeinit
import ObjectAllocation
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
Expand Down Expand Up @@ -262,7 +264,9 @@ registerBenchmark(MonteCarloE)
registerBenchmark(MonteCarloPi)
registerBenchmark(NSDictionaryCastToSwift)
registerBenchmark(NSErrorTest)
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
registerBenchmark(NSStringConversion)
#endif
registerBenchmark(NibbleSort)
registerBenchmark(NopDeinit)
registerBenchmark(ObjectAllocation)
Expand Down