Skip to content

Commit 1bc5ebd

Browse files
Avoid using dynamically allocated vector for punctuators_and_operators (#284)
I'm currently working on a memory profiler, and I noticed that 880 bytes was always being allocated at the start of any program that I tested. This allocation was due to `punctuators_and_operators`, inside cpptrace. <img width="1548" height="404" alt="image" src="https://github.com/user-attachments/assets/5d218d65-0530-4d29-ad1f-24f5ce7a2090" /> I've created this MR to convert `punctuators_and_operators` into a statically allocated array, so that cpptrace doesn't do any allocations on initialization. --------- Co-authored-by: Jeremy Rifkin <[email protected]>
1 parent 787d8af commit 1bc5ebd

File tree

1 file changed

+61
-16
lines changed

1 file changed

+61
-16
lines changed

src/prune_symbol.cpp

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,67 @@ namespace detail {
6060
}
6161

6262
// http://eel.is/c++draft/lex.operators#nt:operator-or-punctuator
63-
const std::vector<string_view> punctuators_and_operators = []() {
64-
std::vector<string_view> vec{
65-
"{", "}", "[", "]", "(", ")",
66-
"<:", ":>", "<%", "%>", ";", ":", "...",
67-
"?", "::", ".", ".*", "->", "->*", "~",
68-
"!", "+", "-", "*", "/", "%", "^", "&", "|",
69-
"=", "+=", "-=", "*=", "/=", "%=", "^=", "&=", "|=",
70-
"==", "!=", "<", ">", "<=", ">=", "<=>", "&&", "||",
71-
"<<", ">>", "<<=", ">>=", "++", "--", ",",
72-
// "and", "or", "xor", "not", "bitand", "bitor", "compl",
73-
// "and_eq", "or_eq", "xor_eq", "not_eq",
74-
"#", // extension for {lambda()#1}
75-
};
76-
std::sort(vec.begin(), vec.end(), [](string_view a, string_view b) { return a.size() > b.size(); });
77-
return vec;
78-
} ();
63+
// These must be ordered by length, longest first.
64+
const string_view punctuators_and_operators[] {
65+
// 3 character operators
66+
"->*",
67+
"...",
68+
"<=>",
69+
"<<=",
70+
">>=",
71+
// 2 character operators
72+
"<:",
73+
":>",
74+
"<%",
75+
"%>",
76+
"::",
77+
".*",
78+
"->",
79+
"+=",
80+
"-=",
81+
"*=",
82+
"/=",
83+
"%=",
84+
"^=",
85+
"&=",
86+
"|=",
87+
"==",
88+
"!=",
89+
"<=",
90+
">=",
91+
"&&",
92+
"||",
93+
"<<",
94+
">>",
95+
"++",
96+
"--",
97+
// 1 character operators
98+
"{",
99+
"}",
100+
"[",
101+
"]",
102+
"(",
103+
")",
104+
";",
105+
":",
106+
"?",
107+
".",
108+
"~",
109+
"!",
110+
"+",
111+
"-",
112+
"*",
113+
"/",
114+
"%",
115+
"^",
116+
"&",
117+
"|",
118+
"=",
119+
"<",
120+
">",
121+
",",
122+
"#", // extension for {lambda()#1}
123+
};
79124

80125
const std::array<string_view, 2> anonymous_namespace_spellings = {"(anonymous namespace)", "`anonymous namespace'"};
81126

0 commit comments

Comments
 (0)