mirror of
https://github.com/zeek/zeek.git
synced 2025-10-05 16:18:19 +00:00
Type: Add TypeManager->TypeList() and use for ListVal()
It turns out that for every ListVal we construct, we also allocate and construct a new TypeList instance, even though they are all the same. Pre-create and cache the type instances in a new TypeManager. The following script runs ~10% faster for me after this change. global tbl: table[string] of string; global i = 0; while ( ++i < 10000000 ) tbl["a"] = "a";
This commit is contained in:
parent
04a2ee7220
commit
24c606b4df
4 changed files with 36 additions and 4 deletions
16
src/Type.cc
16
src/Type.cc
|
@ -1981,6 +1981,22 @@ detail::TraversalCode VectorType::Traverse(detail::TraversalCallback* cb) const
|
||||||
HANDLE_TC_TYPE_POST(tc);
|
HANDLE_TC_TYPE_POST(tc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TypeManager::TypeManager()
|
||||||
|
{
|
||||||
|
for ( auto i = 0u; i < base_list_types.size(); ++i )
|
||||||
|
{
|
||||||
|
TypeTag tag = static_cast<TypeTag>(i);
|
||||||
|
TypePtr pure_type = tag == TYPE_ANY ? nullptr : base_type(tag);
|
||||||
|
base_list_types[tag] = make_intrusive<zeek::TypeList>(pure_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const TypeListPtr& TypeManager::TypeList(TypeTag t) const
|
||||||
|
{
|
||||||
|
assert(t >= 0 && t < NUM_TYPES);
|
||||||
|
return base_list_types[t];
|
||||||
|
}
|
||||||
|
|
||||||
// Returns true if t1 is initialization-compatible with t2 (i.e., if an
|
// Returns true if t1 is initialization-compatible with t2 (i.e., if an
|
||||||
// initializer with type t1 can be used to initialize a value with type t2),
|
// initializer with type t1 can be used to initialize a value with type t2),
|
||||||
// false otherwise. Assumes that t1's tag is different from t2's. Note
|
// false otherwise. Assumes that t1's tag is different from t2's. Note
|
||||||
|
|
16
src/Type.h
16
src/Type.h
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
@ -866,6 +867,21 @@ protected:
|
||||||
TypePtr yield_type;
|
TypePtr yield_type;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Holds pre-allocated Type objects.
|
||||||
|
class TypeManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TypeManager();
|
||||||
|
|
||||||
|
// Get a Typelist instance with the given type tag.
|
||||||
|
const TypeListPtr& TypeList(TypeTag t) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<TypeListPtr, NUM_TYPES> base_list_types;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern TypeManager* type_mgr;
|
||||||
|
|
||||||
// True if the two types are equivalent. If is_init is true then the test is
|
// True if the two types are equivalent. If is_init is true then the test is
|
||||||
// done in the context of an initialization. If match_record_field_names is
|
// done in the context of an initialization. If match_record_field_names is
|
||||||
// true then for record types the field names have to match, too.
|
// true then for record types the field names have to match, too.
|
||||||
|
|
|
@ -1188,10 +1188,7 @@ ValPtr PatternVal::DoClone(CloneState* state)
|
||||||
return state->NewClone(this, make_intrusive<PatternVal>(re));
|
return state->NewClone(this, make_intrusive<PatternVal>(re));
|
||||||
}
|
}
|
||||||
|
|
||||||
ListVal::ListVal(TypeTag t) : Val(make_intrusive<TypeList>(t == TYPE_ANY ? nullptr : base_type(t)))
|
ListVal::ListVal(TypeTag t) : Val(type_mgr->TypeList(t)), tag(t) { }
|
||||||
{
|
|
||||||
tag = t;
|
|
||||||
}
|
|
||||||
|
|
||||||
ListVal::~ListVal() { }
|
ListVal::~ListVal() { }
|
||||||
|
|
||||||
|
|
|
@ -172,6 +172,7 @@ void do_ssl_deinit()
|
||||||
} // namespace
|
} // namespace
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
zeek::TypeManager* zeek::type_mgr = nullptr;
|
||||||
zeek::ValManager* zeek::val_mgr = nullptr;
|
zeek::ValManager* zeek::val_mgr = nullptr;
|
||||||
zeek::packet_analysis::Manager* zeek::packet_mgr = nullptr;
|
zeek::packet_analysis::Manager* zeek::packet_mgr = nullptr;
|
||||||
zeek::analyzer::Manager* zeek::analyzer_mgr = nullptr;
|
zeek::analyzer::Manager* zeek::analyzer_mgr = nullptr;
|
||||||
|
@ -437,6 +438,7 @@ static void terminate_zeek()
|
||||||
delete session_mgr;
|
delete session_mgr;
|
||||||
delete fragment_mgr;
|
delete fragment_mgr;
|
||||||
delete telemetry_mgr;
|
delete telemetry_mgr;
|
||||||
|
delete type_mgr;
|
||||||
|
|
||||||
// free the global scope
|
// free the global scope
|
||||||
pop_scope();
|
pop_scope();
|
||||||
|
@ -593,6 +595,7 @@ SetupResult setup(int argc, char** argv, Options* zopts)
|
||||||
|
|
||||||
run_state::zeek_start_time = util::current_time(true);
|
run_state::zeek_start_time = util::current_time(true);
|
||||||
|
|
||||||
|
type_mgr = new TypeManager();
|
||||||
val_mgr = new ValManager();
|
val_mgr = new ValManager();
|
||||||
reporter = new Reporter(options.abort_on_scripting_errors);
|
reporter = new Reporter(options.abort_on_scripting_errors);
|
||||||
thread_mgr = new threading::Manager();
|
thread_mgr = new threading::Manager();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue