Skip to content

Commit

Permalink
Cherry-pick 247ec3b. rdar://123959934
Browse files Browse the repository at this point in the history
    Create a faster cache of _WKWebExtensionTabs
    https://bugs.webkit.org/show_bug.cgi?id=270488
    rdar://123959934

    Reviewed by Timothy Hatcher.

    Instead of iterating over all of the tabs in m_tabMap and checking to see if their delegates match, create a new
    data structure that is a map table from the delegate to the identifier.

    This way, we can just do one O(1) lookup instead of an O(n) iteration.

    * Source/WebKit/UIProcess/Extensions/Cocoa/WebExtensionContextCocoa.mm:
    (WebKit::WebExtensionContext::WebExtensionContext):
    (WebKit::WebExtensionContext::getOrCreateTab const):
    (WebKit::WebExtensionContext::forgetTab const):
    * Source/WebKit/UIProcess/Extensions/WebExtensionContext.h:

    Canonical link: https://commits.webkit.org/275692@main
  • Loading branch information
b-weinstein authored and Mohsin Qureshi committed Mar 7, 2024
1 parent adf1129 commit 6a8dcd2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ - (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView
m_extension = extension.ptr();
m_baseURL = URL { makeString("webkit-extension://", uniqueIdentifier(), '/') };
m_delegate = [[_WKWebExtensionContextDelegate alloc] initWithWebExtensionContext:*this];
m_tabDelegateToIdentifierMap = [NSMapTable weakToStrongObjectsMapTable];
}

static _WKWebExtensionContextError toAPI(WebExtensionContext::Error error)
Expand Down Expand Up @@ -302,6 +303,7 @@ static _WKWebExtensionContextError toAPI(WebExtensionContext::Error error)

m_tabMap.clear();
m_extensionPageTabMap.clear();
[m_tabDelegateToIdentifierMap removeAllObjects];

m_windowMap.clear();
m_windowOrderVector.clear();
Expand Down Expand Up @@ -1485,13 +1487,18 @@ static _WKWebExtensionContextError toAPI(WebExtensionContext::Error error)
{
ASSERT(delegate);

for (Ref tab : m_tabMap.values()) {
if (tab->delegate() == delegate)
return tab;
if (NSNumber *tabIdentifier = [m_tabDelegateToIdentifierMap objectForKey:delegate]) {
if (auto tab = getTab(WebExtensionTabIdentifier(tabIdentifier.unsignedLongLongValue))) {
RELEASE_LOG_DEBUG(Extensions, "Using cached tab for identifier %{public}llu", tabIdentifier.unsignedLongLongValue);
return tab.releaseNonNull();
}
}

Ref tab = adoptRef(*new WebExtensionTab(*this, delegate));
m_tabMap.set(tab->identifier(), tab);

auto tabIdentifier = tab->identifier();
m_tabMap.set(tabIdentifier, tab);
[m_tabDelegateToIdentifierMap setObject:@(tabIdentifier.toUInt64()) forKey:delegate];

RELEASE_LOG_DEBUG(Extensions, "Tab %{public}llu was created", tab->identifier().toUInt64());

Expand Down Expand Up @@ -1602,7 +1609,11 @@ static _WKWebExtensionContextError toAPI(WebExtensionContext::Error error)

void WebExtensionContext::forgetTab(WebExtensionTabIdentifier identifier) const
{
m_tabMap.remove(identifier);
RefPtr tab = m_tabMap.take(identifier);
if (!tab)
return;

[m_tabDelegateToIdentifierMap removeObjectForKey:tab->delegate()];
}

void WebExtensionContext::openNewTab(const WebExtensionTabParameters& parameters, CompletionHandler<void(RefPtr<WebExtensionTab>)>&& completionHandler)
Expand Down
4 changes: 4 additions & 0 deletions Source/WebKit/UIProcess/Extensions/WebExtensionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@
OBJC_CLASS NSArray;
OBJC_CLASS NSDate;
OBJC_CLASS NSDictionary;
OBJC_CLASS NSMapTable;
OBJC_CLASS NSMutableDictionary;
OBJC_CLASS NSNumber;
OBJC_CLASS NSString;
OBJC_CLASS NSURL;
OBJC_CLASS NSUUID;
Expand Down Expand Up @@ -875,6 +877,8 @@ class WebExtensionContext : public API::ObjectImpl<API::Object::Type::WebExtensi
PageTabIdentifierMap m_extensionPageTabMap;
PopupPageActionMap m_popupPageActionMap;

RetainPtr<NSMapTable> m_tabDelegateToIdentifierMap;

CommandsVector m_commands;
bool m_populatedCommands { false };

Expand Down

0 comments on commit 6a8dcd2

Please sign in to comment.