Skip to content

Commit

Permalink
Merge pull request #720 from ParsePlatform/nlutsenko.commandcache.perf
Browse files Browse the repository at this point in the history
Improve performance of adding commands to PFCommandCache.
  • Loading branch information
nlutsenko committed Jan 5, 2016
2 parents a9665d6 + 695b243 commit 3a1bb8b
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions Parse/Internal/PFCommandCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,34 @@ - (BFTask *)_cleanupDiskCacheWithRequiredFreeSize:(NSUInteger)requiredSize {
return [BFTask taskFromExecutor:[BFExecutor defaultExecutor] withBlock:^id{
NSUInteger size = requiredSize;

NSMutableDictionary *commandSizes = [NSMutableDictionary dictionary];
NSMutableDictionary<NSString *, NSNumber *> *commandSizes = [NSMutableDictionary dictionary];

[[PFMultiProcessFileLockController sharedController] beginLockedContentAccessForFileAtPath:self.diskCachePath];
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];

NSString *identifier = nil;
while ((identifier = [enumerator nextObject])) {
NSNumber *fileSize = enumerator.fileAttributes[NSFileSize];
if (fileSize) {
commandSizes[identifier] = fileSize;
size += fileSize.unsignedIntegerValue;

NSDictionary *directoryAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.diskCachePath error:nil];
if ([directoryAttributes[NSFileSize] unsignedLongLongValue] > self.diskCacheSize) {
NSDirectoryEnumerator<NSURL *> *enumerator = [[NSFileManager defaultManager] enumeratorAtURL:[NSURL fileURLWithPath:self.diskCachePath]
includingPropertiesForKeys:@[ NSURLFileSizeKey ]
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
errorHandler:nil];
NSURL *fileURL = nil;
while ((fileURL = [enumerator nextObject])) {
NSNumber *fileSize = nil;
if (![fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil]) {
continue;
}
if (fileSize) {
commandSizes[fileURL.path.lastPathComponent] = fileSize;
size += fileSize.unsignedIntegerValue;
}
}
}

[[PFMultiProcessFileLockController sharedController] endLockedContentAccessForFileAtPath:self.diskCachePath];

if (size > self.diskCacheSize) {
// Get identifiers and sort them to remove oldest commands first
NSArray *identifiers = [commandSizes.allKeys sortedArrayUsingSelector:@selector(compare:)];
NSArray<NSString *> *identifiers = [commandSizes.allKeys sortedArrayUsingSelector:@selector(compare:)];
for (NSString *identifier in identifiers) @autoreleasepool {
[self _removeFileForCommandWithIdentifier:identifier];
size -= [commandSizes[identifier] unsignedIntegerValue];
Expand Down

0 comments on commit 3a1bb8b

Please sign in to comment.