Browse Source

macosx: Avoid double iteration in toolbar delegate by using/reusing index

Signed-off-by: Claudio Cambra <developer@claudiocambra.com>
pull/201/head
Claudio Cambra 8 months ago
committed by Felix Paul Kühne
parent
commit
ee907c1f03
  1. 17
      modules/gui/macosx/library/VLCLibraryWindowToolbarDelegate.m

17
modules/gui/macosx/library/VLCLibraryWindowToolbarDelegate.m

@ -174,15 +174,22 @@ NSString * const VLCLibraryWindowTrackingSeparatorToolbarItemIdentifier =
NSParameterAssert(items != nil);
NSParameterAssert(toolbarItem.itemIdentifier.length > 0);
const NSInteger toolbarItemIndex = [self.toolbar.items indexOfObject:toolbarItem];
if (toolbarItemIndex != NSNotFound) {
// Build toolbar item index map once to avoid double iteration
NSArray<NSToolbarItem *> * const toolbarItems = self.toolbar.items;
NSMapTable * const itemIndexMap = [NSMapTable strongToStrongObjectsMapTable];
for (NSUInteger i = 0; i < toolbarItems.count; i++) {
[itemIndexMap setObject:@(i) forKey:toolbarItems[i]];
}
NSNumber * const toolbarItemIndexNumber = [itemIndexMap objectForKey:toolbarItem];
if (toolbarItemIndexNumber != nil) {
return;
}
for (NSToolbarItem * const item in items) {
const NSInteger itemIndex = [self.toolbar.items indexOfObject:item];
if (itemIndex != NSNotFound) {
NSNumber * const itemIndexNumber = [itemIndexMap objectForKey:item];
if (itemIndexNumber != nil) {
const NSInteger itemIndex = itemIndexNumber.integerValue;
[self.toolbar insertItemWithItemIdentifier:toolbarItem.itemIdentifier
atIndex:itemIndex + 1];
return;

Loading…
Cancel
Save