• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Adding more and more key/value observers is much too slow - workaround needed.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding more and more key/value observers is much too slow - workaround needed.


  • Subject: Re: Adding more and more key/value observers is much too slow - workaround needed.
  • From: Andy Lee <email@hidden>
  • Date: Wed, 12 Aug 2009 14:15:18 -0400

On Aug 12, 2009, at 11:37 AM, Christopher Kane wrote:
Don't pass the observer as the context: argument; pretty much anything else is better.

Out of curiosity -- why would this affect performance and why is this a bad idea (other than performance)? Isn't the context: treated as an opaque pointer by KVO?


--Andy


Passing NULL (just, for example) flattens the curve quite a bit. If you have nothing better to pass in, put this in your code and pass the address of it:

static char _xyzzy_ = 0;


Chris Kane Cocoa Frameworks, Apple


On Aug 3, 2009, at 6:51 AM, Andreas Känner wrote:

Hi,

If you add more and more key/value observers with addObserver:forKeyPath:options:context: this call takes longer and longer in a non-linear way.

I posted a bug report today (ID: 7112953) but maybe someone already has a workaround. Here is my sample code:

#include <Foundation/Foundation.h>

@interface ModelObject : NSObject
{
   NSString* title;
}
@property (readwrite, copy) NSString* title;
@end

@implementation ModelObject
@synthesize title;
@end

@interface Observer : NSObject
{}
@end

@implementation Observer
@end

void addObservers(NSUInteger count)
{
	// Create model objects and observers:

NSMutableArray* modelObjects = [NSMutableArray arrayWithCapacity:count];
NSMutableArray* observers = [NSMutableArray arrayWithCapacity:count];
for(NSUInteger i=0; i<count; i++)
{
[modelObjects addObject:[[ModelObject alloc] init]];
[observers addObject:[[Observer alloc] init]];
}


	// Register observers:

NSDate* startDate = [NSDate date];
for(NSUInteger i=0; i<count; i++)
{
ModelObject* modelObject = [modelObjects objectAtIndex:i];
Observer* observer = [observers objectAtIndex:i];
[modelObject addObserver:observer
forKeyPath:@"title"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:observer];


}
NSLog(@"time to add %d observers: %f s", count, -[startDate timeIntervalSinceNow]);
}




int main (int argc, const char * argv[])
{
	NSAutoreleasePool* pool = [NSAutoreleasePool new];

	addObservers(10);
	addObservers(100);
	addObservers(1000);
	addObservers(10000);
	[pool release];
}

In my test case an observer does always observe only one object and each observed object is only observed by one observer. I think this is a common use case.

I have results for Snow Leopard too, but I think I'm not allowed to post them on this mailing list. Here are the results for Leopard:

I tested this with different build settings (with/without GC and 32/64bit)

Machine: iMac 2.4 GHz Intel Core 2 Duo

Leopard (10.5.7/9J61):

32Bit:

With GC:

time to add    10 observers: 0.001008 s
time to add   100 observers: 0.001796 s
time to add  1000 observers: 0.174864 s
time to add 10000 observers: 10.746330 s

Without GC:

time to add    10 observers: 0.000920 s
time to add   100 observers: 0.000956 s
time to add  1000 observers: 0.056894 s
time to add 10000 observers: 4.615175 s

64Bit:

With GC:

time to add    10 observers: 0.001340 s
time to add   100 observers: 0.001533 s
time to add  1000 observers: 0.125700 s
time to add 10000 observers: 7.545702 s

Without GC:

time to add    10 observers: 0.001058 s
time to add   100 observers: 0.000831 s
time to add  1000 observers: 0.053189 s
time to add 10000 observers: 4.006754 s

Notes:

If you know a workaround for this bottleneck please drop me an email.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Adding more and more key/value observers is much too slow - workaround needed.
      • From: Chris Kane <email@hidden>
References: 
 >Adding more and more key/value observers is much too slow - workaround needed. (From: Andreas Känner <email@hidden>)
 >Re: Adding more and more key/value observers is much too slow - workaround needed. (From: Christopher Kane <email@hidden>)

  • Prev by Date: Re: NSOulineView scroll position
  • Next by Date: Re: FSDetermineIfRefIsEnclosedByFolder(): No const for Cocoa's Temp Dir?
  • Previous by thread: Re: Adding more and more key/value observers is much too slow - workaround needed.
  • Next by thread: Re: Adding more and more key/value observers is much too slow - workaround needed.
  • Index(es):
    • Date
    • Thread