From 5ce96b45bb8ca28f3424bf8ddfb06d8745e08af1 Mon Sep 17 00:00:00 2001 From: Ross Boucher Date: Sun, 26 Jul 2009 22:46:22 -0700 Subject: [PATCH] Slight performance improvement and memory improvement by moving the storage of KVO proxy objects. --- Foundation/CPKeyValueObserving.j | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/Foundation/CPKeyValueObserving.j b/Foundation/CPKeyValueObserving.j index 7a1d575ae..5c57449a2 100644 --- a/Foundation/CPKeyValueObserving.j +++ b/Foundation/CPKeyValueObserving.j @@ -58,7 +58,7 @@ if (!anObserver || !aPath) return; - [[KVOProxyMap objectForKey:[self UID]] _removeObserver:anObserver forKeyPath:aPath]; + [self[KVOProxyKey] _removeObserver:anObserver forKeyPath:aPath]; } + (BOOL)automaticallyNotifiesObserversForKey:(CPString)aKey @@ -98,12 +98,9 @@ CPKeyValueChangeInsertion = 2; CPKeyValueChangeRemoval = 3; CPKeyValueChangeReplacement = 4; -//convenience -var kvoNewAndOld = CPKeyValueObservingOptionNew|CPKeyValueObservingOptionOld; - -// Map of real objects to their KVO proxy -var KVOProxyMap = [CPDictionary dictionary], - DependentKeysMap = [CPDictionary dictionary]; +var kvoNewAndOld = CPKeyValueObservingOptionNew|CPKeyValueObservingOptionOld, + DependentKeysMap = [CPDictionary dictionary], + KVOProxyKey = "$KVOPROXY"; //rule of thumb: _ methods are called on the real proxy object, others are called on the "fake" proxy object (aka the real object) @@ -119,7 +116,7 @@ var KVOProxyMap = [CPDictionary dictionary], + (id)proxyForObject:(CPObject)anObject { - var proxy = [KVOProxyMap objectForKey:[anObject UID]]; + var proxy = anObject[KVOProxyKey]; if (proxy) return proxy; @@ -128,7 +125,7 @@ var KVOProxyMap = [CPDictionary dictionary], [proxy _replaceClass]; - [KVOProxyMap setObject:proxy forKey:[anObject UID]]; + anObject[KVOProxyKey] = proxy; return proxy; } @@ -205,9 +202,10 @@ var KVOProxyMap = [CPDictionary dictionary], } } - var affectingKeys = [[_nativeClass keyPathsForValuesAffectingValueForKey:aKey] allObjects]; + var affectingKeys = [[_nativeClass keyPathsForValuesAffectingValueForKey:aKey] allObjects], + affectingKeysCount = affectingKeys ? affectingKeys.length : 0; - if (![affectingKeys count]) + if (!affectingKeysCount) return; var dependentKeysForClass = [DependentKeysMap objectForKey:[_nativeClass UID]]; @@ -218,11 +216,9 @@ var KVOProxyMap = [CPDictionary dictionary], [DependentKeysMap setObject:dependentKeysForClass forKey:[_nativeClass UID]]; } - var count = [affectingKeys count]; - - while (count--) + while (affectingKeysCount--) { - var affectingKey = affectingKeys[count], + var affectingKey = affectingKeys[affectingKeysCount], affectedKeys = [dependentKeysForClass objectForKey:affectingKey]; if (!affectedKeys) @@ -288,7 +284,7 @@ var KVOProxyMap = [CPDictionary dictionary], if (![_observersForKey count]) { _targetObject.isa = _nativeClass; //restore the original class - [KVOProxyMap removeObjectForKey:[_targetObject UID]]; + delete _targetObject[KVOProxyKey]; } } @@ -360,7 +356,7 @@ var KVOProxyMap = [CPDictionary dictionary], } var observers = [[_observersForKey objectForKey:aKey] allValues], - count = [observers count]; + count = observers ? observers.length : 0; while (count--) { @@ -425,7 +421,7 @@ var KVOProxyMap = [CPDictionary dictionary], - (Class)class { - return [KVOProxyMap objectForKey:[self UID]]._nativeClass; + return self[KVOProxyKey]._nativeClass; } - (Class)superclass