[ot-tags] Optimize language comparison

Now that we know both strings are of equal len of 2 or 3, optimize.

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

Before:
------------------------------------------------------------------------------------------------
Benchmark                                                      Time             CPU   Iterations
------------------------------------------------------------------------------------------------
BM_hb_ot_tags_from_script_and_language/COMMON abcd_XY       8.50 ns         8.47 ns     81221549
BM_hb_ot_tags_from_script_and_language/COMMON zh_CN         79.6 ns         79.3 ns      8785804
BM_hb_ot_tags_from_script_and_language/COMMON en_US         40.0 ns         39.9 ns     17462768
BM_hb_ot_tags_from_script_and_language/LATIN en_US          39.2 ns         39.1 ns     17886793
BM_hb_ot_tags_from_script_and_language/COMMON none          4.31 ns         4.30 ns    162805417
BM_hb_ot_tags_from_script_and_language/LATIN none           4.32 ns         4.31 ns    162656688

After:
------------------------------------------------------------------------------------------------
Benchmark                                                      Time             CPU   Iterations
------------------------------------------------------------------------------------------------
BM_hb_ot_tags_from_script_and_language/COMMON abcd_XY       8.27 ns         8.24 ns     81868701
BM_hb_ot_tags_from_script_and_language/COMMON zh_CN         56.1 ns         56.0 ns     12353284
BM_hb_ot_tags_from_script_and_language/COMMON en_US         24.3 ns         24.2 ns     28955030
BM_hb_ot_tags_from_script_and_language/LATIN en_US          24.5 ns         24.4 ns     28664868
BM_hb_ot_tags_from_script_and_language/COMMON none          4.35 ns         4.34 ns    161190014
BM_hb_ot_tags_from_script_and_language/LATIN none           4.36 ns         4.34 ns    161319000
This commit is contained in:
Behdad Esfahbod 2022-05-17 15:19:40 -06:00
parent dde48d78c1
commit c1f4b57c06
2 changed files with 14 additions and 19 deletions

View File

@ -352,14 +352,15 @@ struct hb_sorted_array_t :
unsigned int i;
return bfind (x, &i) ? &this->arrayZ[i] : not_found;
}
template <typename T>
template <typename T, typename ...Ts>
bool bfind (const T &x, unsigned int *i = nullptr,
hb_not_found_t not_found = HB_NOT_FOUND_DONT_STORE,
unsigned int to_store = (unsigned int) -1) const
unsigned int to_store = (unsigned int) -1,
Ts... ds) const
{
unsigned pos;
if (bsearch_impl (x, &pos))
if (bsearch_impl (x, &pos, ds...))
{
if (i)
*i = pos;
@ -384,15 +385,16 @@ struct hb_sorted_array_t :
}
return false;
}
template <typename T>
bool bsearch_impl (const T &x, unsigned *pos) const
template <typename T, typename ...Ts>
bool bsearch_impl (const T &x, unsigned *pos, Ts... ds) const
{
return hb_bsearch_impl (pos,
x,
this->arrayZ,
this->length,
sizeof (Type),
_hb_cmp_method<T, Type>);
_hb_cmp_method<T, Type, Ts...>,
ds...);
}
};
template <typename T> inline hb_sorted_array_t<T>

View File

@ -219,22 +219,13 @@ struct LangTag
char language[4];
hb_tag_t tag;
int cmp (const char *a) const
int cmp (const char *a, unsigned len) const
{
const char *b = this->language;
unsigned int da, db;
const char *p;
p = strchr (a, '-');
da = p ? (unsigned int) (p - a) : strlen (a);
p = strchr (b, '-');
db = p ? (unsigned int) (p - b) : strlen (b);
return strncmp (a, b, hb_max (da, db));
return strncmp (a, b, len);
}
int cmp (const LangTag *that) const
{ return cmp (that->language); }
{ return cmp (that->language, strlen (that->language)); }
};
#include "hb-ot-tag-table.hh"
@ -298,7 +289,9 @@ hb_ot_tags_from_language (const char *lang_str,
ot_languages = ot_languages3;
ot_languages_len = ARRAY_LENGTH (ot_languages3);
}
if (hb_sorted_array (ot_languages, ot_languages_len).bfind (lang_str, &tag_idx))
if (hb_sorted_array (ot_languages, ot_languages_len).bfind (lang_str, &tag_idx,
HB_NOT_FOUND_DONT_STORE, (unsigned) -1,
first_len))
{
unsigned int i;
while (tag_idx != 0 &&