LibGfx+LibWeb: Append glyphs in LineBoxFragment run more efficiently
Some checks are pending
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run

Since we know the number of glyphs we're going to append, ensure the
LineBoxFragment run's capacity once and perform unchecked appends.
This commit is contained in:
Jelle Raaijmakers 2025-06-17 14:40:08 +02:00 committed by Alexander Kalenik
commit 46a46a1c61
Notes: github-actions[bot] 2025-06-17 15:04:38 +00:00
2 changed files with 17 additions and 10 deletions

View file

@ -61,8 +61,6 @@ public:
[[nodiscard]] bool is_empty() const { return m_glyphs.is_empty(); }
[[nodiscard]] float width() const { return m_width; }
void append(DrawGlyph glyph) { m_glyphs.append(glyph); }
private:
Vector<DrawGlyph> m_glyphs;
NonnullRefPtr<Font const> m_font;

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -101,18 +102,22 @@ void LineBoxFragment::append_glyph_run(RefPtr<Gfx::GlyphRun> const& glyph_run, C
void LineBoxFragment::append_glyph_run_ltr(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
{
auto run_direction = resolve_glyph_run_direction(glyph_run->text_type());
auto inline_offset = m_inline_length.to_float();
if (m_current_insert_direction != run_direction) {
if (run_direction == CSS::Direction::Rtl)
m_insert_position = m_inline_length.to_float();
m_insert_position = inline_offset;
m_current_insert_direction = run_direction;
}
auto& glyphs = m_glyph_run->glyphs();
glyphs.ensure_capacity(glyphs.size() + glyph_run->glyphs().size());
switch (run_direction) {
case CSS::Direction::Ltr:
for (auto& glyph : glyph_run->glyphs()) {
glyph.position.translate_by(m_inline_length.to_float(), 0);
m_glyph_run->append(glyph);
glyph.position.translate_by(inline_offset, 0);
glyphs.unchecked_append(glyph);
}
break;
case CSS::Direction::Rtl:
@ -122,7 +127,7 @@ void LineBoxFragment::append_glyph_run_ltr(RefPtr<Gfx::GlyphRun> const& glyph_ru
}
for (auto& glyph : glyph_run->glyphs()) {
glyph.position.translate_by(m_insert_position, 0);
m_glyph_run->append(glyph);
glyphs.unchecked_append(glyph);
}
break;
}
@ -133,6 +138,7 @@ void LineBoxFragment::append_glyph_run_ltr(RefPtr<Gfx::GlyphRun> const& glyph_ru
void LineBoxFragment::append_glyph_run_rtl(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width)
{
auto run_direction = resolve_glyph_run_direction(glyph_run->text_type());
auto run_offset = run_width.to_float();
if (m_current_insert_direction != run_direction) {
if (run_direction == CSS::Direction::Ltr)
@ -140,25 +146,28 @@ void LineBoxFragment::append_glyph_run_rtl(RefPtr<Gfx::GlyphRun> const& glyph_ru
m_current_insert_direction = run_direction;
}
auto& glyphs = m_glyph_run->glyphs();
glyphs.ensure_capacity(glyphs.size() + glyph_run->glyphs().size());
switch (run_direction) {
case CSS::Direction::Ltr:
for (auto& glyph : m_glyph_run->glyphs()) {
if (glyph.position.x() >= m_insert_position)
glyph.position.translate_by(run_width.to_float(), 0);
glyph.position.translate_by(run_offset, 0);
}
for (auto& glyph : glyph_run->glyphs()) {
glyph.position.translate_by(m_insert_position, 0);
m_glyph_run->append(glyph);
glyphs.unchecked_append(glyph);
}
break;
case CSS::Direction::Rtl:
if (glyph_run->text_type() != Gfx::GlyphRun::TextType::EndPadding) {
for (auto& glyph : m_glyph_run->glyphs()) {
glyph.position.translate_by(run_width.to_float(), 0);
glyph.position.translate_by(run_offset, 0);
}
}
for (auto& glyph : glyph_run->glyphs()) {
m_glyph_run->append(glyph);
glyphs.unchecked_append(glyph);
}
break;
}