Skip to content

Commit b77ac91

Browse files
committed
Disable debug timers in non-CPP2_DEBUG_BUILD builds
And improve timer performance by removing `make_shared`
1 parent 8cbc2a8 commit b77ac91

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

Diff for: source/common.h

+20-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
#include "cpp2util.h"
2424

2525

26+
//===========================================================================
27+
// When defined, builds cppfront in an instrumented (and slower) mode that
28+
// prints more information under -_debug
29+
//===========================================================================
30+
31+
//#define CPP2_DEBUG_BUILD
32+
33+
2634
//===========================================================================
2735
// Common types
2836
//===========================================================================
@@ -1005,6 +1013,7 @@ class stable_vector
10051013
}
10061014

10071015
auto size() const -> size_t {
1016+
testing.enforce(!data.empty());
10081017
return (data.size() - 1) * PageSize + data.back().size();
10091018
}
10101019

@@ -1359,16 +1368,19 @@ class timer
13591368

13601369
static std::unordered_map<std::string_view, timer> timers; // global named timers
13611370

1362-
auto scope_timer(std::string_view name) -> std::shared_ptr<void> {
1363-
if (flag_internal_debug) {
1364-
timers[name].start();
1365-
auto stop = [=]{ timers[name].stop(); };
1366-
return std::make_shared<finally<decltype(stop)>>( std::move(stop) );
1367-
}
1368-
// Else
1369-
return {};
1371+
auto scope_timer(std::string_view name) {
1372+
timers[name].start();
1373+
auto stop = [=]{ timers[name].stop(); };
1374+
return finally( std::move(stop) );
13701375
}
13711376

1377+
#ifdef CPP2_DEBUG_BUILD
1378+
#define CPP2_CONCAT(x,y) x##y
1379+
#define CPP2_UNIQUE_NAME(x,y) CPP2_CONCAT(x,y)
1380+
#define CPP2_SCOPE_TIMER(name) auto CPP2_UNIQUE_NAME(timer,__LINE__) = scope_timer(name)
1381+
#else
1382+
#define CPP2_SCOPE_TIMER(name)
1383+
#endif
13721384

13731385
}
13741386

Diff for: source/sema.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ class sema
481481
}
482482
//--------- END TEMPORARY REGRESSION TEST CODE FOR G_D_O OPTIMIZATION VERIFICATION -----------
483483

484-
auto timer = scope_timer("get_declaration_of_new - including external caching");
484+
CPP2_SCOPE_TIMER("get_declaration_of_new - including external caching");
485485

486486
// Check the cache first to avoid duplicate computations
487487
//
@@ -554,7 +554,7 @@ class sema
554554
) const
555555
-> declaration_sym const*
556556
{
557-
auto timer = scope_timer("get_declaration_of - old");
557+
CPP2_SCOPE_TIMER("get_declaration_of - old");
558558

559559
// First find the position the query is coming from
560560
// and remember its depth
@@ -750,15 +750,15 @@ class sema
750750
) const
751751
-> declaration_sym const*
752752
{
753-
auto timer = scope_timer("get_declaration_of_new");
753+
CPP2_SCOPE_TIMER("get_declaration_of_new");
754754

755755
// First find the position the query is coming from
756756
// and remember its depth
757757
auto i = symbols.cbegin();
758758
auto depth = 0;
759759

760760
{
761-
auto timer1 = scope_timer("get_declaration_of_new - phase 1 initial loop");
761+
CPP2_SCOPE_TIMER("get_declaration_of_new - phase 1 initial loop");
762762

763763
// If the declaration_starts list got this far yet, use that to
764764
// skip repeating the whole linear search from the table beginning
@@ -887,20 +887,20 @@ class sema
887887
}
888888
}
889889

890-
auto timer2 = scope_timer("get_declaration_of_new - phase 2 backward scan loop");
890+
CPP2_SCOPE_TIMER("get_declaration_of_new - phase 2 backward scan loop");
891891

892892
// Then look backward to find the first declaration of
893893
// this name that is not deeper (in a nested scope)
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-
auto timer2a = 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-
auto timer2aa = 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);
@@ -942,7 +942,7 @@ class sema
942942
return &decl;
943943
}
944944

945-
auto timer2b = scope_timer("get_declaration_of_new - phase 2b 'move this' part of loop");
945+
CPP2_SCOPE_TIMER("get_declaration_of_new - phase 2b 'move this' part of loop");
946946

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

0 commit comments

Comments
 (0)