mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 00:28:21 +00:00
Scope: convert Scope::Lookup() and others to template
Allows passing rvalue references which eliminates unnecessary std::string copies. This speeds up Zeek startup by 1-2%.
This commit is contained in:
parent
f8e9cc0fc5
commit
f1908b6212
2 changed files with 13 additions and 7 deletions
|
@ -177,11 +177,11 @@ ID* install_ID(const char* name, const char* module_name,
|
||||||
|
|
||||||
ID* id = new ID(full_name.data(), scope, is_export);
|
ID* id = new ID(full_name.data(), scope, is_export);
|
||||||
if ( SCOPE_FUNCTION != scope )
|
if ( SCOPE_FUNCTION != scope )
|
||||||
global_scope()->Insert(full_name, id);
|
global_scope()->Insert(std::move(full_name), id);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
id->SetOffset(top_scope->Length());
|
id->SetOffset(top_scope->Length());
|
||||||
top_scope->Insert(full_name, id);
|
top_scope->Insert(std::move(full_name), id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
|
|
16
src/Scope.h
16
src/Scope.h
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
@ -18,18 +19,23 @@ public:
|
||||||
explicit Scope(ID* id, attr_list* al);
|
explicit Scope(ID* id, attr_list* al);
|
||||||
~Scope() override;
|
~Scope() override;
|
||||||
|
|
||||||
ID* Lookup(const std::string& name) const
|
template<typename N>
|
||||||
|
ID* Lookup(N &&name) const
|
||||||
{
|
{
|
||||||
const auto& entry = local.find(name);
|
const auto& entry = local.find(std::forward<N>(name));
|
||||||
if ( entry != local.end() )
|
if ( entry != local.end() )
|
||||||
return entry->second;
|
return entry->second;
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
void Insert(const std::string& name, ID* id) { local[name] = id; }
|
|
||||||
ID* Remove(const std::string& name)
|
template<typename N>
|
||||||
|
void Insert(N &&name, ID* id) { local[std::forward<N>(name)] = id; }
|
||||||
|
|
||||||
|
template<typename N>
|
||||||
|
ID* Remove(N &&name)
|
||||||
{
|
{
|
||||||
const auto& entry = local.find(name);
|
const auto& entry = local.find(std::forward<N>(name));
|
||||||
if ( entry != local.end() )
|
if ( entry != local.end() )
|
||||||
{
|
{
|
||||||
ID* id = entry->second;
|
ID* id = entry->second;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue