From 693add8df97fb1ab986ec0f25f964568f2305d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 14 Jan 2016 18:14:45 +0100 Subject: [PATCH] Drop too optimistic OptimisticDictionary --- GRDB/Core/Statement.swift | 38 ++++++-------------------------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index a560d1558..b7eea1e71 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -498,7 +498,7 @@ public struct StatementArguments { /// - parameter sequence: A sequence of (key, value) pairs /// - returns: A StatementArguments. public init(_ sequence: Sequence) { - kind = .NamedValues(OptimisticDictionary(sequence)) + kind = .NamedValues(Dictionary(sequence)) } public var isEmpty: Bool { @@ -515,7 +515,7 @@ public struct StatementArguments { enum Kind { case Values([DatabaseValueConvertible?]) - case NamedValues(OptimisticDictionary) + case NamedValues(Dictionary) } let kind: Kind @@ -572,38 +572,12 @@ extension StatementArguments : CustomStringConvertible { } -// MARK: - OptimisticDictionary - -/// OptimisticDictionary is faster than Dictionary at optimistic key lookup when -/// there are few keys. -/// -/// TODO: we need evidence to support that. -/// -/// Assuming this is true, this makes OptimisticDictionary a suitable storage -/// for named statement arguments, since there are usually not numberous, and -/// they usually match the names of arguments in the SQL query. -struct OptimisticDictionary: SequenceType /* Require Hashable key like Dictionary */ { - let pairs: [(Key, Value)] - +extension Dictionary { init(_ sequence: Sequence) { - pairs = Array(sequence) - } - - var isEmpty: Bool { - return pairs.isEmpty - } - - func generate() -> IndexingGenerator<[(Key, Value)]> { - return pairs.generate() - } - - subscript(key: Key) -> Value? { - for (k, value) in pairs { - if key == k { - return value - } + self.init(minimumCapacity: sequence.underestimateCount()) + for (key, value) in sequence { + self[key] = value } - return nil } }