浏览代码

LibWeb: Cache the result of Selector::specificity()

This function was showing up as taking 30% of all runtime during a
profile of Browser. This change effectively eliminates it completely.
Idan Horowitz 3 年之前
父节点
当前提交
89bd4cd80d
共有 2 个文件被更改,包括 7 次插入1 次删除
  1. 6 1
      Userland/Libraries/LibWeb/CSS/Selector.cpp
  2. 1 0
      Userland/Libraries/LibWeb/CSS/Selector.h

+ 6 - 1
Userland/Libraries/LibWeb/CSS/Selector.cpp

@@ -20,6 +20,9 @@ Selector::~Selector()
 
 
 u32 Selector::specificity() const
 u32 Selector::specificity() const
 {
 {
+    if (m_specificity.has_value())
+        return *m_specificity;
+
     unsigned ids = 0;
     unsigned ids = 0;
     unsigned tag_names = 0;
     unsigned tag_names = 0;
     unsigned classes = 0;
     unsigned classes = 0;
@@ -42,7 +45,9 @@ u32 Selector::specificity() const
         }
         }
     }
     }
 
 
-    return ids * 0x10000 + classes * 0x100 + tag_names;
+    m_specificity = ids * 0x10000 + classes * 0x100 + tag_names;
+
+    return *m_specificity;
 }
 }
 
 
 // https://www.w3.org/TR/cssom/#serialize-a-simple-selector
 // https://www.w3.org/TR/cssom/#serialize-a-simple-selector

+ 1 - 0
Userland/Libraries/LibWeb/CSS/Selector.h

@@ -138,6 +138,7 @@ private:
     explicit Selector(Vector<CompoundSelector>&&);
     explicit Selector(Vector<CompoundSelector>&&);
 
 
     Vector<CompoundSelector> m_compound_selectors;
     Vector<CompoundSelector> m_compound_selectors;
+    mutable Optional<u32> m_specificity;
 };
 };
 
 
 constexpr StringView pseudo_element_name(Selector::SimpleSelector::PseudoElement);
 constexpr StringView pseudo_element_name(Selector::SimpleSelector::PseudoElement);