C++ script generation fix for lambdas that have identical bodies

This commit is contained in:
Vern Paxson 2022-09-16 16:46:35 -07:00
parent 7210225bba
commit dbae112bdc

View file

@ -155,20 +155,24 @@ void CPPCompile::Compile(bool report_uncompilable)
if ( ! func.ShouldSkip() ) if ( ! func.ShouldSkip() )
DeclareFunc(func); DeclareFunc(func);
// We track lambdas by their internal names, because two different // We track lambdas by their internal names, and associate those
// LambdaExpr's can wind up referring to the same underlying lambda // with their AST bodies. Two different LambdaExpr's can wind up
// if the bodies happen to be identical. In that case, we don't // referring to the same underlying lambda if the bodies happen to
// want to generate the lambda twice. // be identical. In that case, we don't want to generate the lambda
unordered_set<string> lambda_names; // twice, but we do want to map the second one to the same body name.
unordered_map<string, const Stmt*> lambda_ASTs;
for ( const auto& l : pfs.Lambdas() ) for ( const auto& l : pfs.Lambdas() )
{ {
const auto& n = l->Name(); const auto& n = l->Name();
if ( lambda_names.count(n) > 0 ) const auto body = l->Ingredients().body.get();
// Skip it. if ( lambda_ASTs.count(n) > 0 )
continue; // Reuse previous body.
body_names[body] = body_names[lambda_ASTs[n]];
DeclareLambda(l, pfs.ExprProf(l).get()); else
lambda_names.insert(n); {
DeclareLambda(l, pfs.ExprProf(l).get());
lambda_ASTs[n] = body;
}
} }
NL(); NL();
@ -178,15 +182,15 @@ void CPPCompile::Compile(bool report_uncompilable)
if ( ! func.ShouldSkip() ) if ( ! func.ShouldSkip() )
CompileFunc(func); CompileFunc(func);
lambda_names.clear(); lambda_ASTs.clear();
for ( const auto& l : pfs.Lambdas() ) for ( const auto& l : pfs.Lambdas() )
{ {
const auto& n = l->Name(); const auto& n = l->Name();
if ( lambda_names.count(n) > 0 ) if ( lambda_ASTs.count(n) > 0 )
continue; continue;
CompileLambda(l, pfs.ExprProf(l).get()); CompileLambda(l, pfs.ExprProf(l).get());
lambda_names.insert(n); lambda_ASTs[n] = l->Ingredients().body.get();
} }
NL(); NL();