<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[282765] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/282765">282765</a></dd>
<dt>Author</dt> <dd>wenson_hsieh@apple.com</dd>
<dt>Date</dt> <dd>2021-09-20 11:42:17 -0700 (Mon, 20 Sep 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>[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:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorepagePagecpp">trunk/Source/WebCore/page/Page.cpp</a></li>
<li><a href="#trunkSourceWebCorepagePageh">trunk/Source/WebCore/page/Page.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (282764 => 282765)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,3 +1,20 @@
</span><ins>+2021-09-20  Wenson Hsieh  <wenson_hsieh@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:
+
</ins><span class="cx"> 2021-09-20  Antti Koivisto  <antti@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [LFC][Integration] Enable selections
</span></span></pre></div>
<a id="trunkSourceWebCorepagePagecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/Page.cpp (282764 => 282765)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -3629,24 +3629,22 @@
</span><span class="cx"> 
</span><span class="cx"> void Page::updateElementsWithTextRecognitionResults()
</span><span class="cx"> {
</span><del>-    if (m_textRecognitionResultsByElement.isEmpty())
</del><ins>+    if (m_textRecognitionResults.isEmptyIgnoringNullReferences())
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    m_textRecognitionResultsByElement.removeAllMatching([](auto& elementAndResult) {
-        return !elementAndResult.first;
-    });
</del><ins>+    m_textRecognitionResults.removeNullReferences();
</ins><span class="cx"> 
</span><span class="cx">     Vector<std::pair<Ref<HTMLElement>, TextRecognitionResult>> elementsToUpdate;
</span><del>-    for (auto& [element, resultAndRect] : m_textRecognitionResultsByElement) {
-        if (!element->isConnected())
</del><ins>+    for (auto entry : m_textRecognitionResults) {
+        Ref protectedElement = entry.key;
+        if (!protectedElement->isConnected())
</ins><span class="cx">             continue;
</span><span class="cx"> 
</span><del>-        auto& [result, containerRect] = resultAndRect;
-        auto protectedElement = makeRef(*element);
</del><span class="cx">         auto renderer = protectedElement->renderer();
</span><span class="cx">         if (!is<RenderImage>(renderer))
</span><span class="cx">             continue;
</span><span class="cx"> 
</span><ins>+        auto& [result, containerRect] = entry.value;
</ins><span class="cx">         auto newContainerRect = protectedElement->containerRectForTextRecognition();
</span><span class="cx">         if (containerRect == newContainerRect)
</span><span class="cx">             continue;
</span><span class="lines">@@ -3668,31 +3666,17 @@
</span><span class="cx"> 
</span><span class="cx"> bool Page::hasCachedTextRecognitionResult(const HTMLElement& element) const
</span><span class="cx"> {
</span><del>-    return m_elementsWithTextRecognitionResults.contains(element);
</del><ins>+    return m_textRecognitionResults.contains(element);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void Page::cacheTextRecognitionResult(const HTMLElement& element, const IntRect& containerRect, const TextRecognitionResult& result)
</span><span class="cx"> {
</span><del>-    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;
-    });
</del><ins>+    m_textRecognitionResults.set(element, CachedTextRecognitionResult { result, containerRect });
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void Page::resetTextRecognitionResults()
</span><span class="cx"> {
</span><del>-    m_textRecognitionResultsByElement.clear();
-    m_elementsWithTextRecognitionResults.clear();
</del><ins>+    m_textRecognitionResults.clear();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #endif // ENABLE(IMAGE_ANALYSIS)
</span></span></pre></div>
<a id="trunkSourceWebCorepagePageh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/Page.h (282764 => 282765)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx"> #include "Document.h"
</span><span class="cx"> #include "FindOptions.h"
</span><span class="cx"> #include "FrameLoaderTypes.h"
</span><ins>+#include "IntRectHash.h"
</ins><span class="cx"> #include "LayoutMilestone.h"
</span><span class="cx"> #include "LayoutRect.h"
</span><span class="cx"> #include "LengthBox.h"
</span><span class="lines">@@ -55,6 +56,7 @@
</span><span class="cx"> #include <wtf/Noncopyable.h>
</span><span class="cx"> #include <wtf/Ref.h>
</span><span class="cx"> #include <wtf/UniqueRef.h>
</span><ins>+#include <wtf/WeakHashMap.h>
</ins><span class="cx"> #include <wtf/WeakHashSet.h>
</span><span class="cx"> #include <wtf/WeakPtr.h>
</span><span class="cx"> #include <wtf/text/WTFString.h>
</span><span class="lines">@@ -1199,9 +1201,8 @@
</span><span class="cx">     UniqueRef<StorageProvider> m_storageProvider;
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(IMAGE_ANALYSIS)
</span><del>-    // 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;
</del><ins>+    using CachedTextRecognitionResult = std::pair<TextRecognitionResult, IntRect>;
+    WeakHashMap<HTMLElement, CachedTextRecognitionResult> m_textRecognitionResults;
</ins><span class="cx"> #endif
</span><span class="cx"> };
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>