From 96ead2be916174118d20612e2ac54da667dcaa77 Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Sat, 12 Jun 2021 14:11:33 +1000 Subject: [PATCH] Avoid hashing the operator type in the GraphViewer priority node check unless the string has a chance of matching. (#7972) * Avoid hashing the operator type in the GraphViewer priority node check unless the string has a chance of matching. Below are perf numbers from a test that loads 16 models multiple times. I was checking that some unrelated changes didn't have unexpected perf cost and found the PriorityNodeCompare overwhelmed any contribution the other changes were making. *Before* CPU Time:74.678s CPU Time for relevant Top Hotspots std::_Hash_array_representation 20.834s onnxruntime::PriorityNodeCompare::IsHighPri 7.589s onnxruntime::Graph::KahnsTopologicalSort 4.487s *After* CPU Time:47.103s CPU Time for relevant Top Hotspots onnxruntime::Graph::KahnsTopologicalSort 4.465s onnxruntime::PriorityNodeCompare::IsHighPri 2.873s --- onnxruntime/core/graph/graph_viewer.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/onnxruntime/core/graph/graph_viewer.cc b/onnxruntime/core/graph/graph_viewer.cc index 0b25b51a80..5cc0579642 100644 --- a/onnxruntime/core/graph/graph_viewer.cc +++ b/onnxruntime/core/graph/graph_viewer.cc @@ -18,15 +18,19 @@ bool NodeCompare::operator()(const Node* n1, const Node* n2) const { #if !defined(ORT_MINIMAL_BUILD) struct PriorityNodeCompare { inline bool IsHighPri(const Node* n) const { - static const std::unordered_set high_pri_ops = {"Shape", "Size"}; - return high_pri_ops.find(n->OpType()) != high_pri_ops.end(); + // local statics so we can compare std::strings in the checks + static const std::string shape_op("Shape"); + static const std::string size_op("Size"); + + const auto& op_type = n->OpType(); + return op_type == shape_op || op_type == size_op; } // Used for std::priority_queue // If return false, n1 will be output first // If return true, n2 will be output first bool operator()(const Node* n1, const Node* n2) const { - // nodes in global high priorty list will be output first + // nodes in global high priority list will be output first if (IsHighPri(n1) != IsHighPri(n2)) { return IsHighPri(n2); }