From 50c2791b091cd116f54866c7efe1d025a8d5825b Mon Sep 17 00:00:00 2001 From: John Children Date: Wed, 27 Nov 2024 15:53:47 +0000 Subject: [PATCH] perf: Avoid rehashing in index_map The IndexMap data structure is an unordered_map which means that if the hash table is too small it can result in having to rehash all members of the table as seen in https://github.com/CQCL/tket/issues/1696 . This commit attempts to reserve an exact size of hash table up front such that rehashing is unlikely as an additional optimization for large circuits. --- tket/src/Circuit/Circuit.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tket/src/Circuit/Circuit.cpp b/tket/src/Circuit/Circuit.cpp index 115880b746..b5826dc75c 100644 --- a/tket/src/Circuit/Circuit.cpp +++ b/tket/src/Circuit/Circuit.cpp @@ -132,6 +132,9 @@ VertexVec Circuit::vertices_in_order() /*const*/ { IndexMap Circuit::index_map() const { IndexMap im; + // Reserve space for every edge to avoid rehashing. + im.reserve(boost::concepts::EdgeListGraph::num_edges(dag)); + std::size_t i = 0; BGL_FORALL_VERTICES(v, dag, DAG) { im[v] = i++; } return im;