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:
Max Kellermann 2020-02-04 11:53:33 +01:00
parent f8e9cc0fc5
commit f1908b6212
2 changed files with 13 additions and 7 deletions

View file

@ -2,6 +2,7 @@
#pragma once
#include <utility>
#include <string>
#include <map>
@ -18,18 +19,23 @@ public:
explicit Scope(ID* id, attr_list* al);
~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() )
return entry->second;
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() )
{
ID* id = entry->second;