Author Archive for Kev

Another decorative pizza

While I was home this past weekend, Leslie and I did another decorative pizza. At Wheaton North High School where Leslie and her sister Lizzy attend, there is a program where French foreign exchange students come and spend two weeks at the homes of students at the school. This year, Leslie’s family volunteered to have someone join them. On Saturday, the day we made the pizza, Agathe arrived. So in honor of her arrival to the states and her Frenchness, we decided to make the pizza French themed.

Can you guess what we did?

Our French pizza

The cry for musings

I have to apologize, the last like 132984 posts on my blog have been technical. I hear the cries of those who just want to read musings, and so I will try my hardest to muse more.

Keep checking back!

Thread safety performance on different CPU architectures

A few weeks ago, I did a post on thread safety performance on my machine. However, that was only on one machine, a 2.16 GHz Intel Core Duo architecture.

Yesterday, Joey and I decided to run some more tests of thread safety performance implementations (no thread safety, mutexes, and semaphores) on different architectures, and the results were quite interesting. We ran tests on my machine again, on a 2.16 GHz Intel Core 2 Duo iMac, a 800 GHz PowerPC G4 iMac, and a Quad Core 2.66 GHz Intel Xeon Mac Pro. After running the same tests on each machine, we normalized the data to get rid of the clock-speed factor.

For mutexes, the machines all performed about the same, with my Core Duo doing slightly better than all of the other machines. In second place was the PPC G4, and then the Core 2 Duo machines took up last. For the semaphores, the PPC G4 smoked the Intel chips, a very interesting result.

Here is a summary of our results (the numbers listed are in cycles/5 seconds and the numbers in parenthesis are the normalized data):

Core Duo:
No thread safety: 440909468 (1)
Mutexes: 98324839 (0.223)
Semaphores: 197981 (0.00044)

Core 2 Duo:
No thread safety: 831519248 (1)
Mutexes: 135168338 (0.163)
Semaphores: 1153251 (0.00139)

PowerPC G4:
No thread safety: 119723944 (1)
Mutexes: 23709889 (0.198)
Semaphores: 1020029 (0.00852)

Quad-core Xeon:
No thread safety: 884848737 (1)
Mutexes: 140097898 (0.158)
Semaphores: 1157646 (0.00131)

Here are some charts representing this data:
All data

Mutex performance

Semaphore performance

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