Skip to content

Commit f831af3

Browse files
authored
[web] Cache line break property lookups (flutter#19846)
1 parent e51642e commit f831af3

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/web_ui/lib/src/engine/text/unicode_range.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ int? getCodePoint(String text, int index) {
107107
/// has. The properties are then used to decide word boundaries, line break
108108
/// opportunities, etc.
109109
class UnicodePropertyLookup<P> {
110-
const UnicodePropertyLookup(this.ranges, this.defaultProperty);
110+
UnicodePropertyLookup(this.ranges, this.defaultProperty);
111111

112112
/// Creates a [UnicodePropertyLookup] from packed line break data.
113113
factory UnicodePropertyLookup.fromPackedData(
@@ -129,6 +129,9 @@ class UnicodePropertyLookup<P> {
129129
/// known range.
130130
final P defaultProperty;
131131

132+
/// Cache for lookup results.
133+
final Map<int, P> _cache = <int, P>{};
134+
132135
/// Take a [text] and an [index], and returns the property of the character
133136
/// located at that [index].
134137
///
@@ -147,8 +150,16 @@ class UnicodePropertyLookup<P> {
147150
return defaultProperty;
148151
}
149152

153+
final P? cacheHit = _cache[char];
154+
if (cacheHit != null) {
155+
return cacheHit;
156+
}
157+
150158
final int rangeIndex = _binarySearch(char);
151-
return rangeIndex == -1 ? defaultProperty : ranges[rangeIndex].property;
159+
final P result = rangeIndex == -1 ? defaultProperty : ranges[rangeIndex].property;
160+
// Cache the result.
161+
_cache[char] = result;
162+
return result;
152163
}
153164

154165
int _binarySearch(int value) {

0 commit comments

Comments
 (0)