LibWeb: Move initial creation of Unicode segmenters to the Document

The expensive part of creating a segmenter is doing the locale and UCD
data lookups at creation time. Instead of doing this once per text node,
cache the segmenters on the document, and clone them as needed (cloning
is much, much cheaper).

On a profile loading Ladybird's GitHub repo, the following hot methods
changed as follows:

    ChunkIterator ctor: 6.08% -> 0.21%
    Segmenter factory:  5.86% ->    0%
    Segmenter clone:    N/A   -> 0.09%
This commit is contained in:
Timothy Flynn 2024-09-22 10:03:23 -04:00 committed by Andreas Kling
commit 5d71758742
Notes: github-actions[bot] 2024-09-22 16:43:21 +00:00
8 changed files with 52 additions and 18 deletions

View file

@ -39,7 +39,7 @@ public:
class ChunkIterator {
public:
ChunkIterator(String const& text, bool wrap_lines, bool respect_linebreaks, Gfx::FontCascadeList const&);
ChunkIterator(TextNode const&, bool wrap_lines, bool respect_linebreaks);
Optional<Chunk> next();
Optional<Chunk> peek(size_t);
@ -53,7 +53,7 @@ public:
Utf8View m_utf8_view;
Gfx::FontCascadeList const& m_font_cascade_list;
NonnullOwnPtr<Unicode::Segmenter> m_segmenter;
Unicode::Segmenter& m_grapheme_segmenter;
size_t m_current_index { 0 };
Vector<Chunk> m_peek_queue;
@ -62,12 +62,15 @@ public:
void invalidate_text_for_rendering();
void compute_text_for_rendering();
Unicode::Segmenter& grapheme_segmenter() const;
virtual JS::GCPtr<Painting::Paintable> create_paintable() const override;
private:
virtual bool is_text_node() const final { return true; }
Optional<String> m_text_for_rendering;
mutable OwnPtr<Unicode::Segmenter> m_grapheme_segmenter;
};
template<>