From 39dae322a4944da1e6874c088177faeb3f499c1e Mon Sep 17 00:00:00 2001 From: Makoto Kato Date: Tue, 12 Dec 2023 00:59:27 +0000 Subject: [PATCH] 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 --- intl/unicharutil/util/nsUnicodeProperties.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/intl/unicharutil/util/nsUnicodeProperties.cpp b/intl/unicharutil/util/nsUnicodeProperties.cpp index ea7fa80ea92b..748a2d32c246 100644 --- a/intl/unicharutil/util/nsUnicodeProperties.cpp +++ b/intl/unicharutil/util/nsUnicodeProperties.cpp @@ -177,6 +177,10 @@ bool IsClusterExtenderExcludingJoiners(uint32_t aCh, uint8_t aCategory) { } uint32_t CountGraphemeClusters(Span aText) { + if (aText.IsEmpty()) { + // Fast path for empty text. + return 0; + } intl::GraphemeClusterBreakIteratorUtf16 iter(aText); uint32_t result = 0; while (iter.Next()) {