mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 09:28:13 +00:00
LibWeb: Compress specificity into a 32-bit unsigned int
Instead of storing the three-part specificy for every selector, just mash them together into a 32-bit value instead. This saves both space and time, and matches the behavior of other browser engines.
This commit is contained in:
parent
8be74ea65c
commit
49dd4b7e8a
4 changed files with 6 additions and 68 deletions
|
@ -37,7 +37,7 @@ Selector::~Selector()
|
|||
{
|
||||
}
|
||||
|
||||
Specificity Selector::specificity() const
|
||||
u32 Selector::specificity() const
|
||||
{
|
||||
unsigned ids = 0;
|
||||
unsigned tag_names = 0;
|
||||
|
@ -61,7 +61,7 @@ Specificity Selector::specificity() const
|
|||
}
|
||||
}
|
||||
|
||||
return { ids, classes, tag_names };
|
||||
return ids * 0x10000 + classes * 0x100 + tag_names;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -91,7 +91,7 @@ public:
|
|||
|
||||
const Vector<ComplexSelector>& complex_selectors() const { return m_complex_selectors; }
|
||||
|
||||
Specificity specificity() const;
|
||||
u32 specificity() const;
|
||||
|
||||
private:
|
||||
Vector<ComplexSelector> m_complex_selectors;
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Web {
|
||||
|
||||
class Specificity {
|
||||
public:
|
||||
Specificity(unsigned ids, unsigned classes, unsigned tag_names)
|
||||
: m_ids(ids)
|
||||
, m_classes(classes)
|
||||
, m_tag_names(tag_names)
|
||||
{
|
||||
}
|
||||
|
||||
unsigned ids() const { return m_ids; }
|
||||
unsigned classes() const { return m_classes; }
|
||||
unsigned tag_names() const { return m_tag_names; }
|
||||
|
||||
bool operator<(const Specificity& other) const
|
||||
{
|
||||
return m_ids < other.m_ids
|
||||
|| m_classes < other.m_classes
|
||||
|| m_tag_names < other.m_tag_names;
|
||||
}
|
||||
|
||||
bool operator==(const Specificity& other) const
|
||||
{
|
||||
return m_ids == other.m_ids
|
||||
|| m_classes == other.m_classes
|
||||
|| m_tag_names == other.m_tag_names;
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned m_ids { 0 };
|
||||
unsigned m_classes { 0 };
|
||||
unsigned m_tag_names { 0 };
|
||||
};
|
||||
|
||||
}
|
|
@ -497,12 +497,14 @@ NonnullRefPtr<StyleProperties> StyleResolver::resolve_style(const Element& eleme
|
|||
quick_sort(matching_rules, [&](MatchingRule& a, MatchingRule& b) {
|
||||
auto& a_selector = a.rule->selectors()[a.selector_index];
|
||||
auto& b_selector = b.rule->selectors()[b.selector_index];
|
||||
auto a_specificity = a_selector.specificity();
|
||||
auto b_specificity = b_selector.specificity();
|
||||
if (a_selector.specificity() == b_selector.specificity()) {
|
||||
if (a.style_sheet_index == b.style_sheet_index)
|
||||
return a.rule_index < b.rule_index;
|
||||
return a.style_sheet_index < b.style_sheet_index;
|
||||
}
|
||||
return a_selector.specificity() < b_selector.specificity();
|
||||
return a_specificity < b_specificity;
|
||||
});
|
||||
|
||||
for (auto& match : matching_rules) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue