Don't pass the observer as the context: argument; pretty much anything else is better. 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/ckane%40apple.com

This email sent to ck...@apple.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to