Skip to content

Commit

Permalink
Cpu ram optimization (#4183)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tigrim authored Dec 10, 2024
1 parent 8ccc98c commit 553918b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
disablePerformanceAntipatternChecker = "YES"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
6 changes: 2 additions & 4 deletions Sources/Controllers/Map/Layers/OAGPXLayer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,10 @@ - (void) refreshGpxTracks
{
NSArray<OASTrack *> *tracksCopy_ = [gpxFile_.tracks copy];
NSInteger trackIndex = [tracksCopy_ indexOfObject:track];
NSArray<OASTrack *> *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);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Controllers/Map/OAMapViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3720,7 +3720,7 @@ - (void) initRendererWithGpxTracks
[_mapLayers.gpxMapLayer refreshGpxTracks:gpxFilesDic reset:YES];
}

- (void) refreshGpxTracks
- (void)refreshGpxTracks
{
@synchronized(_rendererSync)
{
Expand Down
61 changes: 34 additions & 27 deletions Sources/Controllers/Map/OASelectedGPXHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ @implementation OASelectedGPXHelper
OsmAndAppInstance _app;

NSMutableArray *_selectedGPXFilesBackup;
NSMutableArray *_loadingGPXPaths;
NSMutableDictionary<NSString *, OASGpxFile *> *_activeGpx;
}

Expand All @@ -54,6 +55,7 @@ - (instancetype)init
_settings = [OAAppSettings sharedManager];
_selectedGPXFilesBackup = [NSMutableArray new];
_activeGpx = [NSMutableDictionary dictionary];
_loadingGPXPaths = [NSMutableArray new];
}
return self;
}
Expand Down Expand Up @@ -83,53 +85,58 @@ - (void)markTrackForReload:(NSString *)filePath
[self removeGpxFileWith:filePath];
}

- (BOOL) buildGpxList
- (BOOL)buildGpxList
{
BOOL loading = NO;
[_settings hideRemovedGpx];

for (NSString *filePath in _settings.mapSettingVisibleGpx.get)

NSSet<NSString *> *mapSettingVisibleGpx = [NSSet setWithArray:[_settings.mapSettingVisibleGpx get]];

for (NSString *filePath in mapSettingVisibleGpx)
{
if ([filePath hasSuffix:kBackupSuffix])
{
[_selectedGPXFilesBackup addObject:filePath];
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<NSString *> *mapSettingVisibleGpx = [_settings.mapSettingVisibleGpx get];
NSMutableArray<NSString *> *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;
}

Expand Down

0 comments on commit 553918b

Please sign in to comment.