Stephen Adams 182847ec84 [web] More efficient fallback font selection (flutter/engine#44526)
The PR improves the code size and runtime performance of fallback font selection.

### Performance improvements

Initialization of the data structures to support fallback font selection has been moved from creating the FallbackFontManager (first frame) to the first use, i.e. the first frame actually needing a fallback font.

The numbers reported below are for a lightly edited version of the counter demo that appends to the counter about ~300 missing code points that need ~25 fallback fonts to cover the missing code points. Timings taken from a few profiles on my performance workstation.

|  |  Before | After  | 
| --- | ---: | ---: |
| FallbackFontManager() |~100ms | <2ms |
| First need | 0ms | 12ms |
| Subsequent need | 20-30ms | <1ms |

### Size improvements

|  |  Before | After  | Δ |
| --- | ---: | ---: | ---: |
| main.dart.js | 1586405 | 1477319 | -109086 (-6.87%) |
| brotli -9 | 427304 | 401611 | -25693 (-6.01%) |

### Algorithm notes

#### Startup

The old algorithm built an interval tree from the code point ranges of the ~140 fallback fonts and uses the interval tree to build a list of fonts that support each missing code point. The new algorithm uses a binary search map that directly produces the list of fonts. There are fewer binary search ranges (~22k) than the aggregate ranges for all the fonts (~26k).
Most of the startup time gain comes from using a data unpacks directly into a useful form rather than needing processing to build an interval tree (~12ms vs ~100ms).

#### Running

The runtime for font selection is greatly improved for several reasons

- The code point space is partitioned into components so that code point counting can be batched.
- When a font is selected, the counts are updated incrementally rather than being recomputed.
- The counts are held in fields of the NotoFont and component objects rather than in Maps or Sets.

Batching, incremental update and avoiding hash tables are roughly multiplicative in effect.

## Issues

- https://github.com/flutter/flutter/issues/131440

*If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
2023-08-31 22:42:20 +00:00
Languages
Dart 75%
C++ 16.5%
Objective-C++ 2.9%
Java 2.8%
Objective-C 0.7%
Other 1.9%