Title: [290415] trunk/Source/WTF
Revision
290415
Author
cdu...@apple.com
Date
2022-02-24 00:55:39 -0800 (Thu, 24 Feb 2022)

Log Message

Small optimizations to TinyLRUCache
https://bugs.webkit.org/show_bug.cgi?id=237117

Reviewed by Darin Adler.

Small optimizations to TinyLRUCache:
- When trying to find an entry in the cache, start from the end of the vector
  since this is where the most recent entries are.
- Use reverseFind() instead of a for loop to simplify the code a bit
- Use Vector::uncheckedAppend() instead of Vector::append() to bypass capacity
  checks given that the vector has an inline capacity that is the size of the
  cache (vector capacity never grows or shrinks).

* wtf/TinyLRUCache.h:
(WTF::TinyLRUCache::get):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (290414 => 290415)


--- trunk/Source/WTF/ChangeLog	2022-02-24 07:28:49 UTC (rev 290414)
+++ trunk/Source/WTF/ChangeLog	2022-02-24 08:55:39 UTC (rev 290415)
@@ -1,3 +1,21 @@
+2022-02-24  Chris Dumez  <cdu...@apple.com>
+
+        Small optimizations to TinyLRUCache
+        https://bugs.webkit.org/show_bug.cgi?id=237117
+
+        Reviewed by Darin Adler.
+
+        Small optimizations to TinyLRUCache:
+        - When trying to find an entry in the cache, start from the end of the vector
+          since this is where the most recent entries are.
+        - Use reverseFind() instead of a for loop to simplify the code a bit
+        - Use Vector::uncheckedAppend() instead of Vector::append() to bypass capacity
+          checks given that the vector has an inline capacity that is the size of the
+          cache (vector capacity never grows or shrinks).
+
+        * wtf/TinyLRUCache.h:
+        (WTF::TinyLRUCache::get):
+
 2022-02-23  Chris Dumez  <cdu...@apple.com>
 
         Adopt more widely the new URL constructor that takes in a String

Modified: trunk/Source/WTF/wtf/TinyLRUCache.h (290414 => 290415)


--- trunk/Source/WTF/wtf/TinyLRUCache.h	2022-02-24 07:28:49 UTC (rev 290414)
+++ trunk/Source/WTF/wtf/TinyLRUCache.h	2022-02-24 08:55:39 UTC (rev 290415)
@@ -49,17 +49,14 @@
             return valueForNull;
         }
 
-        for (size_t i = 0; i < m_cache.size(); ++i) {
-            if (m_cache[i].first != key)
-                continue;
-
-            if (i == m_cache.size() - 1)
-                return m_cache[i].second;
-
-            // If the entry is not the last one, move it to the end of the cache.
-            Entry entry = WTFMove(m_cache[i]);
-            m_cache.remove(i);
-            m_cache.append(WTFMove(entry));
+        auto index = m_cache.reverseFindIf([&key](auto& entry) { return entry.first == key; });
+        if (index != notFound) {
+            // Move entry to the end of the cache if necessary.
+            if (index != m_cache.size() - 1) {
+                auto entry = WTFMove(m_cache[index]);
+                m_cache.remove(index);
+                m_cache.uncheckedAppend(WTFMove(entry));
+            }
             return m_cache[m_cache.size() - 1].second;
         }
 
@@ -67,17 +64,17 @@
         if (m_cache.size() == capacity)
             m_cache.remove(0);
 
-        m_cache.append(std::make_pair(Policy::createKeyForStorage(key), Policy::createValueForKey(key)));
+        m_cache.uncheckedAppend(std::pair { Policy::createKeyForStorage(key), Policy::createValueForKey(key) });
         return m_cache.last().second;
     }
 
 private:
-    typedef std::pair<KeyType, ValueType> Entry;
-    typedef Vector<Entry, capacity> Cache;
+    using Entry = std::pair<KeyType, ValueType>;
+    using Cache = Vector<Entry, capacity>;
     Cache m_cache;
 };
 
-}
+} // namespace WTF
 
 using WTF::TinyLRUCache;
 using WTF::TinyLRUCachePolicy;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to