Skip to content

Commit f1ab605

Browse files
author
Justin Meiners
committed
improve performance of prefixed column resolution
- Previously this allocated arrays every time it was accessed. It will now only allocate in one of the error cases. - It also keeps track of the columnName index for both key and value so it doesn't need to look it up twice.
1 parent 0ecc452 commit f1ab605

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

Sources/SQLite/Typed/Query.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,16 +1169,25 @@ public struct Row {
11691169
}
11701170

11711171
guard let idx = columnNames[column.template] else {
1172-
let similar = Array(columnNames.keys).filter { $0.hasSuffix(".\(column.template)") }
1173-
1174-
switch similar.count {
1175-
case 0:
1172+
func match(_ s: String) -> Bool {
1173+
return s.hasSuffix(".\(column.template)")
1174+
}
1175+
1176+
guard let firstIndex = columnNames.firstIndex(where: { match($0.key) }) else {
11761177
throw QueryError.noSuchColumn(name: column.template, columns: columnNames.keys.sorted())
1177-
case 1:
1178-
return valueAtIndex(columnNames[similar[0]]!)
1179-
default:
1180-
throw QueryError.ambiguousColumn(name: column.template, similar: similar)
11811178
}
1179+
1180+
let secondIndex = columnNames
1181+
.suffix(from: columnNames.index(after: firstIndex))
1182+
.firstIndex(where: { match($0.key) })
1183+
1184+
guard secondIndex == nil else {
1185+
throw QueryError.ambiguousColumn(
1186+
name: column.template,
1187+
similar: columnNames.keys.filter(match).sorted()
1188+
)
1189+
}
1190+
return valueAtIndex(columnNames[firstIndex].value)
11821191
}
11831192

11841193
return valueAtIndex(idx)

0 commit comments

Comments
 (0)