Skip to content

Add benchmarks for creating Strings from ASCII #27665

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 1 commit into from
Oct 14, 2019
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
34 changes: 34 additions & 0 deletions benchmark/single-source/UTF8Decode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public let UTF8Decode = [
name: "UTF8Decode_InitFromBytes_ascii",
runFunction: run_UTF8Decode_InitFromBytes_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromData_ascii_as_ascii",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, the names aren't great here. Shouldn't we call this ASCIIDecode.initFromData?

The existing benchmarks have set an unfortunate pattern, but perhaps it's worth breaking it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt keeping the prefix the same was worthwhile for being able to do something like "run all the benchmarks for X". The name does really suck though.

runFunction: run_UTF8Decode_InitFromData_ascii_as_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitDecoding_ascii_as_ascii",
runFunction: run_UTF8Decode_InitDecoding_ascii_as_ascii,
tags: [.validation, .api, .String]),
BenchmarkInfo(
name: "UTF8Decode_InitFromBytes_ascii_as_ascii",
runFunction: run_UTF8Decode_InitFromBytes_ascii_as_ascii,
tags: [.validation, .api, .String]),
]

// 1-byte sequences
Expand Down Expand Up @@ -129,4 +141,26 @@ public func run_UTF8Decode_InitFromBytes_ascii(_ N: Int) {
}
}

@inline(never)
public func run_UTF8Decode_InitFromData_ascii_as_ascii(_ N: Int) {
let input = asciiData
for _ in 0..<1_000*N {
blackHole(String(data: input, encoding: .ascii))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input stays constant over the duration of the for statement, so I wonder if a sufficiently clever optimizer would be able to move some of its processing outside the loop.

It may be worth passing input through identity. (Then again, the other benchmarks don't do this, and we should stay consistent if we want to be able to compare results,)

}
}
@inline(never)
public func run_UTF8Decode_InitDecoding_ascii_as_ascii(_ N: Int) {
let input = asciiBytes
for _ in 0..<1_000*N {
blackHole(String(decoding: input, as: Unicode.ASCII.self))
}
}
@inline(never)
public func run_UTF8Decode_InitFromBytes_ascii_as_ascii(_ N: Int) {
let input = asciiBytes
for _ in 0..<1_000*N {
blackHole(String(bytes: input, encoding: .ascii))
}
}