From f48f27c812466ce806ab60b43ebe5a2bfc97ed0a Mon Sep 17 00:00:00 2001 From: Emily Kooistra Date: Tue, 17 Jun 2025 16:05:23 +0200 Subject: [PATCH] perf(core): store translations in a globally static NSMutableDictionary fetching public ICS files without this patch result in continual opening and closing of the "Localizable.strings" to create a NSDictionary, looking up one key and closing it again. By caching it in a static variable this greatly improves the performance. --- SoObjects/SOGo/NSObject+Utilities.m | 30 ++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/SoObjects/SOGo/NSObject+Utilities.m b/SoObjects/SOGo/NSObject+Utilities.m index dfd313c3d..08994c9e6 100644 --- a/SoObjects/SOGo/NSObject+Utilities.m +++ b/SoObjects/SOGo/NSObject+Utilities.m @@ -33,6 +33,9 @@ #import "NSObject+Utilities.h" +static NSMutableDictionary *translationCache = nil; + + @implementation NSObject (SOGoObjectUtilities) - (NSString *) jsonRepresentation @@ -101,19 +104,28 @@ if (!bundle) bundle = [NSBundle mainBundle]; languages = [[self _languagesForLabelsInContext: context] objectEnumerator]; + + if(!translationCache) + translationCache = [NSMutableDictionary new]; while (!label && (language = [languages nextObject])) { - paths = [bundle pathsForResourcesOfType: @"strings" - inDirectory: [NSString stringWithFormat: @"%@.lproj", - language] + if (![translationCache objectForKey: language]) { + paths = [bundle pathsForResourcesOfType: @"strings" + inDirectory: [NSString stringWithFormat: @"%@.lproj", + language] forLocalization: language]; - if ([paths count] > 0) - { - strings = [NSDictionary - dictionaryFromStringsFile: [paths objectAtIndex: 0]]; - label = [strings objectForKey: key]; - } + if ([paths count] > 0) + { + strings = [NSDictionary + dictionaryFromStringsFile: [paths objectAtIndex: 0]]; + [translationCache setObject: strings forKey: language]; + label = [strings objectForKey: key]; + } + } else { + strings = [translationCache objectForKey: language]; + label = [strings objectForKey: key]; + } } if (!label) label = key;