Browse Source

macosx: Avoid creating copies where possible during actions in librarymodel

Signed-off-by: Claudio Cambra <developer@claudiocambra.com>

# Conflicts:
#	modules/gui/macosx/library/favorites-library/VLCLibraryFavoritesDataSource.m
pull/201/head
Claudio Cambra 8 months ago
committed by Felix Paul Kühne
parent
commit
e3e36e5677
  1. 38
      modules/gui/macosx/library/VLCLibraryModel.m
  2. 2
      modules/gui/macosx/library/favorites-library/VLCLibraryFavoritesDataSource.m

38
modules/gui/macosx/library/VLCLibraryModel.m

@ -1129,26 +1129,31 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
BOOL (^idCheckBlock)(VLCMediaLibraryMediaItem * const, const NSUInteger, BOOL * const) = ^BOOL(VLCMediaLibraryMediaItem * const mediaItem, const NSUInteger __unused idx, BOOL * const __unused stop) { BOOL (^idCheckBlock)(VLCMediaLibraryMediaItem * const, const NSUInteger, BOOL * const) = ^BOOL(VLCMediaLibraryMediaItem * const mediaItem, const NSUInteger __unused idx, BOOL * const __unused stop) {
NSAssert(mediaItem != nil, @"Cache list should not contain nil media items"); NSAssert(mediaItem != nil, @"Cache list should not contain nil media items");
return mediaItem.libraryID == libraryId; return mediaItem.libraryID == libraryId;
}; };
// Recents can contain media items the other two do // Search immutable arrays first, only copy when modification needed
NSMutableArray * const recentsMutable = self.cachedRecentMedia.mutableCopy; const NSUInteger recentsIndex = [self.cachedRecentMedia indexOfObjectPassingTest:idCheckBlock];
const NSUInteger recentsIndex = [recentsMutable indexOfObjectPassingTest:idCheckBlock]; const NSUInteger videoIndex = [self.cachedVideoMedia indexOfObjectPassingTest:idCheckBlock];
NSMutableArray * const videoMutable = self.cachedVideoMedia.mutableCopy;
const NSUInteger videoIndex = [videoMutable indexOfObjectPassingTest:idCheckBlock];
if (videoIndex != NSNotFound) { if (videoIndex != NSNotFound) {
NSMutableArray * const showsMutable = self.cachedListOfShows.mutableCopy; // Found in video cache - search shows for episode match
NSInteger showIndex = NSNotFound; NSInteger showIndex = NSNotFound;
NSInteger episodeIndex = NSNotFound; NSInteger episodeIndex = NSNotFound;
for (VLCMediaLibraryShow * const show in showsMutable) { NSUInteger currentShowIndex = 0;
for (VLCMediaLibraryShow * const show in self.cachedListOfShows) {
episodeIndex = [show.episodes indexOfObjectPassingTest:idCheckBlock]; episodeIndex = [show.episodes indexOfObjectPassingTest:idCheckBlock];
showIndex = [showsMutable indexOfObject:show];
if (episodeIndex != NSNotFound) { if (episodeIndex != NSNotFound) {
showIndex = currentShowIndex;
break; break;
} }
currentShowIndex++;
} }
// Now create mutable copies for modification
NSMutableArray * const recentsMutable = self.cachedRecentMedia.mutableCopy;
NSMutableArray * const videoMutable = self.cachedVideoMedia.mutableCopy;
NSMutableArray * const showsMutable = self.cachedListOfShows.mutableCopy;
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
action(videoMutable, videoIndex, recentsMutable, recentsIndex, showsMutable, showIndex, episodeIndex); action(videoMutable, videoIndex, recentsMutable, recentsIndex, showsMutable, showIndex, episodeIndex);
self.cachedVideoMedia = videoMutable.copy; self.cachedVideoMedia = videoMutable.copy;
@ -1157,20 +1162,25 @@ static void libraryCallback(void *p_data, const vlc_ml_event_t *p_event)
return; return;
} }
NSMutableArray * const recentAudiosMutable = self.cachedRecentAudioMedia.mutableCopy; // Not in video cache, check audio cache
const NSUInteger recentAudiosIndex = [recentAudiosMutable indexOfObjectPassingTest:idCheckBlock]; const NSUInteger recentAudiosIndex = [self.cachedRecentAudioMedia indexOfObjectPassingTest:idCheckBlock];
NSMutableArray * const audioMutable = self.cachedAudioMedia.mutableCopy;
const NSUInteger audioIndex = [self.cachedAudioMedia indexOfObjectPassingTest:idCheckBlock]; const NSUInteger audioIndex = [self.cachedAudioMedia indexOfObjectPassingTest:idCheckBlock];
if (audioIndex != NSNotFound) { if (audioIndex != NSNotFound) {
// Found in audio cache - create mutable copies for modification
NSMutableArray * const recentsMutable = self.cachedRecentMedia.mutableCopy;
NSMutableArray * const recentAudiosMutable = self.cachedRecentAudioMedia.mutableCopy;
NSMutableArray * const audioMutable = self.cachedAudioMedia.mutableCopy;
dispatch_sync(dispatch_get_main_queue(), ^{ dispatch_sync(dispatch_get_main_queue(), ^{
action(audioMutable, audioIndex, recentAudiosMutable, recentAudiosIndex, nil, NSNotFound, NSNotFound); action(audioMutable, audioIndex, recentAudiosMutable, recentAudiosIndex, nil, NSNotFound, NSNotFound);
self.cachedAudioMedia = audioMutable.copy; self.cachedAudioMedia = audioMutable.copy;
self.cachedRecentAudioMedia = recentsMutable.copy; self.cachedRecentAudioMedia = recentAudiosMutable.copy;
}); });
return; return;
} }
// Not found in any cache
action(nil, NSNotFound, nil, NSNotFound, nil, NSNotFound, NSNotFound); action(nil, NSNotFound, nil, NSNotFound, nil, NSNotFound, NSNotFound);
}); });
} }

2
modules/gui/macosx/library/favorites-library/VLCLibraryFavoritesDataSource.m

@ -162,7 +162,7 @@ NSString * const VLCLibraryFavoritesDataSourceDisplayedCollectionChangedNotifica
- (void)updateVisibleSectionMapping - (void)updateVisibleSectionMapping
{ {
NSMutableArray<NSNumber *> * const visibleSections = [NSMutableArray array]; NSMutableArray<NSNumber *> * const visibleSections = [NSMutableArray arrayWithCapacity:VLCLibraryFavoritesSectionCount];
for (NSUInteger i = 1; i < VLCLibraryFavoritesSectionCount; i++) { for (NSUInteger i = 1; i < VLCLibraryFavoritesSectionCount; i++) {
NSArray * const sectionArray = [self arrayForSection:i]; NSArray * const sectionArray = [self arrayForSection:i];
if (sectionArray.count > 0) { if (sectionArray.count > 0) {

Loading…
Cancel
Save