From 77941b98d572f3522096bf74deea9a66741b37d0 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Tue, 8 Aug 2023 11:44:21 -0700 Subject: [PATCH] [Impeller] improved glyph hashing performance (flutter/engine#44502) This change makes the glyph hash 39% faster (66ms vs 107ms). The glyph hash function occupies the most CPU time on the raster thread when scrolling around the main screen for the Flutter Gallery ## before Screenshot 2023-08-08 at 10 53 06 AM ## after Screenshot 2023-08-08 at 10 52 51 AM [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../impeller/typographer/font_glyph_pair.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/impeller/typographer/font_glyph_pair.h b/engine/src/flutter/impeller/typographer/font_glyph_pair.h index 0b89c803af..16eaecdc66 100644 --- a/engine/src/flutter/impeller/typographer/font_glyph_pair.h +++ b/engine/src/flutter/impeller/typographer/font_glyph_pair.h @@ -31,8 +31,20 @@ struct FontGlyphPair { struct Hash { std::size_t operator()(const FontGlyphPair& p) const { - return fml::HashCombine(p.font.GetHash(), p.glyph.index, p.glyph.type, - p.scale); + static_assert(sizeof(p.glyph.index) == 2); + static_assert(sizeof(p.glyph.type) == 1); + size_t index = p.glyph.index; + size_t type = static_cast(p.glyph.type); + // By packaging multiple values in a single size_t the hash function is + // more efficient without losing entropy. + if (sizeof(size_t) == 8 && sizeof(Scalar) == 4) { + const float* fScale = &p.scale; + size_t nScale = *reinterpret_cast(fScale); + size_t index_type_scale = nScale << 32 | index << 16 | type; + return fml::HashCombine(p.font.GetHash(), index_type_scale); + } + size_t index_type = index << 16 | type; + return fml::HashCombine(p.font.GetHash(), index_type, p.scale); } }; struct Equal {