Title: [282765] trunk/Source/WebCore
Revision
282765
Author
wenson_hs...@apple.com
Date
2021-09-20 11:42:17 -0700 (Mon, 20 Sep 2021)

Log Message

[Live Text] Adopt WeakHashMap for caching per-element text recognition results
https://bugs.webkit.org/show_bug.cgi?id=230461

Reviewed by Megan Gardner.

Simplify this logic by replacing the WeakHashSet and WeakPtr/TextRecognitionResult pair with just a WeakHashMap.
No change in behavior.

* page/Page.cpp:
(WebCore::Page::updateElementsWithTextRecognitionResults):
(WebCore::Page::hasCachedTextRecognitionResult const):
(WebCore::Page::cacheTextRecognitionResult):
(WebCore::Page::resetTextRecognitionResults):
* page/Page.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (282764 => 282765)


--- trunk/Source/WebCore/ChangeLog	2021-09-20 18:20:29 UTC (rev 282764)
+++ trunk/Source/WebCore/ChangeLog	2021-09-20 18:42:17 UTC (rev 282765)
@@ -1,3 +1,20 @@
+2021-09-20  Wenson Hsieh  <wenson_hs...@apple.com>
+
+        [Live Text] Adopt WeakHashMap for caching per-element text recognition results
+        https://bugs.webkit.org/show_bug.cgi?id=230461
+
+        Reviewed by Megan Gardner.
+
+        Simplify this logic by replacing the WeakHashSet and WeakPtr/TextRecognitionResult pair with just a WeakHashMap.
+        No change in behavior.
+
+        * page/Page.cpp:
+        (WebCore::Page::updateElementsWithTextRecognitionResults):
+        (WebCore::Page::hasCachedTextRecognitionResult const):
+        (WebCore::Page::cacheTextRecognitionResult):
+        (WebCore::Page::resetTextRecognitionResults):
+        * page/Page.h:
+
 2021-09-20  Antti Koivisto  <an...@apple.com>
 
         [LFC][Integration] Enable selections

Modified: trunk/Source/WebCore/page/Page.cpp (282764 => 282765)


--- trunk/Source/WebCore/page/Page.cpp	2021-09-20 18:20:29 UTC (rev 282764)
+++ trunk/Source/WebCore/page/Page.cpp	2021-09-20 18:42:17 UTC (rev 282765)
@@ -3629,24 +3629,22 @@
 
 void Page::updateElementsWithTextRecognitionResults()
 {
-    if (m_textRecognitionResultsByElement.isEmpty())
+    if (m_textRecognitionResults.isEmptyIgnoringNullReferences())
         return;
 
-    m_textRecognitionResultsByElement.removeAllMatching([](auto& elementAndResult) {
-        return !elementAndResult.first;
-    });
+    m_textRecognitionResults.removeNullReferences();
 
     Vector<std::pair<Ref<HTMLElement>, TextRecognitionResult>> elementsToUpdate;
-    for (auto& [element, resultAndRect] : m_textRecognitionResultsByElement) {
-        if (!element->isConnected())
+    for (auto entry : m_textRecognitionResults) {
+        Ref protectedElement = entry.key;
+        if (!protectedElement->isConnected())
             continue;
 
-        auto& [result, containerRect] = resultAndRect;
-        auto protectedElement = makeRef(*element);
         auto renderer = protectedElement->renderer();
         if (!is<RenderImage>(renderer))
             continue;
 
+        auto& [result, containerRect] = entry.value;
         auto newContainerRect = protectedElement->containerRectForTextRecognition();
         if (containerRect == newContainerRect)
             continue;
@@ -3668,31 +3666,17 @@
 
 bool Page::hasCachedTextRecognitionResult(const HTMLElement& element) const
 {
-    return m_elementsWithTextRecognitionResults.contains(element);
+    return m_textRecognitionResults.contains(element);
 }
 
 void Page::cacheTextRecognitionResult(const HTMLElement& element, const IntRect& containerRect, const TextRecognitionResult& result)
 {
-    m_elementsWithTextRecognitionResults.add(element);
-
-    auto index = m_textRecognitionResultsByElement.findMatching([&](auto& elementAndResult) {
-        return elementAndResult.first == &element;
-    });
-
-    if (index == notFound)
-        m_textRecognitionResultsByElement.append({ makeWeakPtr(element), { result, containerRect } });
-    else
-        m_textRecognitionResultsByElement[index].second = { result, containerRect };
-
-    m_textRecognitionResultsByElement.removeAllMatching([](auto& elementAndResult) {
-        return !elementAndResult.first;
-    });
+    m_textRecognitionResults.set(element, CachedTextRecognitionResult { result, containerRect });
 }
 
 void Page::resetTextRecognitionResults()
 {
-    m_textRecognitionResultsByElement.clear();
-    m_elementsWithTextRecognitionResults.clear();
+    m_textRecognitionResults.clear();
 }
 
 #endif // ENABLE(IMAGE_ANALYSIS)

Modified: trunk/Source/WebCore/page/Page.h (282764 => 282765)


--- trunk/Source/WebCore/page/Page.h	2021-09-20 18:20:29 UTC (rev 282764)
+++ trunk/Source/WebCore/page/Page.h	2021-09-20 18:42:17 UTC (rev 282765)
@@ -27,6 +27,7 @@
 #include "Document.h"
 #include "FindOptions.h"
 #include "FrameLoaderTypes.h"
+#include "IntRectHash.h"
 #include "LayoutMilestone.h"
 #include "LayoutRect.h"
 #include "LengthBox.h"
@@ -55,6 +56,7 @@
 #include <wtf/Noncopyable.h>
 #include <wtf/Ref.h>
 #include <wtf/UniqueRef.h>
+#include <wtf/WeakHashMap.h>
 #include <wtf/WeakHashSet.h>
 #include <wtf/WeakPtr.h>
 #include <wtf/text/WTFString.h>
@@ -1199,9 +1201,8 @@
     UniqueRef<StorageProvider> m_storageProvider;
 
 #if ENABLE(IMAGE_ANALYSIS)
-    // FIXME: These should be refactored to use a weak hash map of HTMLElement to std::pair<TextRecognitionResult, IntSize>.
-    Vector<std::pair<WeakPtr<HTMLElement>, std::pair<TextRecognitionResult, IntRect>>> m_textRecognitionResultsByElement;
-    WeakHashSet<HTMLElement> m_elementsWithTextRecognitionResults;
+    using CachedTextRecognitionResult = std::pair<TextRecognitionResult, IntRect>;
+    WeakHashMap<HTMLElement, CachedTextRecognitionResult> m_textRecognitionResults;
 #endif
 };
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to