Skip to content

Commit 0946a99

Browse files
committed
Optimization for ConvertibleConfig
1 parent 3227b96 commit 0946a99

File tree

3 files changed

+31
-90
lines changed

3 files changed

+31
-90
lines changed

KakaJSON/Convert/ConvertibleConfig.swift

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ public typealias JSONKeyConfig = (Property) -> JSONPropertyKey
1414
public typealias JSONValueConfig = (Any?, Property) -> Any?
1515

1616
/// Config for convertible
17+
///
18+
/// Suggest:
19+
/// 1. Set every config once for every type
20+
/// 2. Set config after app finish lauching
1721
public class ConvertibleConfig {
18-
private static var lock = pthread_rwlock_t()
19-
private static let initLock = {
20-
pthread_rwlock_init(&lock, nil)
21-
}()
22-
2322
private class Item {
2423
var modelKey: ModelKeyConfig?
2524
var modelValue: ModelValueConfig?
@@ -32,15 +31,11 @@ public class ConvertibleConfig {
3231
init(jsonValue: @escaping JSONValueConfig) { self.jsonValue = jsonValue }
3332
}
3433

35-
private static let global: Item = Item()
34+
private static let global = Item()
3635
private static var items: [TypeKey: Item] = [:]
3736

3837
/// get global config for modelKey
3938
public static func modelKey(from property: Property) -> ModelPropertyKey {
40-
let _ = initLock
41-
pthread_rwlock_rdlock(&lock)
42-
defer { pthread_rwlock_unlock(&lock) }
43-
4439
guard let fn = global.modelKey else { return property.name }
4540
return fn(property)
4641
}
@@ -54,10 +49,6 @@ public class ConvertibleConfig {
5449
/// get type's config for modelKey
5550
public static func modelKey(from property: Property,
5651
_ type: Convertible.Type) -> ModelPropertyKey {
57-
let _ = initLock
58-
pthread_rwlock_rdlock(&lock)
59-
defer { pthread_rwlock_unlock(&lock) }
60-
6152
if let fn = items[typeKey(type)]?.modelKey {
6253
return fn(property)
6354
}
@@ -72,17 +63,15 @@ public class ConvertibleConfig {
7263
}
7364
}
7465

75-
guard let fn = global.modelKey else { return property.name }
66+
guard let fn = global.modelKey else {
67+
return property.name
68+
}
7669
return fn(property)
7770
}
7871

7972
/// get global config for modelValue
8073
public static func modelValue(from jsonValue: Any?,
8174
_ property: Property) -> Any? {
82-
let _ = initLock
83-
pthread_rwlock_rdlock(&lock)
84-
defer { pthread_rwlock_unlock(&lock) }
85-
8675
guard let fn = global.modelValue else { return jsonValue }
8776
return fn(jsonValue, property)
8877
}
@@ -98,34 +87,32 @@ public class ConvertibleConfig {
9887
public static func modelValue(from jsonValue: Any?,
9988
_ property: Property,
10089
_ type: Convertible.Type) -> Any? {
101-
let _ = initLock
102-
pthread_rwlock_rdlock(&lock)
103-
defer { pthread_rwlock_unlock(&lock) }
104-
105-
if let fn = items[typeKey(type)]?.modelValue {
90+
let key = typeKey(type)
91+
if let fn = items[key]?.modelValue {
10692
return fn(jsonValue, property)
10793
}
10894

10995
let mt = Metadata.type(type)
11096
if var classMt = mt as? ClassType {
11197
while let superMt = classMt.super {
11298
if let fn = items[typeKey(superMt.type)]?.modelValue {
99+
items[key]?.modelValue = fn
113100
return fn(jsonValue, property)
114101
}
115102
classMt = superMt
116103
}
117104
}
118105

119-
guard let fn = global.modelValue else { return jsonValue }
106+
guard let fn = global.modelValue else {
107+
items[key]?.modelValue = { v, _ in v }
108+
return jsonValue
109+
}
110+
items[key]?.modelValue = fn
120111
return fn(jsonValue, property)
121112
}
122113

123114
/// get global config for JSONKey
124115
public static func JSONKey(from property: Property) -> JSONPropertyKey {
125-
let _ = initLock
126-
pthread_rwlock_rdlock(&lock)
127-
defer { pthread_rwlock_unlock(&lock) }
128-
129116
guard let fn = global.jsonKey else { return property.name }
130117
return fn(property)
131118
}
@@ -139,10 +126,6 @@ public class ConvertibleConfig {
139126
/// get type's config for JSONKey
140127
public static func JSONKey(from property: Property,
141128
_ type: Convertible.Type) -> JSONPropertyKey {
142-
let _ = initLock
143-
pthread_rwlock_rdlock(&lock)
144-
defer { pthread_rwlock_unlock(&lock) }
145-
146129
if let fn = items[typeKey(type)]?.jsonKey {
147130
return fn(property)
148131
}
@@ -157,17 +140,15 @@ public class ConvertibleConfig {
157140
}
158141
}
159142

160-
guard let fn = global.jsonKey else { return property.name }
143+
guard let fn = global.jsonKey else {
144+
return property.name
145+
}
161146
return fn(property)
162147
}
163148

164149
/// get global config for JSONValue
165150
public static func JSONValue(from modelValue: Any?,
166151
_ property: Property) -> Any? {
167-
let _ = initLock
168-
pthread_rwlock_rdlock(&lock)
169-
defer { pthread_rwlock_unlock(&lock) }
170-
171152
guard let fn = global.modelValue else { return modelValue }
172153
return fn(modelValue, property)
173154
}
@@ -183,25 +164,27 @@ public class ConvertibleConfig {
183164
public static func JSONValue(from modelValue: Any?,
184165
_ property: Property,
185166
_ type: Convertible.Type) -> Any? {
186-
let _ = initLock
187-
pthread_rwlock_rdlock(&lock)
188-
defer { pthread_rwlock_unlock(&lock) }
189-
190-
if let fn = items[typeKey(type)]?.jsonValue {
167+
let key = typeKey(type)
168+
if let fn = items[key]?.jsonValue {
191169
return fn(modelValue, property)
192170
}
193171

194172
let mt = Metadata.type(type)
195173
if var classMt = mt as? ClassType {
196174
while let superMt = classMt.super {
197175
if let fn = items[typeKey(superMt.type)]?.jsonValue {
176+
items[key]?.jsonValue = fn
198177
return fn(modelValue, property)
199178
}
200179
classMt = superMt
201180
}
202181
}
203182

204-
guard let fn = global.modelValue else { return modelValue }
183+
guard let fn = global.modelValue else {
184+
items[key]?.jsonValue = { v, _ in v}
185+
return modelValue
186+
}
187+
items[key]?.jsonValue = fn
205188
return fn(modelValue, property)
206189
}
207190

@@ -214,13 +197,6 @@ public class ConvertibleConfig {
214197
/// set types's config for modelKey
215198
public static func setModelKey(for types: [Convertible.Type] = [],
216199
_ modelKey: @escaping ModelKeyConfig) {
217-
let _ = initLock
218-
pthread_rwlock_wrlock(&lock)
219-
defer { pthread_rwlock_unlock(&lock) }
220-
221-
// clear model key cache
222-
Metadata.modelTypes.forEach { $0.clearModelKeys() }
223-
224200
if types.count == 0 {
225201
global.modelKey = modelKey
226202
return
@@ -245,10 +221,6 @@ public class ConvertibleConfig {
245221
/// set types's config for modelValue
246222
public static func setModelValue(for types: [Convertible.Type] = [],
247223
modelValue: @escaping ModelValueConfig) {
248-
let _ = initLock
249-
pthread_rwlock_wrlock(&lock)
250-
defer { pthread_rwlock_unlock(&lock) }
251-
252224
if types.count == 0 {
253225
global.modelValue = modelValue
254226
return
@@ -273,13 +245,6 @@ public class ConvertibleConfig {
273245
/// set types's config for jsonKey
274246
public static func setJSONKey(for types: [Convertible.Type] = [],
275247
jsonKey: @escaping JSONKeyConfig) {
276-
let _ = initLock
277-
pthread_rwlock_wrlock(&lock)
278-
defer { pthread_rwlock_unlock(&lock) }
279-
280-
// clear JSON key cache
281-
Metadata.modelTypes.forEach { $0.clearJSONKeys() }
282-
283248
if types.count == 0 {
284249
global.jsonKey = jsonKey
285250
return
@@ -304,10 +269,6 @@ public class ConvertibleConfig {
304269
/// set types's config for jsonValue
305270
public static func setJSONValue(for types: [Convertible.Type] = [],
306271
jsonValue: @escaping JSONValueConfig) {
307-
let _ = initLock
308-
pthread_rwlock_wrlock(&lock)
309-
defer { pthread_rwlock_unlock(&lock) }
310-
311272
if types.count == 0 {
312273
global.jsonValue = jsonValue
313274
return

KakaJSON/Metadata/Metadata.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import Foundation
1111
public struct Metadata {
1212
private static let typeLock = NSRecursiveLock()
1313
private static var types = [TypeKey: BaseType]()
14-
// type conform to Convertible
15-
private(set) static var modelTypes = [ModelType]()
1614

1715
public static func type(_ type: Any.Type) -> BaseType? {
1816
// get from cache
@@ -48,10 +46,6 @@ public struct Metadata {
4846
// ceate and put it into cache
4947
let mt = mtt.init(name: name, type: type, kind: kind)
5048
types[key] = mt
51-
if type is Convertible.Type,
52-
let modelType = mt as? ModelType {
53-
modelTypes.append(modelType)
54-
}
5549
return mt
5650
}
5751

KakaJSON/Metadata/Type/ModelType.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import Foundation
1111
public class ModelType: BaseType {
1212
public internal(set) var properties: [Property]?
1313
public internal(set) var genericTypes: [Any.Type]?
14-
private lazy var modelKeysLock = DispatchSemaphore(value: 1)
15-
private lazy var modelKeys: [String: ModelPropertyKey] = [:]
16-
private lazy var jsonKeysLock = DispatchSemaphore(value: 1)
17-
private lazy var jsonKeys: [String: String] = [:]
14+
private var modelKeysLock = DispatchSemaphore(value: 1)
15+
private var modelKeys: [String: ModelPropertyKey] = [:]
16+
private var jsonKeysLock = DispatchSemaphore(value: 1)
17+
private var jsonKeys: [String: String] = [:]
1818

1919
func modelKey(from propertyName: String,
2020
_ createdKey: @autoclosure () -> ModelPropertyKey) -> ModelPropertyKey {
@@ -29,13 +29,6 @@ public class ModelType: BaseType {
2929
return resultKey
3030
}
3131

32-
func clearModelKeys() {
33-
modelKeysLock.wait()
34-
defer { modelKeysLock.signal() }
35-
36-
modelKeys.removeAll()
37-
}
38-
3932
func JSONKey(from propertyName: String,
4033
_ createdKey: @autoclosure () -> String) -> String {
4134
if let key = jsonKeys[propertyName] { return key }
@@ -48,11 +41,4 @@ public class ModelType: BaseType {
4841
jsonKeys[propertyName] = resultKey
4942
return resultKey
5043
}
51-
52-
func clearJSONKeys() {
53-
jsonKeysLock.wait()
54-
defer { jsonKeysLock.signal() }
55-
56-
jsonKeys.removeAll()
57-
}
5844
}

0 commit comments

Comments
 (0)