-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.cpp
More file actions
248 lines (215 loc) · 8.72 KB
/
Copy pathScope.cpp
File metadata and controls
248 lines (215 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "BenchmarkSupport.hpp"
#include <arithmetics/core/CoreModule.hpp>
#include <chrono>
#include <stdexcept>
#include <string>
#include <string_view>
#include <pegium/core/syntax-tree/AbstractReference.hpp>
namespace pegium::bench {
namespace {
using Clock = std::chrono::steady_clock;
volatile std::size_t g_scope_bench_sink = 0;
struct ScopeBenchSource {
std::string text;
std::string localReferenceName;
std::string globalReferenceName;
};
ScopeBenchSource make_scope_source(std::size_t targetBytes) {
std::string source;
source.reserve(targetBytes + 4096);
source += "Module scopeBench\n\n";
std::size_t index = 0;
while (source.size() < targetBytes / 2) {
source += "def value" + std::to_string(index) + ": " +
std::to_string(index % 97) + ";\n";
++index;
}
const auto parameterCount = std::max<std::size_t>(64, index / 2);
const auto localName = "arg" + std::to_string(parameterCount - 1);
const auto globalName = "value" + std::to_string(index - 1);
source += "\ndef scoped(";
for (std::size_t paramIndex = 0; paramIndex < parameterCount; ++paramIndex) {
if (paramIndex > 0) {
source += ", ";
}
source += "arg" + std::to_string(paramIndex);
}
source += "):\n " + localName + " + " + globalName + ";\n";
source += "\nscoped(";
for (std::size_t argIndex = 0; argIndex < parameterCount; ++argIndex) {
if (argIndex > 0) {
source += ", ";
}
source += std::to_string(argIndex);
}
source += ");\n";
return {.text = std::move(source),
.localReferenceName = std::move(localName),
.globalReferenceName = std::move(globalName)};
}
const AbstractReference &find_reference(const workspace::Document &document,
std::string_view refText) {
for (const auto &handle : document.parseResult.references) {
const auto &reference = *handle.getConst();
if (reference.getRefText() == refText) {
return reference;
}
}
throw std::runtime_error("Could not find scope benchmark reference '" +
std::string(refText) + "'.");
}
std::shared_ptr<BenchmarkFixture> build_scope_fixture(std::string source) {
ExampleBenchSpec spec{
.name = "scope",
.languageId = "arithmetics",
.extension = ".calc",
.registerLanguages = arithmetics::registerArithmeticsCoreServices,
.makeSource = [](std::size_t) { return std::string{}; },
};
auto fixture = create_fixture(spec, std::move(source));
pegium::installDefaultSharedCoreServices(*fixture->sharedServices);
pegium::installDefaultSharedLspServices(*fixture->sharedServices);
if (!spec.registerLanguages(*fixture->sharedServices)) {
throw std::runtime_error("Failed to register language services for " +
spec.name + ".");
}
fixture->services =
&fixture->sharedServices->serviceRegistry->getServices(fixture->uri);
create_changed_document(*fixture);
workspace::BuildOptions options;
options.validation = false;
const std::array documents{fixture->document};
fixture->sharedServices->workspace.documentBuilder->build(documents, options);
if (fixture->document == nullptr ||
fixture->document->state < workspace::DocumentState::Linked) {
throw std::runtime_error("Scope benchmark document was not built.");
}
return fixture;
}
BenchmarkTimings measure_scope_operation(
const std::shared_ptr<BenchmarkFixture> &fixture,
const std::function<std::size_t()> &operation) {
const auto start = Clock::now();
g_scope_bench_sink += operation();
const auto end = Clock::now();
BenchmarkTimings timings{};
timings[static_cast<std::size_t>(BenchmarkStep::FullBuild)] =
std::chrono::duration<double, std::milli>(end - start).count();
return timings;
}
} // namespace
void register_scope_benchmarks(BenchmarkRegistry ®istry) {
const auto source = make_scope_source(benchmark_target_bytes());
const auto bytes = source.text.size();
registry.add("scope-local", bytes, [source] {
auto fixture = build_scope_fixture(source.text);
const auto *scopeProvider = fixture->services->references.scopeProvider.get();
if (fixture->document == nullptr) {
throw std::runtime_error("Scope benchmark services are incomplete.");
}
const auto &localReference =
find_reference(*fixture->document, source.localReferenceName);
const auto localInfo = makeReferenceInfo(localReference);
const auto iterations = get_env_int("PEGIUM_SCOPE_LOOKUP_ITERATIONS", 5000, 1);
return measure_scope_operation(fixture, [&] {
std::size_t total = 0;
for (int iteration = 0; iteration < iterations; ++iteration) {
const auto *entry = scopeProvider->getScopeEntry(localInfo);
if (entry != nullptr) {
total += entry->name.size();
}
}
return total;
});
}, /*fullBuildOnly=*/true);
registry.add("scope-global", bytes, [source] {
auto fixture = build_scope_fixture(source.text);
const auto *scopeProvider = fixture->services->references.scopeProvider.get();
if (fixture->document == nullptr) {
throw std::runtime_error("Scope benchmark services are incomplete.");
}
const auto &globalReference =
find_reference(*fixture->document, source.globalReferenceName);
const auto globalInfo = makeReferenceInfo(globalReference);
const auto iterations = get_env_int("PEGIUM_SCOPE_LOOKUP_ITERATIONS", 5000, 1);
return measure_scope_operation(fixture, [&] {
std::size_t total = 0;
for (int iteration = 0; iteration < iterations; ++iteration) {
const auto *entry = scopeProvider->getScopeEntry(globalInfo);
if (entry != nullptr) {
total += entry->name.size();
}
}
return total;
});
}, /*fullBuildOnly=*/true);
registry.add("scope-all-elements", bytes, [source] {
auto fixture = build_scope_fixture(source.text);
const auto *scopeProvider = fixture->services->references.scopeProvider.get();
if (fixture->document == nullptr) {
throw std::runtime_error("Scope benchmark services are incomplete.");
}
const auto &globalReference =
find_reference(*fixture->document, source.globalReferenceName);
auto allInfo = makeReferenceInfo(globalReference);
allInfo.referenceText = {};
const auto iterations = get_env_int("PEGIUM_SCOPE_ENUM_ITERATIONS", 200, 1);
return measure_scope_operation(fixture, [&] {
std::size_t total = 0;
for (int iteration = 0; iteration < iterations; ++iteration) {
const auto collectEntry =
[&total](const workspace::AstNodeDescription &entry) {
total += entry.name.size();
return true;
};
const auto completed = scopeProvider->visitScopeEntries(
allInfo,
utils::function_ref<bool(const workspace::AstNodeDescription &)>(
collectEntry));
(void)completed;
}
return total;
});
}, /*fullBuildOnly=*/true);
// M4 guardrail: resolving a name against the global index (the inline doc
// {@link} path). `index-find-by-name` is the new single-copy scan;
// `index-all-elements-scan` is the old path that deep-copied the entire index
// per lookup. Both resolve the same (last) global name on the large index, so
// their throughput is directly comparable — find-by-name should win by the
// avoided per-element copies, and must never regress below the scan path.
registry.add("index-find-by-name", bytes, [source] {
auto fixture = build_scope_fixture(source.text);
auto *indexManager = fixture->sharedServices->workspace.indexManager.get();
const auto iterations =
get_env_int("PEGIUM_INDEX_LOOKUP_ITERATIONS", 2000, 1);
return measure_scope_operation(fixture, [&] {
std::size_t total = 0;
for (int iteration = 0; iteration < iterations; ++iteration) {
const auto entry = indexManager->findByName(source.globalReferenceName);
if (entry.has_value()) {
total += entry->name.size();
}
}
return total;
});
}, /*fullBuildOnly=*/true);
registry.add("index-all-elements-scan", bytes, [source] {
auto fixture = build_scope_fixture(source.text);
auto *indexManager = fixture->sharedServices->workspace.indexManager.get();
const auto iterations =
get_env_int("PEGIUM_INDEX_LOOKUP_ITERATIONS", 2000, 1);
return measure_scope_operation(fixture, [&] {
std::size_t total = 0;
for (int iteration = 0; iteration < iterations; ++iteration) {
for (const auto &entry : indexManager->allElements()) {
if (entry.name == source.globalReferenceName) {
total += entry.name.size();
break;
}
}
}
return total;
});
}, /*fullBuildOnly=*/true);
}
} // namespace pegium::bench