mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:50:14 +01:00
This is intended to address currently flaky timeouts that are likely caused by the size of the prelude. I'm addressing a performance bottleneck in AnalyzeProgram with trace output. Trying to omit prelude traces reduces most trace output significantly, and I think it'll scale better as the prelude size increases. The basic mechanics here are: - In order to consistently track whether tracing is on, I've added a TraceStream class, explorer/interpreter/trace_stream.h. - The AST now has a num_prelude_declarations field, so that it's provided where the boundary is. - In order to mark where we try to skip prelude output, I've added calls to set_in_prelude in type_checker. - In exec_program, I just use num_prelude_declarations directly to skip over. - Everywhere checks TraceStream::is_enabled before printing, similar to the std::optional check that was previously used. This does add some timing output in order to better diagnose where slowness is coming from, when tracing. It also adds "verbose" targets to make it easier to get the trace output. So for example, here's a timing for zero.carbon: ``` Timings: - Parse: 13ms - AddPrelude: 25ms - AnalyzeProgram: 116ms - ExecProgram: 12ms ``` If I make a small change to just not set skipping_prelude (essentially getting back to current output): ``` - Parse: 13ms - AddPrelude: 25ms - AnalyzeProgram: 2359ms - ExecProgram: 57ms ``` Thus in this trivial example, I'm eliminating about 95% of the execution time. Note this approach could still be refined in a few ways: - We could add a flag to allow overriding in_prelude. It should be a small amount of work after this change. But it's a little consistent with how parser_debug works, that it won't print prelude output by default (unless there's an error). - Execution could skip messages involving initialization of globals declared in the prelude. This is a little noisy right now, but I don't think it's significant for performance because ExecProgram is tiny. - Once files are more separated, we should be able to change the num_prelude_declarations/set_in_prelude approach. Co-authored-by: Richard Smith <richard@metafoo.co.uk>
29 lines
1.1 KiB
C++
29 lines
1.1 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "explorer/syntax/prelude.h"
|
|
|
|
#include "explorer/syntax/parse.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Adds the Carbon prelude to `declarations`.
|
|
void AddPrelude(std::string_view prelude_file_name, Nonnull<Arena*> arena,
|
|
std::vector<Nonnull<Declaration*>>* declarations,
|
|
int* num_prelude_declarations) {
|
|
ErrorOr<AST> parse_result = Parse(arena, prelude_file_name, false);
|
|
if (!parse_result.ok()) {
|
|
// Try again with tracing, to help diagnose the problem.
|
|
ErrorOr<AST> trace_parse_result = Parse(arena, prelude_file_name, true);
|
|
CARBON_FATAL() << "Failed to parse prelude:\n"
|
|
<< trace_parse_result.error();
|
|
}
|
|
const auto& prelude = *parse_result;
|
|
declarations->insert(declarations->begin(), prelude.declarations.begin(),
|
|
prelude.declarations.end());
|
|
*num_prelude_declarations = prelude.declarations.size();
|
|
}
|
|
|
|
} // namespace Carbon
|