-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[benchmark] Add ReplaceSubrange benchmark #25310
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
Changes from all commits
0f6d56c
e67c8b1
459861b
4d86f3f
082285f
0de61f0
4c0ea56
b82f6c5
b76169c
a9cbe2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//===--- StringReplaceSubrange.swift -------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import TestsUtils | ||
|
||
let tags: [BenchmarkCategory] = [.validation, .api, .String] | ||
|
||
public let StringReplaceSubrange = [ | ||
BenchmarkInfo( | ||
name: "String.replaceSubrange.String.Small", | ||
runFunction: { replaceSubrange($0, smallString, with: "t") }, | ||
tags: tags | ||
), | ||
BenchmarkInfo( | ||
name: "String.replaceSubrange.String", | ||
runFunction: { replaceSubrange($0, largeString, with: "t") }, | ||
tags: tags | ||
), | ||
BenchmarkInfo( | ||
name: "String.replaceSubrange.Substring.Small", | ||
runFunction: { replaceSubrange($0, smallString, with: "t"[...]) }, | ||
tags: tags | ||
), | ||
BenchmarkInfo( | ||
name: "String.replaceSubrange.Substring", | ||
runFunction: { replaceSubrange($0, largeString, with: "t"[...]) }, | ||
tags: tags | ||
), | ||
BenchmarkInfo( | ||
name: "String.replaceSubrange.ArrChar.Small", | ||
runFunction: { replaceSubrange($0, smallString, with: arrayCharacter) }, | ||
tags: tags | ||
), | ||
BenchmarkInfo( | ||
name: "String.replaceSubrange.ArrChar", | ||
runFunction: { replaceSubrange($0, largeString, with: arrayCharacter) }, | ||
tags: tags | ||
), | ||
BenchmarkInfo( | ||
name: "String.replaceSubrange.RepChar.Small", | ||
runFunction: { replaceSubrange($0, smallString, with: repeatedCharacter) }, | ||
tags: tags | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The benchmark name "Str.replaceSubrange.SmallLiteral.RepeatedChar" is longer than 40 characters, but I couldn't think a better name fitting 40. Maybe it can be like "Str.replaceSubrange.LargeManagedRepChar", but I was concerned "RepChar" is a little bit hard to understand that it means There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at what @milseman writes in SR-8905:
I'd say the naming convention calls for base name of
The longest one is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an awesome naming idea. I will use them. Thanks for your suggestion! |
||
), | ||
BenchmarkInfo( | ||
name: "String.replaceSubrange.RepChar", | ||
runFunction: { replaceSubrange($0, largeString, with: repeatedCharacter) }, | ||
tags: tags | ||
), | ||
] | ||
|
||
let smallString = "coffee" | ||
let largeString = "coffee\u{301}coffeecoffeecoffeecoffee" | ||
|
||
let arrayCharacter = Array<Character>(["t"]) | ||
let repeatedCharacter = repeatElement(Character("t"), count: 1) | ||
|
||
@inline(never) | ||
private func replaceSubrange<C: Collection>( | ||
_ N: Int, _ string: String, with newElements: C | ||
) where C.Element == Character { | ||
var copy = getString(string) | ||
let range = string.startIndex..<string.index(after: string.startIndex) | ||
for _ in 0 ..< 500 * N { | ||
copy.replaceSubrange(range, with: newElements) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind explaining what is the difference between the small literal string vs the large managed string? Is this something related to this small string optimization? If the string fits 15 ASCII characters length, it won't be allocated in the heap memory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. See:
_SmallString
and_StringGuts
for implementation details if you're interested.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the links! I will take a look at them π
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small form accommodates 15 UTF-8 code units in length (not just ASCII)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for clarifying the length of the small string, Michael π