mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
font_for_code_point() was the heaviest function in layout profiles of a YouTube page (216ms CPU out of 2900ms total). Every call walked the full cascade and ran a virtual contains_glyph() against each entry, even though the result is the same for most ASCII code points across a document. Add a 128-entry direct-mapped cache keyed by code point that stores the resolved Font pointer on first lookup. Subsequent ASCII lookups become a null check plus a load. No invalidation is needed: m_fonts is append-only, and the cascade returns the first matching font, so once an entry claims a code point, later appends cannot change the answer.
90 lines
2.5 KiB
C++
90 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibGfx/FontCascadeList.h>
|
|
|
|
namespace Gfx {
|
|
|
|
void FontCascadeList::add(NonnullRefPtr<Font const> font)
|
|
{
|
|
m_fonts.append({ move(font), {} });
|
|
}
|
|
|
|
void FontCascadeList::add(NonnullRefPtr<Font const> font, Vector<UnicodeRange> unicode_ranges)
|
|
{
|
|
if (unicode_ranges.is_empty()) {
|
|
m_fonts.append({ move(font), {} });
|
|
return;
|
|
}
|
|
u32 lowest_code_point = 0xFFFFFFFF;
|
|
u32 highest_code_point = 0;
|
|
|
|
for (auto& range : unicode_ranges) {
|
|
lowest_code_point = min(lowest_code_point, range.min_code_point());
|
|
highest_code_point = max(highest_code_point, range.max_code_point());
|
|
}
|
|
|
|
m_fonts.append({ move(font),
|
|
Entry::RangeData {
|
|
{ lowest_code_point, highest_code_point },
|
|
move(unicode_ranges),
|
|
} });
|
|
}
|
|
|
|
void FontCascadeList::extend(FontCascadeList const& other)
|
|
{
|
|
m_fonts.extend(other.m_fonts);
|
|
}
|
|
|
|
Gfx::Font const& FontCascadeList::font_for_code_point(u32 code_point) const
|
|
{
|
|
if (code_point < m_ascii_cache.size()) {
|
|
if (auto const* cached = m_ascii_cache[code_point])
|
|
return *cached;
|
|
}
|
|
|
|
auto cache_and_return = [&](Font const& font) -> Font const& {
|
|
if (code_point < m_ascii_cache.size())
|
|
m_ascii_cache[code_point] = &font;
|
|
return font;
|
|
};
|
|
|
|
for (auto const& entry : m_fonts) {
|
|
if (entry.range_data.has_value()) {
|
|
if (!entry.range_data->enclosing_range.contains(code_point))
|
|
continue;
|
|
for (auto const& range : entry.range_data->unicode_ranges) {
|
|
if (range.contains(code_point) && entry.font->contains_glyph(code_point))
|
|
return cache_and_return(*entry.font);
|
|
}
|
|
} else if (entry.font->contains_glyph(code_point)) {
|
|
return cache_and_return(*entry.font);
|
|
}
|
|
}
|
|
|
|
if (m_system_font_fallback_callback) {
|
|
if (auto fallback = m_system_font_fallback_callback(code_point, first())) {
|
|
m_fonts.append({ fallback.release_nonnull(), {} });
|
|
return cache_and_return(*m_fonts.last().font);
|
|
}
|
|
}
|
|
|
|
return cache_and_return(*m_last_resort_font);
|
|
}
|
|
|
|
bool FontCascadeList::equals(FontCascadeList const& other) const
|
|
{
|
|
if (m_fonts.size() != other.m_fonts.size())
|
|
return false;
|
|
for (size_t i = 0; i < m_fonts.size(); ++i) {
|
|
if (m_fonts[i].font != other.m_fonts[i].font)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|