2
2
3
3
import Foundation
4
4
5
- private let accessQueue = DispatchQueue ( label: " Memoization queue " , attributes: [ . concurrent] )
6
5
private let assignmentQueue = DispatchQueue (
7
6
label: " Memoization optimization assignment (for classes) " ,
8
7
attributes: [ . concurrent]
@@ -22,14 +21,14 @@ private func cachedValue<A, B>(
22
21
}
23
22
24
23
public func memoize< A: Hashable , B> ( _ f: @escaping ( A ) throws -> B ) -> ( A ) throws -> B {
25
- let cache = Atomic ( initialValue: [ A: B] ( ) , accessQueue : accessQueue )
24
+ let cache = Atomic ( initialValue: [ A: B] ( ) )
26
25
return { ( input: A ) -> B in
27
26
try cachedValue ( from: cache, for: input, fallback: f)
28
27
}
29
28
}
30
29
31
30
public func memoize< A: Hashable , B> ( _ f: @escaping ( A ) -> B ) -> ( A ) -> B {
32
- let cache = Atomic ( initialValue: [ A: B] ( ) , accessQueue : accessQueue )
31
+ let cache = Atomic ( initialValue: [ A: B] ( ) )
33
32
return { ( input: A ) -> B in
34
33
cachedValue ( from: cache, for: input, fallback: f)
35
34
}
@@ -41,7 +40,7 @@ private struct MemoizeParams2<A: Hashable, B: Hashable>: Hashable {
41
40
}
42
41
43
42
public func memoize< A: Hashable , B: Hashable , C> ( _ f: @escaping ( A , B ) -> C ) -> ( A , B ) -> C {
44
- let cache = Atomic ( initialValue: [ MemoizeParams2 < A , B > : C ] ( ) , accessQueue : accessQueue )
43
+ let cache = Atomic ( initialValue: [ MemoizeParams2 < A , B > : C ] ( ) )
45
44
return { ( a: A , b: B ) -> C in
46
45
cachedValue ( from: cache, for: MemoizeParams2 ( a: a, b: b) , fallback: { f ( $0. a, $0. b) } )
47
46
}
@@ -73,9 +72,9 @@ private class MemoizeParams3AClass<A: Hashable, B: Hashable, C: Hashable>: Hasha
73
72
74
73
static func == ( lhs: MemoizeParams3AClass , rhs: MemoizeParams3AClass ) -> Bool {
75
74
// This 🦃 performs a very specific optimization for the case when
76
- // we put the calculcations for the specific string (which has reference type) to the cache,
75
+ // we put the calculations for the specific string (which has reference type) to the cache,
77
76
// but _sometimes_ we re-create the instance of this string. Thus, if we don't modify
78
- // dictionary key, we'll always miss comparation by reference.
77
+ // dictionary key, we'll always miss comparison by reference.
79
78
// Here we rely on the implementation detail of Dictionary, namely we hope that
80
79
// `lhs` corresponds to the key _already contained_ in dictionary and `rhs` corresponds
81
80
// to the key by which we're trying to get the value.
@@ -96,7 +95,7 @@ public func memoize<
96
95
C: Hashable ,
97
96
D
98
97
> ( _ f: @escaping ( A , B , C ) -> D ) -> ( A , B , C ) -> D {
99
- let cache = Atomic ( initialValue: [ MemoizeParams3 < A , B , C > : D ] ( ) , accessQueue : accessQueue )
98
+ let cache = Atomic ( initialValue: [ MemoizeParams3 < A , B , C > : D ] ( ) )
100
99
return { ( a: A , b: B , c: C ) -> D in
101
100
cachedValue (
102
101
from: cache,
@@ -112,7 +111,7 @@ public func memoizeAClass<
112
111
C: Hashable ,
113
112
D
114
113
> ( _ f: @escaping ( A , B , C ) -> D ) -> ( A , B , C ) -> D where A: AnyObject {
115
- let cache = Atomic ( initialValue: [ MemoizeParams3AClass < A , B , C > : D ] ( ) , accessQueue : accessQueue )
114
+ let cache = Atomic ( initialValue: [ MemoizeParams3AClass < A , B , C > : D ] ( ) )
116
115
return { ( a: A , b: B , c: C ) -> D in
117
116
cachedValue (
118
117
from: cache,
0 commit comments