Skip to content

Commit 8511298

Browse files
committed
Change all deque to stable_vector, add some g_d_o timers for #904 investigation
Timers now run only in `-_debug` since they slow things down `-_debug` now implies `-verbose` `-verbose` now sorts the timer output in descending cost order
1 parent 6dbd1ba commit 8511298

File tree

8 files changed

+59
-38
lines changed

8 files changed

+59
-38
lines changed

Diff for: source/common.h

+24-10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <cstdint>
3939
#include <iomanip>
4040
#include <iterator>
41+
#include <memory>
4142
#include <map>
4243
#include <string>
4344
#include <string_view>
@@ -887,12 +888,20 @@ static cmdline_processor::register_flag cmd_gen_version(
887888
[]{ cmdline.gen_version(); }
888889
);
889890

891+
static auto flag_verbose = false;
892+
static cmdline_processor::register_flag cmd_verbose(
893+
9,
894+
"verbose",
895+
"Print verbose output and statistics",
896+
[]{ flag_verbose = true; }
897+
);
898+
890899
static auto flag_internal_debug = false;
891900
static cmdline_processor::register_flag cmd_internal_debug(
892901
0,
893902
"_debug",
894-
"Generate internal debug instrumentation",
895-
[]{ flag_internal_debug = true; }
903+
"Generate internal debug data, implies -verbose",
904+
[]{ flag_internal_debug = true; flag_verbose = true; }
896905
);
897906

898907
static auto flag_print_colon_errors = false;
@@ -981,14 +990,14 @@ class stable_vector
981990

982991
std::vector< std::vector<T> > data;
983992

984-
auto add_segment() -> void {
985-
data.push_back( {} );
993+
auto add_segment( std::initializer_list<T> init = {} ) -> void {
994+
data.push_back( init );
986995
data.back().reserve(PageSize);
987996
}
988997

989998
public:
990-
stable_vector() {
991-
add_segment();
999+
stable_vector( std::initializer_list<T> init = {}) {
1000+
add_segment( init);
9921001
}
9931002

9941003
auto empty() const -> bool {
@@ -1348,11 +1357,16 @@ class timer
13481357
}
13491358
};
13501359

1351-
static std::map<std::string, timer> timers; // global named timers
1360+
static std::unordered_map<std::string_view, timer> timers; // global named timers
13521361

1353-
auto scope_timer(std::string const& name) {
1354-
timers[name].start();
1355-
return finally( [=]{ timers[name].stop(); } );
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 {};
13561370
}
13571371

13581372

Diff for: source/cppfront.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,16 @@ auto main(
115115
auto total_time = print_with_thousands(t.elapsed().count());
116116
std::cout << "\n Time " << total_time << " ms";
117117

118+
std::multimap< long long, std::string_view, std::greater<long long> > sorted_timers;
118119
for (auto [name, t] : timers) {
120+
sorted_timers.insert({t.elapsed().count(), name});
121+
}
122+
123+
for (auto [elapsed, name] : sorted_timers) {
119124
std::cout
120125
<< "\n "
121126
<< std::right << std::setw(total_time.size())
122-
<< print_with_thousands(t.elapsed().count()) << " ms" << " in " << name;
127+
<< print_with_thousands(elapsed) << " ms" << " in " << name;
123128
}
124129
}
125130

Diff for: source/lex.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "io.h"
2222
#include <map>
2323
#include <climits>
24-
#include <deque>
2524
#include <cstring>
2625

2726

@@ -614,11 +613,11 @@ auto expand_raw_string_literal(
614613
// A stable place to store additional text for source tokens that are merged
615614
// into a whitespace-containing token (to merge the Cpp1 multi-token keywords)
616615
// -- this isn't about tokens generated later, that's tokens::generated_tokens
617-
static auto generated_text = std::deque<std::string>{}; // TODO: static
618-
static auto generated_lines = std::deque<std::vector<source_line>>{}; // TODO: static
616+
static auto generated_text = stable_vector<std::string>{}; // TODO: static
617+
static auto generated_lines = stable_vector<std::vector<source_line>>{}; // TODO: static
619618

620619

621-
static auto multiline_raw_strings = std::deque<multiline_raw_string>{}; // TODO: static
620+
static auto multiline_raw_strings = stable_vector<multiline_raw_string>{}; // TODO: static
622621

623622
auto lex_line(
624623
std::string& mutable_line,
@@ -1885,7 +1884,7 @@ class tokens
18851884
std::vector<comment> comments;
18861885

18871886
// A stable place to store additional tokens that are synthesized later
1888-
std::deque<token> generated_tokens;
1887+
stable_vector<token> generated_tokens;
18891888

18901889
public:
18911890
//-----------------------------------------------------------------------
@@ -2070,7 +2069,7 @@ class tokens
20702069

20712070
};
20722071

2073-
static auto generated_lexers = std::deque<tokens>{}; // TODO: static
2072+
static auto generated_lexers = stable_vector<tokens>{}; // TODO: static
20742073

20752074
}
20762075

Diff for: source/parse.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -5331,7 +5331,7 @@ class parser
53315331
};
53325332

53335333
std::vector<token> const* tokens = {};
5334-
std::deque<token>* generated_tokens = {};
5334+
stable_vector<token>* generated_tokens = {};
53355335
int pos = 0;
53365336
std::string parse_kind = {};
53375337

@@ -5430,8 +5430,8 @@ class parser
54305430
// sections in a TU to build the whole TU's parse tree
54315431
//
54325432
auto parse(
5433-
std::vector<token> const& tokens_,
5434-
std::deque<token>& generated_tokens_
5433+
std::vector<token> const& tokens_,
5434+
stable_vector<token>& generated_tokens_
54355435
)
54365436
-> bool
54375437
{
@@ -5469,7 +5469,7 @@ class parser
54695469
//
54705470
auto parse_one_declaration(
54715471
std::vector<token> const& tokens_,
5472-
std::deque<token>& generated_tokens_
5472+
stable_vector<token>& generated_tokens_
54735473
)
54745474
-> std::unique_ptr<statement_node>
54755475
{

Diff for: source/reflect.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class compiler_services
8686
//
8787
private: std::vector<error_entry>* errors;
8888
private: int errors_original_size;
89-
private: std::deque<token>* generated_tokens;
89+
private: stable_vector<token>* generated_tokens;
9090
private: cpp2::parser parser;
9191
private: std::string metafunction_name {};
9292
private: std::vector<std::string> metafunction_args {};
@@ -97,7 +97,7 @@ class compiler_services
9797
public: explicit compiler_services(
9898

9999
std::vector<error_entry>* errors_,
100-
std::deque<token>* generated_tokens_
100+
stable_vector<token>* generated_tokens_
101101
);
102102

103103
#line 58 "reflect.h2"
@@ -790,7 +790,7 @@ namespace meta {
790790
compiler_services::compiler_services(
791791

792792
std::vector<error_entry>* errors_,
793-
std::deque<token>* generated_tokens_
793+
stable_vector<token>* generated_tokens_
794794
)
795795
: errors{ errors_ }
796796
, errors_original_size{ cpp2::unsafe_narrow<int>(std::ssize(*cpp2::impl::assert_not_null(errors))) }

Diff for: source/reflect.h2

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ compiler_services: @polymorphic_base @copyable type =
3535
//
3636
errors : *std::vector<error_entry>;
3737
errors_original_size : int;
38-
generated_tokens : *std::deque<token>;
38+
generated_tokens : *stable_vector<token>;
3939
parser : cpp2::parser;
4040
metafunction_name : std::string = ();
4141
metafunction_args : std::vector<std::string> = ();
@@ -46,7 +46,7 @@ compiler_services: @polymorphic_base @copyable type =
4646
operator=: (
4747
out this,
4848
errors_ : *std::vector<error_entry>,
49-
generated_tokens_: *std::deque<token>
49+
generated_tokens_: *stable_vector<token>
5050
)
5151
= {
5252
errors = errors_;

Diff for: source/sema.h

+14-3
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ 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");
485+
484486
// Check the cache first to avoid duplicate computations
485487
//
486488
auto my_params = gdo_params{&t, look_beyond_current_function, include_implicit_this};
@@ -748,11 +750,15 @@ class sema
748750
) const
749751
-> declaration_sym const*
750752
{
751-
auto timer = scope_timer("get_declaration_of_new");
753+
auto timer = scope_timer("get_declaration_of_new");
752754

753755
// First find the position the query is coming from
754756
// and remember its depth
755757
auto i = symbols.cbegin();
758+
auto depth = 0;
759+
760+
{
761+
auto timer1 = scope_timer("get_declaration_of_new - phase 1 initial loop");
756762

757763
// If the declaration_starts list got this far yet, use that to
758764
// skip repeating the whole linear search from the table beginning
@@ -805,8 +811,6 @@ class sema
805811

806812
initial_find_new = i;
807813

808-
auto depth = 0;
809-
810814
// If we found it exactly, we have its depth
811815
if (
812816
i != symbols.cend()
@@ -881,17 +885,22 @@ class sema
881885

882886
depth = i->depth;
883887
}
888+
}
889+
890+
auto timer2 = scope_timer("get_declaration_of_new - phase 2 backward scan loop");
884891

885892
// Then look backward to find the first declaration of
886893
// this name that is not deeper (in a nested scope)
887894
// and is in the same function
888895
using I = stable_vector<symbol>::const_iterator;
889896
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");
890898
auto in = i;
891899
if (std::abs(n) >= std::abs(bound - i)) {
892900
i = bound;
893901
}
894902
else {
903+
auto timer2aa = scope_timer("get_declaration_of_new - phase 2aa 'std::advance' specifically");
895904
std::advance(i, n);
896905
}
897906
return n - (i - in);
@@ -933,6 +942,8 @@ class sema
933942
return &decl;
934943
}
935944

945+
auto timer2b = scope_timer("get_declaration_of_new - phase 2b 'move this' part of loop");
946+
936947
// If we reached a 'move this' parameter, look it up in the type members
937948
if (
938949
include_implicit_this

Diff for: source/to_cpp1.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,6 @@ static cmdline_processor::register_flag cmd_cpp1_filename(
164164
[](std::string const& name) { flag_cpp1_filename = name; }
165165
);
166166

167-
static auto flag_verbose = false;
168-
static cmdline_processor::register_flag cmd_verbose(
169-
9,
170-
"verbose",
171-
"Print verbose output and statistics",
172-
[]{ flag_verbose = true; }
173-
);
174-
175167
static auto flag_no_exceptions = false;
176168
static cmdline_processor::register_flag cmd_no_exceptions(
177169
4,
@@ -1087,7 +1079,7 @@ class cppfront
10871079
};
10881080
class current_functions_
10891081
{
1090-
std::deque<function_info> list = { {} };
1082+
stable_vector<function_info> list = { {} };
10911083
public:
10921084
auto push(
10931085
declaration_node const* decl,

0 commit comments

Comments
 (0)