Skip to content

Commit 72baf02

Browse files
author
Harlan Haskins
authored
Significantly improve prefix trie performance and memory usage (#109)
* Significantly improve prefix trie performance and memory usage The previous prefix trie made a new `Node` for each character along the input string. In addition to being wasteful, this would introduce many more hops to match a given string. Instead, store common `Substring`s in the `Node`s, and use a sorted array/binary search through children to avoid extra time spend hashing and storing in hashed collections. Doing this reduces the number of nodes in the trie for the driver's options by an order of magnitude -- from 6601 nodes to 660. (This is actually a port of LLVM's Trie, but cleaned up and made more idiomatic in Swift) * Remove testCollectionMatching from LinuxMain
1 parent dfaa51f commit 72baf02

File tree

4 files changed

+370
-115
lines changed

4 files changed

+370
-115
lines changed

Sources/SwiftOptions/OptionParsing.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ extension OptionTable {
3737
/// Throws an error if the command line contains any errors.
3838
public func parse(_ arguments: [String],
3939
for driverKind: DriverKind) throws -> ParsedOptions {
40-
var trie = PrefixTrie<String.UTF8View, Option>()
40+
var trie = PrefixTrie<Option>()
4141
for opt in options {
42-
trie[opt.spelling.utf8] = opt
42+
trie[opt.spelling] = opt
4343
}
4444

4545
var parsedOptions = ParsedOptions()
@@ -71,7 +71,7 @@ extension OptionTable {
7171
// match -- if the option is a `.flag`, we'll explicitly check to see if
7272
// there's an unmatched suffix at the end, and pop an error. Otherwise,
7373
// we'll treat the unmatched suffix as the argument to the option.
74-
guard let option = trie[argument.utf8] else {
74+
guard let option = trie[argument] else {
7575
throw OptionParseError.unknownOption(
7676
index: index - 1, argument: argument)
7777
}

0 commit comments

Comments
 (0)