Add Component::GetCanonicalNameVal to avoid extra StringVal allocations

This commit is contained in:
Tim Wojtulewicz 2022-05-10 13:31:06 -07:00
parent 22ab3bb0b3
commit a680c2faf0
5 changed files with 51 additions and 3 deletions

View file

@ -50,7 +50,7 @@ function __name%(atype: AllAnalyzers::Tag%) : string
if ( ! component ) if ( ! component )
return zeek::make_intrusive<zeek::StringVal>("<error>"); return zeek::make_intrusive<zeek::StringVal>("<error>");
return zeek::make_intrusive<zeek::StringVal>(component->CanonicalName()); return component->CanonicalNameVal();
%} %}
function __tag%(name: string%) : AllAnalyzers::Tag function __tag%(name: string%) : AllAnalyzers::Tag

View file

@ -103,8 +103,7 @@ function Files::__stop%(file_id: string%): bool
## :zeek:see:`Files::analyzer_name`. ## :zeek:see:`Files::analyzer_name`.
function Files::__analyzer_name%(tag: Files::Tag%) : string function Files::__analyzer_name%(tag: Files::Tag%) : string
%{ %{
const auto& n = zeek::file_mgr->GetComponentName(zeek::IntrusivePtr{zeek::NewRef{}, tag->AsEnumVal()}); return zeek::file_mgr->GetComponentNameVal(zeek::IntrusivePtr{zeek::NewRef{}, tag->AsEnumVal()});
return zeek::make_intrusive<zeek::StringVal>(n);
%} %}
## :zeek:see:`Files::file_exists`. ## :zeek:see:`Files::file_exists`.

View file

@ -16,6 +16,7 @@ Component::Component(component::Type arg_type, const std::string& arg_name,
tag_subtype(tag_subtype) tag_subtype(tag_subtype)
{ {
canon_name = util::canonify_name(name); canon_name = util::canonify_name(name);
canon_name_val = make_intrusive<StringVal>(canon_name);
} }
void Component::Describe(ODesc* d) const void Component::Describe(ODesc* d) const

View file

@ -8,6 +8,7 @@
#include "zeek/Tag.h" #include "zeek/Tag.h"
#include "zeek/Type.h" #include "zeek/Type.h"
#include "zeek/Val.h"
namespace zeek namespace zeek
{ {
@ -98,6 +99,7 @@ public:
* ID. * ID.
*/ */
const std::string& CanonicalName() const { return canon_name; } const std::string& CanonicalName() const { return canon_name; }
StringValPtr CanonicalNameVal() const { return canon_name_val; }
/** /**
* Returns a textual representation of the component. This goes into * Returns a textual representation of the component. This goes into
@ -135,6 +137,7 @@ private:
component::Type type; component::Type type;
std::string name; std::string name;
std::string canon_name; std::string canon_name;
StringValPtr canon_name_val;
/** The automatically assigned component tag */ /** The automatically assigned component tag */
zeek::Tag tag; zeek::Tag tag;

View file

@ -74,6 +74,22 @@ public:
*/ */
const std::string& GetComponentName(EnumValPtr val) const; const std::string& GetComponentName(EnumValPtr val) const;
/**
* Get a component name from its tag.
*
* @param tag A component's tag.
* @return The canonical component name as a StringValPtr.
*/
StringValPtr GetComponentNameVal(zeek::Tag tag) const;
/**
* Get a component name from it's enum value.
*
* @param val A component's enum value.
* @return The canonical component name as a StringValPtr.
*/
StringValPtr GetComponentNameVal(EnumValPtr val) const;
/** /**
* Get a component tag from its name. * Get a component tag from its name.
* *
@ -216,6 +232,35 @@ template <class C> const std::string& ComponentManager<C>::GetComponentName(Enum
return error; return error;
} }
template <class C> StringValPtr ComponentManager<C>::GetComponentNameVal(zeek::Tag tag) const
{
static auto error = make_intrusive<StringVal>("<error>");
if ( ! tag )
return error;
if ( C* c = Lookup(tag) )
return c->CanonicalNameVal();
reporter->InternalWarning("requested name of unknown component tag %s", tag.AsString().c_str());
return error;
}
template <class C> StringValPtr ComponentManager<C>::GetComponentNameVal(EnumValPtr val) const
{
static auto error = make_intrusive<StringVal>("<error>");
if ( ! val )
return error;
if ( C* c = Lookup(val.get()) )
return c->CanonicalNameVal();
reporter->InternalWarning("requested name of unknown component tag %s",
val->AsString()->CheckString());
return error;
}
template <class C> zeek::Tag ComponentManager<C>::GetComponentTag(const std::string& name) const template <class C> zeek::Tag ComponentManager<C>::GetComponentTag(const std::string& name) const
{ {
C* c = Lookup(name); C* c = Lookup(name);