From 7f6e8c5536fd13a56b4bd030233960aa1af38d05 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 17 May 2022 16:58:35 -0600 Subject: [PATCH] [ot-tags] Optimize subtag_matches() further Part of https://github.com/harfbuzz/harfbuzz/issues/3591 Comparing before to after Benchmark Time CPU Time Old Time New CPU Old CPU New ---------------------------------------------------------------------------------------------------------------------------------------------------- BM_hb_ot_tags_from_script_and_language/COMMON abcd_XY -0.3371 -0.3371 71 47 71 47 --- src/hb-ot-tag.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/hb-ot-tag.cc b/src/hb-ot-tag.cc index c5630fb31..8d63eb791 100644 --- a/src/hb-ot-tag.cc +++ b/src/hb-ot-tag.cc @@ -195,6 +195,10 @@ subtag_matches (const char *lang_str, const char *subtag) { unsigned subtag_len = strlen (subtag); + + if (likely ((unsigned) (limit - lang_str) < subtag_len)) + return false; + do { const char *s = strstr (lang_str, subtag); if (!s || s >= limit)