Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[web] Cache line break property lookups #19846

Merged
merged 1 commit into from
Jul 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/web_ui/lib/src/engine/text/unicode_range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int? getCodePoint(String text, int index) {
/// has. The properties are then used to decide word boundaries, line break
/// opportunities, etc.
class UnicodePropertyLookup<P> {
const UnicodePropertyLookup(this.ranges, this.defaultProperty);
UnicodePropertyLookup(this.ranges, this.defaultProperty);

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

/// Cache for lookup results.
final Map<int, P> _cache = <int, P>{};

/// Take a [text] and an [index], and returns the property of the character
/// located at that [index].
///
Expand All @@ -147,8 +150,16 @@ class UnicodePropertyLookup<P> {
return defaultProperty;
}

final P? cacheHit = _cache[char];
if (cacheHit != null) {
return cacheHit;
}

final int rangeIndex = _binarySearch(char);
return rangeIndex == -1 ? defaultProperty : ranges[rangeIndex].property;
final P result = rangeIndex == -1 ? defaultProperty : ranges[rangeIndex].property;
// Cache the result.
_cache[char] = result;
return result;
}

int _binarySearch(int value) {
Expand Down