Tweak FuncInfo memory management

Idea being for everything to be automatically released when process
exits just so there's less potential noise from leak profilers.
This commit is contained in:
Jon Siwek 2020-12-13 17:06:57 -08:00
parent c7bec09e14
commit 7b2ee2514e
4 changed files with 30 additions and 37 deletions

View file

@ -24,13 +24,13 @@ void Inliner::Analyze()
std::unordered_set<const Func*> cs;
// Aspirational ....
non_recursive_funcs.insert(f->Func());
non_recursive_funcs.insert(f.Func());
for ( auto& func : f->Profile()->ScriptCalls() )
for ( auto& func : f.Profile()->ScriptCalls() )
{
cs.insert(func);
if ( func == f->Func() )
if ( func == f.Func() )
{
if ( report_recursive )
printf("%s is directly recursive\n",
@ -40,7 +40,7 @@ void Inliner::Analyze()
}
}
call_set[f->Func()] = cs;
call_set[f.Func()] = cs;
}
// Transitive closure. If we had any self-respect, we'd implement
@ -118,11 +118,11 @@ void Inliner::Analyze()
// sizes for them correctly, and more fundamentally since
// we don't compile them and hence inlining them will
// make the parent non-compilable.
if ( f->Func()->Flavor() == FUNC_FLAVOR_FUNCTION &&
non_recursive_funcs.count(f->Func()) > 0 &&
f->Profile()->NumLambdas() == 0 &&
f->Profile()->NumWhenStmts() == 0 )
inline_ables.insert(f->Func());
if ( f.Func()->Flavor() == FUNC_FLAVOR_FUNCTION &&
non_recursive_funcs.count(f.Func()) > 0 &&
f.Profile()->NumLambdas() == 0 &&
f.Profile()->NumWhenStmts() == 0 )
inline_ables.insert(f.Func());
for ( auto& f : funcs )
{
@ -134,8 +134,8 @@ void Inliner::Analyze()
// circumstances in which a Zeek function can be called
// not ultimately stemming from an event (such as global
// scripting, or expiration functions).
if ( inline_ables.count(f->Func()) == 0 )
InlineFunction(f);
if ( inline_ables.count(f.Func()) == 0 )
InlineFunction(&f);
}
}

View file

@ -19,7 +19,7 @@ public:
// First argument is a collection of information about *all* of
// the script functions. Second argument states whether to report
// recursive functions (of interest as they're not in-lineable).
Inliner(std::vector<FuncInfo*>& _funcs, bool _report_recursive)
Inliner(std::vector<FuncInfo>& _funcs, bool _report_recursive)
: funcs(_funcs), report_recursive(_report_recursive)
{ Analyze(); }
@ -40,7 +40,7 @@ protected:
// Information about all of the functions (and events/hooks) in
// the full set of scripts.
std::vector<FuncInfo*>& funcs;
std::vector<FuncInfo>& funcs;
// Functions that we've determined to be suitable for inlining.
std::unordered_set<Func*> inline_ables;

View file

@ -12,19 +12,19 @@ namespace zeek::detail {
std::unordered_set<const Func*> non_recursive_funcs;
// Tracks all of the loaded functions (including event handlers and hooks).
static std::vector<FuncInfo*> funcs;
static std::vector<FuncInfo> funcs;
FuncInfo::~FuncInfo()
{
delete pf;
delete save_file;
}
FuncInfo::FuncInfo(ScriptFuncPtr _func, ScopePtr _scope, StmtPtr _body)
: func(std::move(_func)), scope(std::move(_scope)), body(std::move(_body))
{}
void FuncInfo::SetProfile(std::unique_ptr<ProfileFunc> _pf)
{ pf = std::move(_pf); }
void analyze_func(ScriptFuncPtr f)
{
auto info = new FuncInfo(f, {NewRef{}, f->GetScope()}, f->CurrentBody());
funcs.push_back(info);
funcs.emplace_back(f, ScopePtr{NewRef{}, f->GetScope()}, f->CurrentBody());
}
static void check_env_opt(const char* opt, bool& opt_flag)
@ -50,8 +50,8 @@ void analyze_scripts(Options& opts)
for ( auto& f : funcs )
{
f->SetProfile(new ProfileFunc(true));
f->Body()->Traverse(f->Profile());
f.SetProfile(std::make_unique<ProfileFunc>(true));
f.Body()->Traverse(f.Profile());
}
Inliner* inl = nullptr;

View file

@ -32,34 +32,27 @@ using ScriptFuncPtr = IntrusivePtr<ScriptFunc>;
// Info we need for tracking an instance of a function.
class FuncInfo {
public:
FuncInfo(ScriptFuncPtr _func, ScopePtr _scope, StmtPtr _body)
{
func = std::move(_func);
scope = std::move(_scope);
body = std::move(_body);
}
~FuncInfo();
FuncInfo(ScriptFuncPtr _func, ScopePtr _scope, StmtPtr _body);
ScriptFunc* Func() { return func.get(); }
ScriptFuncPtr FuncPtr() { return func; }
ScopePtr Scope() { return scope; }
StmtPtr Body() { return body; }
ProfileFunc* Profile() { return pf; }
const char* SaveFile() { return save_file; }
ProfileFunc* Profile() { return pf.get(); }
const std::string& SaveFile() { return save_file; }
void SetProfile(ProfileFunc* _pf) { pf = _pf; }
void SetSaveFile(const char* _sf);
void SetProfile(std::unique_ptr<ProfileFunc> _pf);
void SetSaveFile(std::string _sf) { save_file = std::move(_sf); }
protected:
ScriptFuncPtr func;
ScopePtr scope;
StmtPtr body;
ProfileFunc* pf = nullptr;
std::unique_ptr<ProfileFunc> pf;
// If we're saving this function in a file, this is the name
// of the file to use.
char* save_file = nullptr;
std::string save_file;
};