Factor static-local nil IntrusivePtrs to global locations

Minor optimization to remove any run-time impact.
This commit is contained in:
Jon Siwek 2020-05-21 17:22:39 -07:00
parent a384bb8b81
commit ea878208ba
8 changed files with 21 additions and 23 deletions

View file

@ -28,8 +28,6 @@ EventHandler::operator bool() const
const IntrusivePtr<FuncType>& EventHandler::GetType(bool check_export)
{
static IntrusivePtr<FuncType> nil;
if ( type )
return type;
@ -37,10 +35,10 @@ const IntrusivePtr<FuncType>& EventHandler::GetType(bool check_export)
check_export);
if ( ! id )
return nil;
return FuncType::nil;
if ( id->GetType()->Tag() != TYPE_FUNC )
return nil;
return FuncType::nil;
type = id->GetType<FuncType>();
return type;

View file

@ -30,6 +30,8 @@ class Scope;
class Func : public BroObj {
public:
static inline const IntrusivePtr<Func> nil;
enum Kind { BRO_FUNC, BUILTIN_FUNC };
explicit Func(Kind arg_kind);
@ -99,10 +101,7 @@ public:
uint32_t GetUniqueFuncID() const { return unique_id; }
static const IntrusivePtr<Func>& GetFuncPtrByID(uint32_t id)
{
static IntrusivePtr<Func> nil;
return id >= unique_ids.size() ? nil : unique_ids[id];
}
{ return id >= unique_ids.size() ? Func::nil : unique_ids[id]; }
protected:
Func();

View file

@ -28,6 +28,8 @@ typedef enum { SCOPE_FUNCTION, SCOPE_MODULE, SCOPE_GLOBAL } IDScope;
class ID final : public BroObj, public notifier::Modifiable {
public:
static inline const IntrusivePtr<ID> nil;
ID(const char* name, IDScope arg_scope, bool arg_is_export);
~ID() override;

View file

@ -14,7 +14,6 @@ typedef PList<Scope> scope_list;
static scope_list scopes;
static Scope* top_scope;
static IntrusivePtr<ID> nil_id;
Scope::Scope(IntrusivePtr<ID> id, attr_list* al)
: scope_id(std::move(id))
@ -64,7 +63,7 @@ const IntrusivePtr<ID>& Scope::Find(std::string_view name) const
if ( entry != local.end() )
return entry->second;
return nil_id;
return ID::nil;
}
IntrusivePtr<ID> Scope::Remove(std::string_view name)
@ -172,7 +171,7 @@ const IntrusivePtr<ID>& lookup_ID(const char* name, const char* curr_module,
return global_scope()->Find(globalname);
}
return nil_id;
return ID::nil;
}
IntrusivePtr<ID> install_ID(const char* name, const char* module_name,

View file

@ -111,8 +111,7 @@ int BroType::MatchesIndex(ListExpr* const index) const
const IntrusivePtr<BroType>& BroType::Yield() const
{
static IntrusivePtr<BroType> nil;
return nil;
return BroType::nil;
}
bool BroType::HasField(const char* /* field */) const

View file

@ -141,6 +141,8 @@ const int MATCHES_INDEX_VECTOR = 2;
class BroType : public BroObj {
public:
static inline const IntrusivePtr<BroType> nil;
explicit BroType(TypeTag tag, bool base_type = false);
// Performs a shallow clone operation of the Bro type.
@ -456,6 +458,8 @@ protected:
class FuncType final : public BroType {
public:
static inline const IntrusivePtr<FuncType> nil;
/**
* Prototype is only currently used for events and hooks which declare
* multiple signature prototypes that allow users to have handlers

View file

@ -1897,9 +1897,6 @@ IntrusivePtr<Val> TableVal::Default(const IntrusivePtr<Val>& index)
const IntrusivePtr<Val>& TableVal::Find(const IntrusivePtr<Val>& index)
{
static IntrusivePtr<Val> nil;
static IntrusivePtr<Val> exists = val_mgr->True();
if ( subnets )
{
TableEntryVal* v = (TableEntryVal*) subnets->Lookup(index.get());
@ -1911,10 +1908,10 @@ const IntrusivePtr<Val>& TableVal::Find(const IntrusivePtr<Val>& index)
if ( v->GetVal() )
return v->GetVal();
return exists;
return val_mgr->True();
}
return nil;
return Val::nil;
}
const PDict<TableEntryVal>* tbl = AsTable();
@ -1935,12 +1932,12 @@ const IntrusivePtr<Val>& TableVal::Find(const IntrusivePtr<Val>& index)
if ( v->GetVal() )
return v->GetVal();
return exists;
return val_mgr->True();
}
}
}
return nil;
return Val::nil;
}
IntrusivePtr<Val> TableVal::FindOrDefault(const IntrusivePtr<Val>& index)
@ -3139,10 +3136,8 @@ bool VectorVal::AddTo(Val* val, bool /* is_first_init */) const
const IntrusivePtr<Val>& VectorVal::At(unsigned int index) const
{
static IntrusivePtr<Val> nil;
if ( index >= val.vector_val->size() )
return nil;
return Val::nil;
return (*val.vector_val)[index];
}

View file

@ -118,6 +118,8 @@ union BroValUnion {
class Val : public BroObj {
public:
static inline const IntrusivePtr<Val> nil;
Val(double d, TypeTag t)
: val(d), type(base_type(t))
{}