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

@ -1225,7 +1225,7 @@ void EventHandler::update_selection_range_for_input_or_textarea()
Unicode::Segmenter& EventHandler::word_segmenter()
{
if (!m_word_segmenter)
m_word_segmenter = Unicode::Segmenter::create(Unicode::SegmenterGranularity::Word);
m_word_segmenter = m_navigable->active_document()->word_segmenter().clone();
return *m_word_segmenter;
}