Skip to content

Commit d82b12b

Browse files
committed
Boost speed in circumstance, which updates a few property or creates with a part of dictionary. #197
1 parent 2317eb7 commit d82b12b

File tree

5 files changed

+344
-300
lines changed

5 files changed

+344
-300
lines changed

MJExtension/MJEClass.h

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ NS_ASSUME_NONNULL_BEGIN
1818
NSArray<MJProperty *> * _Nullable _allCodingProperties;
1919

2020
NSArray<MJProperty *> * _Nullable _allProperties2JSON;
21+
NSDictionary <NSString *, MJProperty *> *_mapper;
22+
NSArray<MJProperty *> *_multiKeysProperties;
23+
2124

2225
BOOL _hasOld2NewModifier;
2326
BOOL _hasDictionary2ObjectModifier;

MJExtension/MJEClass.m

+14-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ - (void)mj_handlePropertiesWithAllowedList:(NSSet *)allowedList
189189
NSMutableArray<MJProperty *> *allProperties = NSMutableArray.array;
190190
NSMutableArray<MJProperty *> *codingProperties = NSMutableArray.array;
191191
NSMutableArray<MJProperty *> *allProperties2JSON = NSMutableArray.array;
192+
NSMutableDictionary *mapper = NSMutableDictionary.dictionary;
193+
NSMutableArray<MJProperty *> *multiKeysProperties = NSMutableArray.array;
194+
192195
[cls mj_enumerateClasses:^(__unsafe_unretained Class c, BOOL *stop) {
193196
// 1. get all property list
194197
unsigned int outCount = 0;
@@ -252,6 +255,14 @@ - (void)mj_handlePropertiesWithAllowedList:(NSSet *)allowedList
252255

253256
// handle keypath / keypath array / keypath array(with subkey)
254257
[property handleOriginKey:key];
258+
259+
// The property matched with a singular key is the only condition for dictionary enumeration.
260+
if (property->_isMultiMapping) {
261+
[multiKeysProperties addObject:property];
262+
} else {
263+
property->_nextSame = mapper[property->_mappedKey] ?: nil;
264+
mapper[property->_mappedKey] = property;
265+
}
255266
}
256267

257268
[allProperties addObject:property];
@@ -264,7 +275,9 @@ - (void)mj_handlePropertiesWithAllowedList:(NSSet *)allowedList
264275
_allProperties = allProperties.copy;
265276
_allCodingProperties = codingProperties.copy;
266277
_allProperties2JSON = allProperties2JSON.copy;
267-
278+
_mapper = mapper.copy;
279+
_multiKeysProperties = multiKeysProperties.copy;
280+
268281
_propertiesCount = _allProperties.count;
269282
}
270283

MJExtension/MJProperty.h

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ NS_ASSUME_NONNULL_BEGIN
9595
MJEBasicType _basicObjectType;
9696
/// True if property is a number (e.g: bool, double, int, etc.).
9797
BOOL _isBasicNumber;
98+
/// The property has the same value with it. It's a linked list data structure for different property linked by the same key, which will result to get the same value.
99+
MJProperty * _Nullable _nextSame;
98100
}
99101

100102
/// `Property` name that defined by class.

0 commit comments

Comments
 (0)