Skip to content

Commit 31e7881

Browse files
committed
[flang-rt] Simplify INDEX with len-1 SUBSTRING.
The len-1 case is noticeably slower than gfortran's straightforward implementation https://github.com/gcc-mirror/gcc/blob/075611b646e5554ae02b2622061ea1614bf16ead/libgfortran/intrinsics/string_intrinsics_inc.c#L253 This change speeds up a simple microkernel by 37% on icelake.
1 parent f73db3d commit 31e7881

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

flang-rt/lib/runtime/character.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,17 @@ inline RT_API_ATTRS std::size_t Index(const CHAR *x, std::size_t xLen,
293293
}
294294
return 0;
295295
}
296+
if (wantLen == 1) {
297+
// Trivial case for single character lookup.
298+
// We can use simple forward search.
299+
CHAR ch = want[0];
300+
for (std::size_t at = 0; at < xLen; ++at) {
301+
if (x[at] == ch) {
302+
return at + 1;
303+
}
304+
}
305+
return 0;
306+
}
296307
// Non-trivial forward substring search: use a simplified form of
297308
// Boyer-Moore substring searching.
298309
for (std::size_t at{1}; at + wantLen - 1 <= xLen;) {

flang-rt/unittests/Runtime/CharacterTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ TYPED_TEST(SearchTests, IndexTests) {
354354
{"", "a", true, 0},
355355
{"aa", "a", false, 1},
356356
{"aa", "a", true, 2},
357+
{"aAA", "A", false, 2},
357358
{"Fortran that I ran", "that I ran", false, 9},
358359
{"Fortran that I ran", "that I ran", true, 9},
359360
{"Fortran that you ran", "that I ran", false, 0},

0 commit comments

Comments
 (0)