Bug 1869223 - Add fast path to CountGraphemeClusters when text is empty. r=TYLin

When reloading https://en.wikipedia.org/wiki/Barack_Obama that is used by
browsertime benchmark, `CountGraphemeClusters` is called around 3000 times.
But half calls are that `aText` is empty.

So if we add fast path for empty text, we can avoid a lot of heap allocations
of `ICU4XGraphemeClusterBreakIteratorUtf16`.

Differential Revision: https://phabricator.services.mozilla.com/D196008
This commit is contained in:
Makoto Kato 2023-12-12 00:59:27 +00:00
parent 524584064e
commit 39dae322a4

View file

@ -177,6 +177,10 @@ bool IsClusterExtenderExcludingJoiners(uint32_t aCh, uint8_t aCategory) {
}
uint32_t CountGraphemeClusters(Span<const char16_t> aText) {
if (aText.IsEmpty()) {
// Fast path for empty text.
return 0;
}
intl::GraphemeClusterBreakIteratorUtf16 iter(aText);
uint32_t result = 0;
while (iter.Next()) {