GCD Poor Performance

cocoa, concurrency, grand-central-dispatch, objective-c

Solution

Is there a reason you're not using the GCD C API and the `dispatch_*` family of functions? You don't have much control over the GCD aspects of `NSOperationQueue` (like which queue you want to submit the blocks to). Also, I can't tell if you're using iOS or not, but `NSOperationQueue` does not use GCD on iOS. That might be the reason it spawned so many threads. Either way, your code will be shorter and simpler if you use the GCD API directly:

- (double) detectCollisionsInArray:(NSArray*)objects
{   
  int count = [objects count];
  if (count > 0)
  {
    double time = CFAbsoluteTimeGetCurrent();

    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    for (int i = 0; i < count; i++)
    {
      dispatch_group_async(group, queue, ^{
        for (int j = i + 1; j < count; j++)
        {
          dispatch_group_async(group, queue, ^{
            /** LOTS AND LOTS OF WORK FOR EACH OBJECT **/
          });
        }
      });
    }
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    dispatch_release(group);
    return CFAbsoluteTimeGetCurrent() - time;
  }
  return 0;
}

You can use a `dispatch_group` to group all of the executions together and wait for them all to finish with `dispatch_group_wait`. If you don't care to know when the the blocks finish, you can ignore the group part and just use `dispatch_async`. The `dispatch_get_global_queue` function will get one of the 3 concurrent queues (low, default or high priority) for you to submit your blocks to. You shouldn't have to worry about limiting the thread count or anything like that. The GCD scheduler is supposed to do all of that for you. Just make sure you submit to a concurrent queue, which could either be one of the 3 global queues, or a queue you've created by passing `DISPATCH_QUEUE_CONCURRENT` to `dispatch_queue_create` (this is available starting OS X 10.7 and iOS 5.0).

If you're doing some file I/O in each block or taxing some other resource, you might need to reign in GCD and limit the number of blocks you're submitting to the queue at once. This will have the same effect as limiting the concurrent operation count in an `NSOperationQueue`. You can use a GCD semaphore to do this:

- (double) detectCollisionsInArray:(NSArray*)objects
{   
  int count = [objects count];
  if (count > 0)
  {
    double time = CFAbsoluteTimeGetCurrent();

    dispatch_group_t group = dispatch_group_create();
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    for (int i = 0; i < count; i++)
    {
      dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
      dispatch_group_async(group, queue, ^{
        for (int j = i + 1; j < count; j++)
        {
          dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
          dispatch_group_async(group, queue, ^{
            /** LOTS AND LOTS OF WORK FOR EACH OBJECT **/
            dispatch_semaphore_signal(semaphore);
          });
        }
        dispatch_semaphore_signal(semaphore);
      });
    }
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    dispatch_release(group);
    dispatch_release(semaphore);
    return CFAbsoluteTimeGetCurrent() - time;
  }
  return 0;
}

Once you get the hang of it, GCD is very simple to use. I use it all over my code now.

Can anyone help to put me on the right track and perhaps provide a link to a good GCD tutorial?

Run, don't walk over to Mike Ash's blog. His series on GCD is the clearest and most concise I've seen, and it'll only take you around 30 minutes to read the whole thing. Apple's WWDC videos from 2010 on GCD And blocks are also pretty good.

Problem

As you may remember, I am trying to use GCD to speed up some of my code, namely a collision detection and resolution engine. However, I am clearly doing something wrong because all of my GCD code is significantly slower and less consistent than my serial code (between 1.4x and 10x slower). Allow me to give you an example: I am iterating over an array in a bubble-sort fashion to determine all possible collisions among objects in that array: ``` - (double) detectCollisionsInArray:(NSArray*)objects { int count = [objects count]; if (count > 0) { double time = CFAbsoluteTimeGetCurrent(); for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { /** LOTS AND LOTS OF WORK FOR EACH OBJECT **/ } } return CFAbsoluteTimeGetCurrent() - time; } return 0; } ``` Pretty straightforward, and it seems to perform well given the constraints of the problem. However, I would like to take advantage of the fact that the state of each object is not modified in the code section and use GCD to parallelize this work. To do this I am trying something like this: ``` - (double) detectCollisionsInArray:(NSArray*)objects { int count = [objects count]; if (count > 0) { NSOperationQueue* opQueue = [[NSOperationQueue alloc] init]; NSBlockOperation* blockOperation = nil; double time = CFAbsoluteTimeGetCurrent(); for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { void (^workBlock) (void) = ^() { /** LOTS AND LOTS OF WORK FOR EACH OBJECT **/ }; if (!blockOperation) { blockOperation = [NSBlockOperation blockOperationWithBlock:b]; } else { [blockOperation addExecutionBlock:workBlock]; } } } [opQueue addOperation:blockOperation]; [opQueue autorelease]; return CFAbsoluteTimeGetCurrent() - time; } return 0; } ``` Can anyone help to put me on the right track and perhaps provide a link to a good GCD tutorial? I have looked over several GCD tutorials and scoured all of the documentation and I still feel that my grasp on the subject is tenuous at best. Thanks!

Original source