From 235821ef75878505b5490d6f729c425fc3c81119 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Fri, 4 Mar 2016 22:53:39 -0800 Subject: [PATCH] Reduce stack frame from continuation stack trace if task is completed. --- Bolts/Common/BFTask.m | 72 +++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/Bolts/Common/BFTask.m b/Bolts/Common/BFTask.m index 1588d9e60..60a87bcf5 100644 --- a/Bolts/Common/BFTask.m +++ b/Bolts/Common/BFTask.m @@ -318,59 +318,59 @@ - (BFTask *)continueWithExecutor:(BFExecutor *)executor BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource]; // Capture all of the state that needs to used when the continuation is complete. - void (^wrappedBlock)() = ^() { - [executor execute:^{ - if (cancellationToken.cancellationRequested) { - [tcs cancel]; - return; - } - - id result = nil; - @try { - result = block(self); - } @catch (NSException *exception) { - tcs.exception = exception; - return; - } - - if ([result isKindOfClass:[BFTask class]]) { + dispatch_block_t executionBlock = ^{ + if (cancellationToken.cancellationRequested) { + [tcs cancel]; + return; + } - id (^setupWithTask) (BFTask *) = ^id(BFTask *task) { - if (cancellationToken.cancellationRequested || task.cancelled) { - [tcs cancel]; - } else if (task.exception) { - tcs.exception = task.exception; - } else if (task.error) { - tcs.error = task.error; - } else { - tcs.result = task.result; - } - return nil; - }; + id result = nil; + @try { + result = block(self); + } @catch (NSException *exception) { + tcs.exception = exception; + return; + } - BFTask *resultTask = (BFTask *)result; + if ([result isKindOfClass:[BFTask class]]) { - if (resultTask.completed) { - setupWithTask(resultTask); + id (^setupWithTask) (BFTask *) = ^id(BFTask *task) { + if (cancellationToken.cancellationRequested || task.cancelled) { + [tcs cancel]; + } else if (task.exception) { + tcs.exception = task.exception; + } else if (task.error) { + tcs.error = task.error; } else { - [resultTask continueWithBlock:setupWithTask]; + tcs.result = task.result; } + return nil; + }; + + BFTask *resultTask = (BFTask *)result; + if (resultTask.completed) { + setupWithTask(resultTask); } else { - tcs.result = result; + [resultTask continueWithBlock:setupWithTask]; } - }]; + + } else { + tcs.result = result; + } }; BOOL completed; @synchronized(self.lock) { completed = self.completed; if (!completed) { - [self.callbacks addObject:[wrappedBlock copy]]; + [self.callbacks addObject:[^{ + [executor execute:executionBlock]; + } copy]]; } } if (completed) { - wrappedBlock(); + [executor execute:executionBlock]; } return tcs.task;