From 0b6b6fe4ca20ccb9ad506231f8913e1d48b453eb Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 7 Jan 2017 23:52:56 -0500 Subject: [PATCH] Improve -[SUHost objectForInfoDictionaryKey:] We use the cached host bundle if the host bundle is equal to the main bundle. Otherwise we read in the latest Info dictionary every time. The cached case is the most common case. The odd case (updating other bundles) is less common. Thus we have better performance for the common case, and we retain correct code in both cases. Also we invoke our own implementation of -objectForInfoDictionaryKey: rather than NSBundle's implementation in a few places. --- Sparkle/SUHost.m | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 8470ae22..bd9bc78a 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -20,6 +20,7 @@ @interface SUHost () @property (strong, readwrite) NSBundle *bundle; +@property (nonatomic, readonly) BOOL isMainBundle; @property (copy) NSString *defaultsDomain; @property (assign) BOOL usesStandardUserDefaults; @@ -28,6 +29,7 @@ @implementation SUHost @synthesize bundle; +@synthesize isMainBundle = _isMainBundle; @synthesize defaultsDomain; @synthesize usesStandardUserDefaults; @@ -40,8 +42,10 @@ if (![self.bundle bundleIdentifier]) { SULog(@"Error: the bundle being updated at %@ has no %@! This will cause preference read/write to not work properly.", self.bundle, kCFBundleIdentifierKey); } + + _isMainBundle = [aBundle isEqualTo:[NSBundle mainBundle]]; - self.defaultsDomain = [self.bundle objectForInfoDictionaryKey:SUDefaultsDomainKey]; + self.defaultsDomain = [self objectForInfoDictionaryKey:SUDefaultsDomainKey]; if (!self.defaultsDomain) { self.defaultsDomain = [self.bundle bundleIdentifier]; } @@ -69,7 +73,7 @@ name = [self objectForInfoDictionaryKey:@"SUBundleName"]; if (name && name.length > 0) return name; - name = [self.bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]; + name = [self objectForInfoDictionaryKey:@"CFBundleDisplayName"]; if (name && name.length > 0) return name; name = [self objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey]; @@ -80,7 +84,7 @@ - (NSString *__nonnull)version { - NSString *version = [self.bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; + NSString *version = [self objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; if (!version || [version isEqualToString:@""]) [NSException raise:@"SUNoVersionException" format:@"This host (%@) has no %@! This attribute is required.", [self bundlePath], (__bridge NSString *)kCFBundleVersionKey]; return version; @@ -88,7 +92,7 @@ - (NSString *__nonnull)displayVersion { - NSString *shortVersionString = [self.bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; + NSString *shortVersionString = [self objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; if (shortVersionString) return shortVersionString; else @@ -105,7 +109,7 @@ - (NSString *__nullable)publicDSAKey { // Maybe the key is just a string in the Info.plist. - NSString *key = [self.bundle objectForInfoDictionaryKey:SUPublicDSAKeyKey]; + NSString *key = [self objectForInfoDictionaryKey:SUPublicDSAKeyKey]; if (key) { return key; } @@ -130,7 +134,21 @@ - (id)objectForInfoDictionaryKey:(NSString *)key { - return [self.bundle objectForInfoDictionaryKey:key]; + if (self.isMainBundle) { + // Common fast path - if we're updating the main bundle, that means our updater and host bundle's lifetime is the same + // If the bundle happens to be updated or change, that means our updater process needs to be terminated first to do it safely + // Thus we can rely on the cached Info dictionary + return [self.bundle objectForInfoDictionaryKey:key]; + } else { + // Slow path - if we're updating another bundle, we should read in the most up to date Info dictionary because + // the bundle can be replaced externally or even by us. + // This is the easiest way to read the Info dictionary values *correctly* despite some performance loss. + // A mutable method to reload the Info dictionary at certain points and have it cached at other points is challenging to do correctly. + CFDictionaryRef cfInfoDictionary = CFBundleCopyInfoDictionaryInDirectory((CFURLRef)self.bundle.bundleURL); + NSDictionary *infoDictionary = CFBridgingRelease(cfInfoDictionary); + + return [infoDictionary objectForKey:key]; + } } - (BOOL)boolForInfoDictionaryKey:(NSString *)key