未验证 提交 f831af39 编写于 作者: M Mouad Debbar 提交者: GitHub

[web] Cache line break property lookups (#19846)

上级 e51642e4
...@@ -107,7 +107,7 @@ int? getCodePoint(String text, int index) { ...@@ -107,7 +107,7 @@ int? getCodePoint(String text, int index) {
/// has. The properties are then used to decide word boundaries, line break /// has. The properties are then used to decide word boundaries, line break
/// opportunities, etc. /// opportunities, etc.
class UnicodePropertyLookup<P> { class UnicodePropertyLookup<P> {
const UnicodePropertyLookup(this.ranges, this.defaultProperty); UnicodePropertyLookup(this.ranges, this.defaultProperty);
/// Creates a [UnicodePropertyLookup] from packed line break data. /// Creates a [UnicodePropertyLookup] from packed line break data.
factory UnicodePropertyLookup.fromPackedData( factory UnicodePropertyLookup.fromPackedData(
...@@ -129,6 +129,9 @@ class UnicodePropertyLookup<P> { ...@@ -129,6 +129,9 @@ class UnicodePropertyLookup<P> {
/// known range. /// known range.
final P defaultProperty; 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 /// Take a [text] and an [index], and returns the property of the character
/// located at that [index]. /// located at that [index].
/// ///
...@@ -147,8 +150,16 @@ class UnicodePropertyLookup<P> { ...@@ -147,8 +150,16 @@ class UnicodePropertyLookup<P> {
return defaultProperty; return defaultProperty;
} }
final P? cacheHit = _cache[char];
if (cacheHit != null) {
return cacheHit;
}
final int rangeIndex = _binarySearch(char); 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) { int _binarySearch(int value) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册