Knowing When to Let Go: Better Living Through Memory Management
Subject:   is simpler slower?
Date:   2003-06-12 22:07:15
From:   iapole
Response to: is simpler slower?

Completely subjectively--yes. In the example accessor method I gave (which is not the only possible one, as I'll show in a moment), the penalties are as follows:
- one 4-byte word for the pointer (id old)
- two messages, both of which will have been optimized by Apple considering how commonly they're called
- one assignment.

In the example you show here, you have the same except for the pointer to the object, but the difference in message (autorelease as opposed to release) will make a much larger difference.

For one thing, in order to autorelease the object, the autorelease pool has to know it exists--that is, it has to use a pointer to it of its own. And as I discussed in the article, autoreleasing is a slow operation.

Therefore, I would recommend against autoreleasing in this context unless you have some very specific reason to.

As another example of an accessor, how about:

[friend release];
friend = [aFriend retain];

Note that none of these examples make any attempt at addressing concurrency.

I recommend reading for more insight into Cocoa accessor methods, including thread-safe accessors.

1 to 2 of 2
  1. is simpler slower?
    2003-06-20 18:05:46  anonymous2 [View]

  2. is simpler slower?
    2003-06-13 14:27:00  anonymous2 [View]

    • is simpler slower?
      2003-06-13 20:52:04  iapole [View]

      • is simpler slower?
        2003-06-15 21:29:50  anonymous2 [View]

        • is simpler slower?
          2003-06-16 16:11:16  iapole [View]

1 to 2 of 2