Archive

Detecting conflicting Objective-C category methods

Objective-C categories are an extremely powerful way to add functionality to an existing class. Take for example, the category below that adds some stack-like methods to NSMutableArray:

@interface NSMutableArray (StackAdditions)
- (id)pop;
- (void)push:(id)object;
@end

@implementation NSMutableArray (StackAdditions)
- (id)pop
{
    id lastObject = [[[self lastObject] retain] autorelease];
    [self removeLastObject];
    return lastObject;
}

- (void)push:(id)object
{
    [self addObject:object];
}
@end

Now let’s say that I was writing a plugin for any app in Mac OS X that had a plugin architecture, such as IB, Aperture, Address Book, etc. Now, let’s also say that these apps had the same category methods on NSMutableArray, but they were slightly different, maybe they didn’t retain the object because of a special need they had. Well, when my plugin gets loaded into the app, the Objective-C runtime will auto-magically replace their category with mine because they have methods with the same name.

Obviously, this presents a problem. Now whenever their code calls -[NSMutableArray pop] or -[NSMutableArray push:], then the runtime will use my implementation instead. And if their category relied on some special happenings in their implementation, then the entire app could quickly fall into an unstable and fatal state.

So what’s to be done? If you are writing an application that has a plugin architecture or you are writing a plugin for an app, you should run the application during testing with the environment variable OBJC_PRINT_REPLACED_METHODS set to YES. What this does is it tells the Objective-C runtime to print out any methods it replaces when loading in a plugin. If you see that your implementation is being overwritten or you are overriding someone else’s, then you know that you will need to change the name of your method to something else, such as -[NSMutableArray myPop] instead of the original method -[NSMutableArray pop].

Pictures from Leslie’s visit are up

I just put up a few pictures from Leslie’s visit to the U of I this past weekend.

Click the image below to see them.

Leslie at the U of I

Attempted to upgrade Wordpress

A new version of Wordpress is out, and after attempting to update to this new and improved version, well… my site broke. So after some finagling with the server, I was able to restore the old version, luckily.

A weekend with Leslie

This weekend, Leslie came down to visit me here at the U of I. Boy, it was blessing to have her here.

She arrived Friday evening, and since we both had had long school days, watching LOST entertained us until bed time. She stayed with my friend Anne at Stratford House, which was certainly very nice of her to do.

Saturday was a day of… walking. Before brunch, we took a walk around the 6-pack and south quad. Then after brunch, we walked up to the Engineering Quad, up a little farther to see my apartment for next year, then all the way down to Oregon Avenue for coffee, and then down past Florida and Lincoln to the Arboretum. For a college in Illinois, the Arboretum was quite pleasant. It had some gardens, a lake, wooded areas, and a Japanese house used by the college for Japanese Education. After we were done with that all, we had to walk back.

In all, Leslie and I walked 7.28 miles, according to Google Earth. You can a trace of our trip below:
Our walk

Today we went to church at TCBC, had brunch at FlatTop Grill (I highly recommend it!), and then plan on spending the rest of our time watching LOST or reading Harry Potter before her bus at 5.

Mandarin Wok: Good Chinese food for a corn field

This evening I went to Mandarin Wok for some chow with my good friend Joey. You know, for being in the middle of a cornfield, the food was actually quite tasty. We ordered off of the Chinese menu, which partially meant that I had no idea what I would be receiving. We pointed to stuff, and in broken English, were told was it was… “Chick-in”, “Veggie-tables”. But whatever it was, it was good.

After the meal, I scurried off to a Computational Theory exam before having to walk the 25 minutes back in the freezing, torrential rains amidst a thunderstorm. A nice warm shower and clean clothes awaited me back at the dorm.

Thread synchronization implementation performance

Thread safety is certainly a very important factor in modern software design, especially as the number of cores increases per machine, and being able to run code concurrently becomes a requirement.

So I decided to see just how expensive using pthread_mutex_lock and semaphores are. The basic test idea is to see how many times you can access a critical section of code. I ran the same test for each synchronization implementation:

  1. No thread-safety.
  2. Thread safe using pthread_mutex_lock.
  3. Thread safe using sem_wait.

The code for each test is below:

void lazy_init_no_lock(void)
{
    static _Bool __isInitialized = 0;
    if (__isInitialized)
        __isInitialized = 1;

    __count++;
}

void lazy_init_lock(void)
{
    static _Bool __isInitialized = 0;

    pthread_mutex_lock(&__lock);
    if (__isInitialized)
        __isInitialized = 1;
    pthread_mutex_unlock(&__lock);

    __count++;
}

void lazy_init_semaphore(void)
{
    static _Bool __isInitialized = 0;

    sem_wait(&sem);
    if (__isInitialized)
        __isInitialized = 1;
    sem_post(&sem);

    __count++;
}

And the results were certainly much more interesting than the code. Pretty much, I called each test from an infinite loop for 5 seconds, and docked how many times I was able to complete the call.

    while(1)
    {
        if (test == 0)
            lazy_init_no_lock();
        else if (test == 1)
            lazy_init_lock();
        else if (test == 2)
            lazy_init_semaphore();
        else
            break;
    }

On average, having no thread safety yielded 440,909,468 calls, thread-safety with mutexes yielded 98,324,839 calls, and semaphores brought up the rear with 1,197,981 calls. It made a nice little graph:
Graph

So I guess the moral of the story is, if you don’t need super powers when doing thread-safety, use mutexes. And if you know your code is not going to run on a multithreaded system, don’t nest all your critical regions in mutexes or semaphores.

How MPRuntime performs

Over spring break, one of the other things I did was do some performance tests of MPRuntime, matching up how it performed against Apple’s Foundation and CoreFoundation frameworks. The results I found were actually quite interesting.

Click here or the button below to see a full report.
View the performance report

Spring Break is over

Last week was my spring break. It was relaxing, a time away from the constant pressure and stress of school and its related activities. Some of the highlights of my break include:

  • Lots of time with Leslie: taking walks, watching LOST, cooking pizza, making pizza blindfolded, and reading Harry Potter to name a few things.
  • Palm Sunday lunch at Grandma and Grandpa’s with the family.
  • Lunch at Thipi Thai in Glen Ellyn with cousins Alli and Emily.
  • Lunch with mom at Sushi House.
  • Picking sister Sarah up from the airport and having breakfast at Egg Harbor.
  • Easter Sunday lunch at Grandma and Grandpa’s with the family.

That was a short list, but it was a good break overall. How do I know? It was hard to come back to school.

Leaving me wanting

The SDK is downloaded, I wrote my first iPhone app, but…

I can’t test it on the iPhone.

Unfortunately, in order to be an iPhone developer, you have to have a certificate that code-signs your software. In order to obtain this certificate, it is necessary to enroll in the iPhone Developer Program, which costs anywhere from $99 to $299. At this point, that isn’t worth it for me.

Conclusion: iPhone development to be continued at a later time.

iPhone SDK on the way

A beta of the iPhone SDK was released this afternoon at about 1 CST. I immediately tried to get on the site, only to be greeted by a friendly page that said:

Safari can’t open the page “http://developer.apple.com/iphone/program/” because the server unexpectedly dropped the connection, which sometimes occurs when the server is busy. You might be able to open the page later.

This continued all afternoon until just about 10 minutes, when I was finally able to get through!
My downloads window