mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Improve StringEdits performance in absence of CharacterView
CharacterView was not Hashable, so Set could not be used as an accumulator. String and Substring are Hashable, but using a Set as an accumulator is still slower than first collecting all the results in an Array and then transforming it to a Set at the end. One possible reason why that could be the case, is that by the time conversion happens, we already know the capacity and thus will not re-allocate and re-hash a Set on every insertion beyong current capacity.
This commit is contained in:
@@ -31,14 +31,11 @@ var editWords: [String] = [
|
||||
let alphabet = "abcdefghijklmnopqrstuvwxyz"
|
||||
/// All edits that are one edit away from `word`
|
||||
func edits(_ word: String) -> Set<String> {
|
||||
// create right/left splits as CharacterViews instead
|
||||
let splits = word.indices.map {
|
||||
(word[..<$0], word[$0...])
|
||||
(String(word[..<$0]), String(word[$0...]))
|
||||
}
|
||||
|
||||
// though it should be, CharacterView isn't hashable
|
||||
// so using an array for now, ignore that aspect...
|
||||
var result: Array<Substring> = []
|
||||
var result: Array<String> = []
|
||||
|
||||
for (left, right) in splits {
|
||||
// drop a character
|
||||
@@ -64,7 +61,7 @@ func edits(_ word: String) -> Set<String> {
|
||||
}
|
||||
|
||||
// have to map back to strings right at the end
|
||||
return Set(result.lazy.map(String.init))
|
||||
return Set(result)
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
|
||||
Reference in New Issue
Block a user