Skip to content

Commit

Permalink
Improve performance of age limit trimming
Browse files Browse the repository at this point in the history
This does two things:
1. Before this patch, PINCache was naively scheduling a task to trim to age limit *recursively* every time the age limit was set. This makes it so recursive calls are canceled when it detects another recursive call has been kicked off. This is still less than ideal :/
2. Trimming (mistakenly) used to be a high priority task. Now the age limit itself is set with a high priority, but the trimming is done at a low priority.
  • Loading branch information
garrettmoon committed Jun 7, 2018
1 parent 1f88a1e commit 2c2072c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
23 changes: 18 additions & 5 deletions Source/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,24 @@ - (void)trimToAgeLimitRecursively
return;

NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-ageLimit];
[self trimDiskToDate:date];
[self trimToDateAsync:date completion:nil];

dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ageLimit * NSEC_PER_SEC));
dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityLow];
// Ensure that ageLimit is the same as when we were scheduled, otherwise, we've been
// rescheduled (another dispatch_after was issued) and should cancel.
BOOL shouldReschedule = YES;
[self lock];
if (ageLimit != _ageLimit) {
shouldReschedule = NO;
}
[self unlock];

if (shouldReschedule) {
[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityLow];
}
});
}

Expand Down Expand Up @@ -1506,7 +1517,9 @@ - (void)setAgeLimit:(NSTimeInterval)ageLimit
self->_ageLimit = ageLimit;
[self unlock];

[self trimToAgeLimitRecursively];
[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityLow];
} withPriority:PINOperationQueuePriorityHigh];
}

Expand Down
9 changes: 9 additions & 0 deletions Source/PINMemoryCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ - (void)trimToAgeLimitRecursively

dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ageLimit * NSEC_PER_SEC));
dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
// Ensure that ageLimit is the same as when we were scheduled, otherwise, we've been
// rescheduled (another dispatch_after was issued) and should cancel.
BOOL shouldReschedule = YES;
[self lock];
if (ageLimit != _ageLimit) {
shouldReschedule = NO;
}
[self unlock];

[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityHigh];
Expand Down

0 comments on commit 2c2072c

Please sign in to comment.