Skip to content

Commit c724816

Browse files
committed
Another incremental optimization for #904
1 parent 543633d commit c724816

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

regression-tests/test-results/clang-12-c++20/run-tests-clang-12.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ for f in *.cpp
1010
do
1111
let count=count+1
1212
printf "[%s] Starting clang++-12 %s\n" "$count" "$f"
13-
clang++-12 -I../../../include -std=c++2b -pthread -Wunused-parameter -o test.exe $f > $f.output 2>&1
13+
clang++-12 -I../../../include -std=c++20 -pthread -Wunused-parameter -o test.exe $f > $f.output 2>&1
1414
rm -f $f
1515
if test -f "test.exe"; then
1616
let exe_count=exe_count+1

source/sema.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,21 +894,37 @@ class sema
894894
// and is in the same function
895895
using I = stable_vector<symbol>::const_iterator;
896896
auto advance = [](I& i, int n, I bound) { // TODO Use `std::ranges::advance`
897-
CPP2_SCOPE_TIMER("get_declaration_of_new - phase 2a 'advance' part of loop");
897+
//CPP2_SCOPE_TIMER("get_declaration_of_new - phase 2a 'advance' part of loop");
898898
auto in = i;
899899
if (std::abs(n) >= std::abs(bound - i)) {
900900
i = bound;
901901
}
902902
else {
903-
CPP2_SCOPE_TIMER("get_declaration_of_new - phase 2aa 'std::advance' specifically");
903+
//CPP2_SCOPE_TIMER("get_declaration_of_new - phase 2aa 'std::advance' specifically");
904904
std::advance(i, n);
905905
}
906906
return n - (i - in);
907907
};
908908
advance(i, -int(i->position() > t.position()), symbols.cbegin());
909909
advance(i, 1, symbols.cend());
910-
while (advance(i, -1, symbols.begin()) == 0)
910+
911+
// --- PERFORMANCE NOTE ---
912+
// In this hot function, this is a hot loop, and changing this lambda call...
913+
//
914+
// while (advance(i, -1, symbols.begin()) == 0)
915+
// {
916+
//
917+
// ... to this instead...
918+
//
919+
while (i != symbols.begin())
911920
{
921+
--i;
922+
//
923+
// ... is a 25% performance improvement on this entire loop in the `pure2-last-use.cpp2`
924+
// test code. That's unexpected, so documenting it so we don't inadvertently change it
925+
// back to `advance` "for consistency" with the previous lines.
926+
// --- END PERFORMANCE NOTE ---
927+
912928
if (
913929
i->sym.index() == symbol::active::declaration
914930
&& i->depth <= depth
@@ -942,7 +958,7 @@ class sema
942958
return &decl;
943959
}
944960

945-
CPP2_SCOPE_TIMER("get_declaration_of_new - phase 2b 'move this' part of loop");
961+
//CPP2_SCOPE_TIMER("get_declaration_of_new - phase 2b 'move this' part of loop");
946962

947963
// If we reached a 'move this' parameter, look it up in the type members
948964
if (

0 commit comments

Comments
 (0)