[static runtime] Pre-allocate hash tables (#65343)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/65343

No reason not to save a bit on re-hashing.
ghstack-source-id: 140052518

Test Plan:
CI

Static runtime startup seems to go from 5.9-6.0s to 5.8s-6.0s, perf shows less time spent rehashing

Reviewed By: mikeiovine

Differential Revision: D31027362

fbshipit-source-id: 39dd53ecd462693b518535856ddd92df78a4977b
This commit is contained in:
Scott Wolchok 2021-10-08 10:24:36 -07:00 committed by Facebook GitHub Bot
parent 0020a151c6
commit 3ef69a4598

View File

@ -161,6 +161,8 @@ LivenessMap GetLivenessMap(
std::vector<const Value*> values_in_creation_order;
FastMap<const Value*, size_t> values_to_idx_in_creation_order;
for (const auto* node : graph->nodes()) {
values_to_idx_in_creation_order.reserve(
values_to_idx_in_creation_order.size() + node->outputs().size());
for (const auto* v : node->outputs()) {
values_to_idx_in_creation_order.emplace(
v, values_in_creation_order.size());
@ -184,6 +186,7 @@ LivenessMap GetLivenessMap(
auto& v_live_set = liveness_map[v] = {};
v_live_set.reserve(live_values_use_chain.size());
for (const auto& live_v : live_values_use_chain) {
v_live_set.insert(live_v.first);
liveness_map.at(live_v.first).insert(v);
@ -192,7 +195,7 @@ LivenessMap GetLivenessMap(
// only add values to the live set if they
// have deps, otherwise they die immediately
if (v->uses().size()) {
live_values_use_chain[v] = {};
live_values_use_chain[v] = FastSet<const Node*>(v->uses().size());
}
// record the relationship between v (Value) and its uses (Node)