• 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: [__NSFastEnumerationEnumerator nextObject] unexpectedly not very fast!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [__NSFastEnumerationEnumerator nextObject] unexpectedly not very fast!


  • Subject: Re: [__NSFastEnumerationEnumerator nextObject] unexpectedly not very fast!
  • From: James Bucanek <email@hidden>
  • Date: Fri, 25 Sep 2009 08:36:04 -0700

Graham Cox <mailto:email@hidden> wrote (Thursday, September 24, 2009 6:10 PM +1000):
I thought that NSEnumerator was implemented in terms of fast enumeration
"underneath" - the docs seem to imply that, as does the existence of
__NSFastEnumerationEnumerator private class. It may be still slower than
directly using the fast enumeration syntax, but isn't there some gain to be
had? I have hundreds of these enumeration loops and I still need to support
10.4; I thought I was getting *some* benefit automagically from fast
enumeration on 10.5+

No one replied to your questiona directly, so I'll throw in my two cents.


Fast enumeration is a 10.5+ObjC2.0 feature only and is implemented as an alternate method of enumeration. NSEnumerator in 10.5 is not built on top of fast enumeration or vise versa. The reason is simple: fast enumeration achieves most of its speed improvement by fetching objects in batches. The interface to NSEnumerator fetches one object at a time, so nothing underneath it is going to avoid the overhead of the per-object message that it already incurs.

What's mentioned in the documentation is that NSEnumerator implements the fast enumeration protocol *in order to be compatible with* fast enumeration syntax. This means that you can use any NSEnumerator object as the subject of a fast enumeration statement in Objective-C 2.0:

    NSEnumerator *e = [collection enumerator];
    for ( id i in e ) ...

But this is not optimized; fast enumeration just calls [e nextObject] once for each object, so it isn't any faster than using the NSEnumerator. The only thing you've saved is typing.

In summary, the existence of fast enumeration does nothing for existing enumeration technologies and if you have to support 10.4 (as I do) you simply can't use it unless you fork your code.

My solution, in the few cases where performance is paramount, has been to essentially roll my own fast enumeration. For very large arrays (thousands of objects) I'll get blocks of objects in batching using [NSArray getObjects:range:], process those in a tight C loop, and then get another batch.

Also, wouldn't methods such as -makeObjectsPerformSelector:withObject: and
CFArrayApplyFunction(...) gain some benefit from fast enumeration? I use these
a lot too.

I'm sure these kinds of functions are optimized internally and probably don't use enumerator objects or fast enumeration. But that's just a guess. The rule is, as always, don't worry about it unless it's a problem, then profile it, find the bottleneck, and work around it.
--
James Bucanek


_______________________________________________

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


References: 
 >Re: [__NSFastEnumerationEnumerator nextObject] unexpectedly not very fast! (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: How to create a control just as RGB Sphere and Alpha bar
  • Next by Date: Re: Remove NSTableView Default Selection
  • Previous by thread: Re: [__NSFastEnumerationEnumerator nextObject] unexpectedly not very fast!
  • Next by thread: Exit screen saver programmatically?
  • Index(es):
    • Date
    • Thread