[ot-tag] Cache last bsearch result

Part of https://github.com/harfbuzz/harfbuzz/issues/3591

Humm. Looks like not all of the fat is bsearch overhead now. I cached
the last bsearch result, but most of the time is still there. I'm
baffled.

Before:
------------------------------------------------------------------------------------------------
Benchmark                                                      Time             CPU   Iterations
------------------------------------------------------------------------------------------------
BM_hb_ot_tags_from_script_and_language/COMMON abcd_XY       8.08 ns         8.05 ns     84500482
BM_hb_ot_tags_from_script_and_language/COMMON zh_CN         42.2 ns         42.1 ns     16722006
BM_hb_ot_tags_from_script_and_language/COMMON en_US         16.1 ns         16.0 ns     43461527
BM_hb_ot_tags_from_script_and_language/LATIN en_US          16.5 ns         16.5 ns     42448505
BM_hb_ot_tags_from_script_and_language/COMMON none          4.34 ns         4.33 ns    161290530
BM_hb_ot_tags_from_script_and_language/LATIN none           4.34 ns         4.33 ns    162339799

After:
------------------------------------------------------------------------------------------------
Benchmark                                                      Time             CPU   Iterations
------------------------------------------------------------------------------------------------
BM_hb_ot_tags_from_script_and_language/COMMON abcd_XY       8.13 ns         8.11 ns     80438134
BM_hb_ot_tags_from_script_and_language/COMMON zh_CN         40.0 ns         39.9 ns     17487939
BM_hb_ot_tags_from_script_and_language/COMMON en_US         12.7 ns         12.7 ns     55124394
BM_hb_ot_tags_from_script_and_language/LATIN en_US          13.1 ns         13.0 ns     53660125
BM_hb_ot_tags_from_script_and_language/COMMON none          4.61 ns         4.60 ns    151394104
BM_hb_ot_tags_from_script_and_language/LATIN none           4.70 ns         4.68 ns    150402847
This commit is contained in:
Behdad Esfahbod 2022-05-17 16:21:02 -06:00
parent 909f00ac6e
commit dfca47f419
1 changed files with 9 additions and 2 deletions

View File

@ -257,7 +257,6 @@ hb_ot_tags_from_language (const char *lang_str,
hb_tag_t *tags) hb_tag_t *tags)
{ {
const char *s; const char *s;
unsigned int tag_idx;
/* Check for matches of multiple subtags. */ /* Check for matches of multiple subtags. */
if (hb_ot_tags_from_complex_language (lang_str, limit, count, tags)) if (hb_ot_tags_from_complex_language (lang_str, limit, count, tags))
@ -288,8 +287,16 @@ hb_ot_tags_from_language (const char *lang_str,
ot_languages = ot_languages3; ot_languages = ot_languages3;
ot_languages_len = ARRAY_LENGTH (ot_languages3); ot_languages_len = ARRAY_LENGTH (ot_languages3);
} }
if (hb_sorted_array (ot_languages, ot_languages_len).bfind (hb_tag_from_string (lang_str, first_len), &tag_idx))
hb_tag_t lang_tag = hb_tag_from_string (lang_str, first_len);
static unsigned last_tag_idx; /* Poor man's cache. */
unsigned tag_idx = last_tag_idx;
if ((tag_idx < ot_languages_len && ot_languages[tag_idx].language == lang_tag) ||
hb_sorted_array (ot_languages, ot_languages_len).bfind (lang_tag, &tag_idx))
{ {
last_tag_idx = tag_idx;
unsigned int i; unsigned int i;
while (tag_idx != 0 && while (tag_idx != 0 &&
ot_languages[tag_idx].language == ot_languages[tag_idx - 1].language) ot_languages[tag_idx].language == ot_languages[tag_idx - 1].language)