Skip to content

Commit

Permalink
Boost speed in circumstance, which updates a few property or creates …
Browse files Browse the repository at this point in the history
…with a part of dictionary. #197
  • Loading branch information
wolfcon committed Jan 12, 2022
1 parent 2317eb7 commit d82b12b
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 300 deletions.
3 changes: 3 additions & 0 deletions MJExtension/MJEClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ NS_ASSUME_NONNULL_BEGIN
NSArray<MJProperty *> * _Nullable _allCodingProperties;

NSArray<MJProperty *> * _Nullable _allProperties2JSON;
NSDictionary <NSString *, MJProperty *> *_mapper;
NSArray<MJProperty *> *_multiKeysProperties;


BOOL _hasOld2NewModifier;
BOOL _hasDictionary2ObjectModifier;
Expand Down
15 changes: 14 additions & 1 deletion MJExtension/MJEClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ - (void)mj_handlePropertiesWithAllowedList:(NSSet *)allowedList
NSMutableArray<MJProperty *> *allProperties = NSMutableArray.array;
NSMutableArray<MJProperty *> *codingProperties = NSMutableArray.array;
NSMutableArray<MJProperty *> *allProperties2JSON = NSMutableArray.array;
NSMutableDictionary *mapper = NSMutableDictionary.dictionary;
NSMutableArray<MJProperty *> *multiKeysProperties = NSMutableArray.array;

[cls mj_enumerateClasses:^(__unsafe_unretained Class c, BOOL *stop) {
// 1. get all property list
unsigned int outCount = 0;
Expand Down Expand Up @@ -252,6 +255,14 @@ - (void)mj_handlePropertiesWithAllowedList:(NSSet *)allowedList

// handle keypath / keypath array / keypath array(with subkey)
[property handleOriginKey:key];

// The property matched with a singular key is the only condition for dictionary enumeration.
if (property->_isMultiMapping) {
[multiKeysProperties addObject:property];
} else {
property->_nextSame = mapper[property->_mappedKey] ?: nil;
mapper[property->_mappedKey] = property;
}
}

[allProperties addObject:property];
Expand All @@ -264,7 +275,9 @@ - (void)mj_handlePropertiesWithAllowedList:(NSSet *)allowedList
_allProperties = allProperties.copy;
_allCodingProperties = codingProperties.copy;
_allProperties2JSON = allProperties2JSON.copy;

_mapper = mapper.copy;
_multiKeysProperties = multiKeysProperties.copy;

_propertiesCount = _allProperties.count;
}

Expand Down
2 changes: 2 additions & 0 deletions MJExtension/MJProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ NS_ASSUME_NONNULL_BEGIN
MJEBasicType _basicObjectType;
/// True if property is a number (e.g: bool, double, int, etc.).
BOOL _isBasicNumber;
/// 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.
MJProperty * _Nullable _nextSame;
}

/// `Property` name that defined by class.
Expand Down
Loading

0 comments on commit d82b12b

Please sign in to comment.