From 553918b16920cbc3c65a3c51f1f32e6c95309157 Mon Sep 17 00:00:00 2001 From: Aleksandr Panchenko Date: Tue, 10 Dec 2024 11:23:29 +0200 Subject: [PATCH] Cpu ram optimization (#4183) * disablePerformanceAntipatternChecker = "YES" for debug scheme * fix colorization_scheme cached track (Track not found or index is out of bounds) * optimization of visible GPX loading: Fix for excessive memory allocation (> 2GB), which led to crashes --- .../xcschemes/OsmAnd Maps.xcscheme | 1 + Sources/Controllers/Map/Layers/OAGPXLayer.mm | 6 +- .../Controllers/Map/OAMapViewController.mm | 2 +- .../Controllers/Map/OASelectedGPXHelper.mm | 61 +++++++++++-------- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/OsmAnd.xcworkspace/xcshareddata/xcschemes/OsmAnd Maps.xcscheme b/OsmAnd.xcworkspace/xcshareddata/xcschemes/OsmAnd Maps.xcscheme index 5541af2c3a..9db8795e24 100644 --- a/OsmAnd.xcworkspace/xcshareddata/xcschemes/OsmAnd Maps.xcscheme +++ b/OsmAnd.xcworkspace/xcshareddata/xcschemes/OsmAnd Maps.xcscheme @@ -53,6 +53,7 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" + disablePerformanceAntipatternChecker = "YES" launchStyle = "0" useCustomWorkingDirectory = "NO" ignoresPersistentStateOnLaunch = "NO" diff --git a/Sources/Controllers/Map/Layers/OAGPXLayer.mm b/Sources/Controllers/Map/Layers/OAGPXLayer.mm index a71b465d87..6790d4b513 100644 --- a/Sources/Controllers/Map/Layers/OAGPXLayer.mm +++ b/Sources/Controllers/Map/Layers/OAGPXLayer.mm @@ -522,12 +522,10 @@ - (void) refreshGpxTracks { NSArray *tracksCopy_ = [gpxFile_.tracks copy]; NSInteger trackIndex = [tracksCopy_ indexOfObject:track]; - NSArray *tracksCopy = [tracks copy]; - if (trackIndex != NSNotFound && trackIndex < tracksCopy.count) + if (trackIndex != NSNotFound) { - // TODO: mb use not index? search name? - OASTrack *gpxTrack = tracksCopy[trackIndex]; + OASTrack *gpxTrack = track; OASInt *color = [[OASInt alloc] initWithInt:(int)kDefaultTrackColor]; const auto colorARGB = [UIColorFromARGB([[gpxTrack getColorDefColor:color] intValue]) toFColorARGB]; segmentColors.push_back(colorARGB); diff --git a/Sources/Controllers/Map/OAMapViewController.mm b/Sources/Controllers/Map/OAMapViewController.mm index 05f5415bc8..991ff983cb 100644 --- a/Sources/Controllers/Map/OAMapViewController.mm +++ b/Sources/Controllers/Map/OAMapViewController.mm @@ -3720,7 +3720,7 @@ - (void) initRendererWithGpxTracks [_mapLayers.gpxMapLayer refreshGpxTracks:gpxFilesDic reset:YES]; } -- (void) refreshGpxTracks +- (void)refreshGpxTracks { @synchronized(_rendererSync) { diff --git a/Sources/Controllers/Map/OASelectedGPXHelper.mm b/Sources/Controllers/Map/OASelectedGPXHelper.mm index 551cc67a7f..5d1c031198 100644 --- a/Sources/Controllers/Map/OASelectedGPXHelper.mm +++ b/Sources/Controllers/Map/OASelectedGPXHelper.mm @@ -32,6 +32,7 @@ @implementation OASelectedGPXHelper OsmAndAppInstance _app; NSMutableArray *_selectedGPXFilesBackup; + NSMutableArray *_loadingGPXPaths; NSMutableDictionary *_activeGpx; } @@ -54,6 +55,7 @@ - (instancetype)init _settings = [OAAppSettings sharedManager]; _selectedGPXFilesBackup = [NSMutableArray new]; _activeGpx = [NSMutableDictionary dictionary]; + _loadingGPXPaths = [NSMutableArray new]; } return self; } @@ -83,12 +85,14 @@ - (void)markTrackForReload:(NSString *)filePath [self removeGpxFileWith:filePath]; } -- (BOOL) buildGpxList +- (BOOL)buildGpxList { BOOL loading = NO; [_settings hideRemovedGpx]; - - for (NSString *filePath in _settings.mapSettingVisibleGpx.get) + + NSSet *mapSettingVisibleGpx = [NSSet setWithArray:[_settings.mapSettingVisibleGpx get]]; + + for (NSString *filePath in mapSettingVisibleGpx) { if ([filePath hasSuffix:kBackupSuffix]) { @@ -96,40 +100,43 @@ - (BOOL) buildGpxList continue; } NSString *absoluteGpxFilepath = [OsmAndApp.instance.gpxPath stringByAppendingPathComponent:filePath]; - OASGpxDataItem *gpx = [[OAGPXDatabase sharedDb] getGPXItem:absoluteGpxFilepath]; - NSString __block *path = gpx.file.absolutePath; - - if ([[NSFileManager defaultManager] fileExistsAtPath:path] && ![self containsGpxFileWith:path]) + if ([[NSFileManager defaultManager] fileExistsAtPath:absoluteGpxFilepath] + && ![self containsGpxFileWith:absoluteGpxFilepath] + && ![_loadingGPXPaths containsObject:absoluteGpxFilepath]) { - OAGpxLoader __block *loader = [[OAGpxLoader alloc] init]; - loader.path = path; - _activeGpx[path] = nil; + OAGpxLoader *loader = [OAGpxLoader new]; + loader.path = absoluteGpxFilepath; + [_loadingGPXPaths addObject:absoluteGpxFilepath]; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - OASKFile *file = [[OASKFile alloc] initWithFilePath:path]; - OASGpxFile *gpxFile = [OASGpxUtilities.shared loadGpxFileFile:file]; - loader.gpxFile = gpxFile; - dispatch_async(dispatch_get_main_queue(), ^{ - _activeGpx[loader.path] = loader.gpxFile; - [[_app updateGpxTracksOnMapObservable] notifyEvent]; - }); - }); + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @autoreleasepool { + OASGpxFile *gpxFile = [OASGpxUtilities.shared loadGpxFileFile:[[OASKFile alloc] initWithFilePath:absoluteGpxFilepath]]; + if (gpxFile) + { + loader.gpxFile = gpxFile; + dispatch_async(dispatch_get_main_queue(), ^{ + _activeGpx[loader.path] = gpxFile; + [_loadingGPXPaths removeObject:loader.path]; + [[_app updateGpxTracksOnMapObservable] notifyEvent]; + }); + } + }}); loading = YES; } } - NSArray *mapSettingVisibleGpx = [_settings.mapSettingVisibleGpx get]; NSMutableArray *keysToRemove = [NSMutableArray array]; - - for (NSString *key in _activeGpx.allKeys) { + + for (NSString *key in _activeGpx.allKeys) + { NSString *gpxFilePath = [OAUtilities getGpxShortPath:key]; - - if (![mapSettingVisibleGpx containsObject:gpxFilePath]) { + + if (![mapSettingVisibleGpx containsObject:gpxFilePath]) [keysToRemove addObject:key]; - } } - [_activeGpx removeObjectsForKeys:keysToRemove]; - + + if (keysToRemove.count > 0) + [_activeGpx removeObjectsForKeys:keysToRemove]; + return loading; }