From 12419d31c853cf2f82487734acf854ebb3a51e5d Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Thu, 9 Jul 2020 12:14:41 -0700 Subject: [PATCH] [web] Cache line break property lookups --- lib/web_ui/lib/src/engine/text/unicode_range.dart | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/web_ui/lib/src/engine/text/unicode_range.dart b/lib/web_ui/lib/src/engine/text/unicode_range.dart index d686caafe0c45..8c8b71530e299 100644 --- a/lib/web_ui/lib/src/engine/text/unicode_range.dart +++ b/lib/web_ui/lib/src/engine/text/unicode_range.dart @@ -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

{ - const UnicodePropertyLookup(this.ranges, this.defaultProperty); + UnicodePropertyLookup(this.ranges, this.defaultProperty); /// Creates a [UnicodePropertyLookup] from packed line break data. factory UnicodePropertyLookup.fromPackedData( @@ -129,6 +129,9 @@ class UnicodePropertyLookup

{ /// known range. final P defaultProperty; + /// Cache for lookup results. + final Map _cache = {}; + /// Take a [text] and an [index], and returns the property of the character /// located at that [index]. /// @@ -147,8 +150,16 @@ class UnicodePropertyLookup

{ 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) {