Why is block based enumeration good?

[02:29am] red82: why is block based enumeration good ?
[02:32am] Eridius: red82: it’s as fast as, or faster, than other enumeration
[02:32am] Eridius: red82: and it makes it trivial to do things like reverse enumeration, or concurrent enumeration
[02:32am] red82: how can it be faster, isn’t there a context switch ?
[02:33am] Eridius: there’s a function call..
[02:33am] Eridius: NSFastEnumeration, at its best, calls one function to get a C array of all objects in the collection, and enumerates over them with pointer math. In the ideal world, this is as fast as you can go. But things aren’t ideal
[02:33am] Eridius: if a collection doesn’t have a contiguous array of object pointers, it can’t return them all in one call and will require multiple calls. Or it may copy the objects into a buffer
[02:34am] Eridius: and in key/value collections like NSDictionary, this only retrieves the key, and it requires a message send to get the value
[02:34am] Eridius: block-based enumeration is one function call (the block) per object, but this is the same regardless of the internal layout of the collection (e.g. separate object pointers vs contiguous C arrays of objects vs something else)
[02:34am] Eridius: and block-based enumeration can retrieve the key and value together for key/value collections
[02:35am] Eridius: and, like I said, it trivially supports reverse/concurrent enumeration

This entry was posted in obj-c and tagged . Bookmark the permalink.

Comments are closed.