streamlining of some script optimization APIs

This commit is contained in:
Vern Paxson 2024-01-10 18:36:25 -08:00 committed by Arne Welzel
parent f7b739a47d
commit 896238c173
3 changed files with 13 additions and 12 deletions

View file

@ -550,22 +550,23 @@ static std::unordered_map<std::string, unsigned int> func_attrs = {
// Some of these have side effects that could be checked for in a specific
// context, but the gains from doing so likely aren't worth the complexity.
bool is_special_script_func(std::string func_name) {
bool is_special_script_func(const std::string& func_name) {
auto f_attr = func_attrs.find(func_name);
return f_attr != func_attrs.end() && (f_attr->second & ATTR_SPECIAL_SCRIPT_FUNC) != 0;
}
bool is_idempotent(std::string func_name) {
bool is_idempotent(const std::string& func_name) {
auto f_attr = func_attrs.find(func_name);
return f_attr != func_attrs.end() && (f_attr->second & ATTR_IDEMPOTENT) != 0;
}
bool has_no_script_side_effects(std::string func_name) {
bool has_script_side_effects(const std::string& func_name) {
auto f_attr = func_attrs.find(func_name);
if ( f_attr == func_attrs.end() )
return false;
// We don't know about it, so be conservative.
return true;
return (f_attr->second & (ATTR_NO_SCRIPT_SIDE_EFFECTS | ATTR_NO_ZEEK_SIDE_EFFECTS | ATTR_IDEMPOTENT)) != 0;
return (f_attr->second & (ATTR_NO_SCRIPT_SIDE_EFFECTS | ATTR_NO_ZEEK_SIDE_EFFECTS | ATTR_IDEMPOTENT)) == 0;
}
} // namespace zeek::detail

View file

@ -10,15 +10,15 @@ namespace zeek::detail {
// A "special script function" is one that the event engine explicitly
// knows about.
extern bool is_special_script_func(std::string func_name);
extern bool is_special_script_func(const std::string& func_name);
// An idempotent function returns the same value when called with the
// same arguments (and has no meaningful side effects in terms of script-level
// or Zeek-internal state).
extern bool is_idempotent(std::string func_name);
extern bool is_idempotent(const std::string& func_name);
// Whether the given function (currently, just BiFs) has no Zeek-script-level
// Whether the given function (currently, just BiFs) has Zeek-script-level
// side effects.
extern bool has_no_script_side_effects(std::string func_name);
extern bool has_script_side_effects(const std::string& func_name);
} // namespace zeek::detail

View file

@ -635,7 +635,7 @@ bool ProfileFuncs::GetCallSideEffects(const NameExpr* n, IDSet& non_local_ids, T
auto func = fv->AsFunc();
if ( func->GetKind() == Func::BUILTIN_FUNC ) {
if ( ! has_no_script_side_effects(func->Name()) )
if ( has_script_side_effects(func->Name()) )
is_unknown = true;
return true;
}
@ -1168,7 +1168,7 @@ bool ProfileFuncs::DefinitelyHasNoSideEffects(const ExprPtr& e) const {
return false;
for ( auto& b : pf->BiFGlobals() )
if ( ! has_no_script_side_effects(b->Name()) )
if ( has_script_side_effects(b->Name()) )
return false;
return true;
@ -1259,7 +1259,7 @@ bool ProfileFuncs::AssessSideEffects(const ProfileFunc* pf, IDSet& non_local_ids
}
for ( auto& b : pf->BiFGlobals() )
if ( ! has_no_script_side_effects(b->Name()) ) {
if ( has_script_side_effects(b->Name()) ) {
is_unknown = true;
return true;
}