我想写一个经典的缓存数据结构的Swift版本,它使用一个数组直到某个可配置的限制,然后切换到map [2]。 不幸的是,我在编写甚至更简单的数据结构时所获得的性能特征使我认为此刻无法立即实现。
考虑这段代码:
import Foundation
let aKey = 196
var cache: [(Int, String)] = []
for _ in 0..<1_000_000_000
{
var t: String?
for (k, v) in cache
{
if k == aKey
{
t = v
break
}
}
if t == nil
{
let str = "some cache value with key=\(aKey)"
cache.append((aKey, str))
t = str
}
}
这大约需要0.785s来执行。
另一个程序,应该大致等效:
import Foundation
struct DummyCache<K:Hashable, V>
{
var array:[(K, V?)] = []
subscript(index: K) -> V?
{
get
{
for (k, v) in array
{
if k == index
{
return v
}
}
return nil
}
set(newValue)
{
array.append((index, newValue))
}
}
}
let aKey = 196
var cache: DummyCache<Int, String> = DummyCache()
cache[aKey] = "some cache value with key=\(aKey)"
for _ in 0..<1_000_000_000
{
var t: String? = cache[aKey]
}
实际上慢了45倍! (大约35.532s)
分析此代码时,我可以看到大部分时间都花在了swift运行时中:
有没有办法改善此性能?
[2]作为记录,这是我想到的数据结构:
import Foundation
struct Cache<K:Hashable, V>
{
let limit: Int
var array:[(K, V)]? = nil
var map: [K:V]? = nil
let buildValueFromKey: (K) -> V
init(limit: Int = 20, buildValueFromKey:@escaping (K) -> V) //optimal limit depends, must conduct tests to find optimal value
{
self.limit = limit
self.buildValueFromKey = buildValueFromKey
}
subscript(index: K) -> V
{
mutating get
{
if var map = map
{
if let res = map[index]
{
return res
}
else
{
let res = buildValueFromKey(index)
map[index] = res
return res
}
}
else //array mode
{
if array == nil
{
let res = buildValueFromKey(index)
array = [(index, res)]
return res
}
//else
for (k, v) in array! //can't be null at that point
{
if k == index
{
return v
}
}
// array does not contain index at this point, create & append it
let res = buildValueFromKey(index)
array!.append((index, res))
return res
}
}
}
}
let aKey = 196
var cache: Cache<Int, String> = Cache{ "some cache value with key=\($0)" }
for _ in 0..<1_000_000_000
{
var t: String? = cache[aKey]
}