mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-23 19:42:45 +00:00
Add more complex function definition generation and an impl-file benchmark (#7998)
Previously, we only had the dense API declarations benchmark. This adds a benchmark that aims to emulate what a realistic implementation file in Carbon would look like, based on what `.cpp` files look like in Carbon, Clang, and LLVM. It's still a little artificial in order to provide stability, but it should give us coverage of compile time performance of statements and function definitions at more realistic scales. In order to generate more interesting and longer function definitions, this introduces a plan-based approach to guide how each feature of a definition is produced. Initially, it identifies these as roughly: - local variables - updates to variables - `if`, `else`, `while` blocks - "guard" statements -- conditions with a return, break, or continue - read of a parameter The plan also manages the nesting depth of these constructs. Because the plan establishes how many of these constructs exist in a function definition, and at what nesting depth, the emission can randomize the order and arrangement without changing the size of the function. This follows the pattern in all our benchmark source generation of getting byte-identical stability while avoiding predictable structures. Technically, I think the current approach goes a bit too far because function definitions I suspect are somewhat predictable in their structure in practice. But a conservative benchmark I think is a good starting point. =] The target distributions for definition sizes, maximum depths, and statement mixes come from a very rough Claude-powered analysis of 10,697 function definitions in 533 Carbon toolchain, LLVM, and Clang implementation files. These were analyzed and classified by Claude using a one-off Python script and libclang. I haven't reviewed any of the analysis carefully, the resulting distributions look plausible enough as a starting point. Comparing real code against generated C++ from the plans, each number per-definition average: | statements | definitions | max depth | ifs | loops | |------------|-------------|-------------|-------------|-------------| | 7-9 | 10.9 / 11% | 1.25 / 1.23 | 1.74 / 1.86 | 0.23 / 0.15 | | 14-19 | 7.3 / 7% | 1.89 / 1.88 | 3.53 / 3.71 | 0.61 / 0.42 | | 28-39 | 3.3 / 3% | 2.76 / 2.75 | 7.75 / 8.46 | 1.26 / 1.27 | | 60+ | 1.5 / 2% | 4.03 / 3.80 | 25.0 / 27.1 | 3.72 / 4.04 | To maintain stability, every definition has an accumulator, a statement reading its parameters, and a `return`, so real definitions of one to three statements become three. Note that updates are currently also used instead of calls to other functions -- a later PR will switch those to calls so that we form more plausible call graphs. In general, subsequent PRs will refine and expand the plan to generate increasingly more realistic function definitions. There are probably lots of improvements that could be made here, both to generate higher quality definitions and to improve the generation logic to be more efficient, etc. But already this has allowed _much_ more extensive benchmarking of Carbon's compile-time performance so it seems likely a reasonable increment. Assisted-by: Claude Code and Antigravity with Gemini --------- Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
This commit is contained in:
1 parent
503f09037d
commit
9319bb0369
5 files changed
+1199
-120
No files matched your search
@@ -569,30 +569,78 @@ TEST(SourceGenTest, GenApiFileDenseDeclsInlineBodiesCppCompiles) {
|
||||
}
|
||||
}
|
||||
|
||||
// Every declared function and method has an out-of-line definition, using the
|
||||
// default size distribution (from 0-statement minimal definitions through
|
||||
// deeply nested definitions).
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, OutOfLineDefs) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.inline_function_defs = 2, .max_body_locals = 3},
|
||||
.define_decls_out_of_line = true};
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(3000, params, 16));
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(3000, params)));
|
||||
}
|
||||
|
||||
// Large out-of-line definitions, which nest deeply. A definition's block
|
||||
// arrangement and which locals its statements read depend on the seed, so
|
||||
// compile several seeds.
|
||||
TEST_P(GenApiFileDenseDeclsParamsTest, LargeOutOfLineDefs) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.public_function_decls = 1,
|
||||
.public_method_decls = 2,
|
||||
.private_function_decls = 0,
|
||||
.private_method_decls = 1,
|
||||
.private_field_decls = 2},
|
||||
.define_decls_out_of_line = true,
|
||||
.out_of_line_def_sizes = {{.statements = 40, .weight = 1},
|
||||
{.statements = 150, .weight = 1}}};
|
||||
EXPECT_THAT(GetParam(), GeneratesSeedIndependentSize(3000, params, 24));
|
||||
for (int _ : llvm::seq(4)) {
|
||||
EXPECT_TRUE(TestCompile(
|
||||
GetParam(), SourceGen(GetParam()).GenApiFileDenseDecls(3000, params)));
|
||||
}
|
||||
}
|
||||
|
||||
// The line estimates have to track the emitted lines closely, or files miss
|
||||
// their target size. The target is large so that rounding to a whole number of
|
||||
// classes is small next to the tolerance. C++ gets a larger tolerance for its
|
||||
// access specifier lines, which the estimates don't count.
|
||||
TEST(SourceGenTest, GenApiFileDenseDeclsLineTargetAccuracy) {
|
||||
SourceGen::DenseDeclParams params = {
|
||||
.class_params = {.inline_function_defs = 1,
|
||||
.max_body_locals = 3,
|
||||
.inline_getters = 1,
|
||||
.inline_predicates = 1,
|
||||
.inline_forwarders = 1}};
|
||||
llvm::SmallVector<SourceGen::DenseDeclParams, 0> param_set;
|
||||
param_set.push_back({.class_params = {.inline_function_defs = 1,
|
||||
.max_body_locals = 3,
|
||||
.inline_getters = 1,
|
||||
.inline_predicates = 1,
|
||||
.inline_forwarders = 1}});
|
||||
// The implementation file shape.
|
||||
param_set.push_back(
|
||||
{.class_params = {.public_function_decls = 2,
|
||||
.public_function_decl_params = {.max_params = 2},
|
||||
.public_method_decls = 6,
|
||||
.public_method_decl_params = {.max_params = 2},
|
||||
.private_function_decls = 2,
|
||||
.private_function_decl_params = {.max_params = 2},
|
||||
.private_method_decls = 2,
|
||||
.private_method_decl_params = {.max_params = 2},
|
||||
.private_field_decls = 4},
|
||||
.define_decls_out_of_line = true});
|
||||
|
||||
constexpr int TargetLines = 20000;
|
||||
for (SourceGen::Language language :
|
||||
{SourceGen::Language::Carbon, SourceGen::Language::Cpp}) {
|
||||
SourceGen gen(language);
|
||||
std::string source = gen.GenApiFileDenseDecls(TargetLines, params);
|
||||
ssize_t lines = CountLines(source);
|
||||
if (language == SourceGen::Language::Carbon) {
|
||||
// Within 2% of the requested line count.
|
||||
EXPECT_THAT(lines, AllOf(Ge(19600), Le(20400)));
|
||||
} else {
|
||||
// Within 10% of the requested line count.
|
||||
EXPECT_THAT(lines, AllOf(Ge(18000), Le(22000)));
|
||||
for (const SourceGen::DenseDeclParams& params : param_set) {
|
||||
for (SourceGen::Language language :
|
||||
{SourceGen::Language::Carbon, SourceGen::Language::Cpp}) {
|
||||
SourceGen gen(language);
|
||||
std::string source = gen.GenApiFileDenseDecls(TargetLines, params);
|
||||
ssize_t lines = CountLines(source);
|
||||
if (language == SourceGen::Language::Carbon) {
|
||||
// Within 2% of the requested line count.
|
||||
EXPECT_THAT(lines, AllOf(Ge(19600), Le(20400)))
|
||||
<< "define_decls_out_of_line=" << params.define_decls_out_of_line;
|
||||
} else {
|
||||
// Within 10% of the requested line count.
|
||||
EXPECT_THAT(lines, AllOf(Ge(18000), Le(22000)))
|
||||
<< "define_decls_out_of_line=" << params.define_decls_out_of_line;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user