Skip to content

Commit 4f620c3

Browse files
authored
fix: avoid vector copies in CheckIfSubtreesAreEqual (microsoft#27854)
`indices` is built once and then only read during recursive calls to `CheckIfSubtreesAreEqual`. However it was passed by value, causing a full copy on every recursive call. Changed to `const&`. ## Data from the profiler: To collect the following data, a model with a single TreeEnsembleClassifier node (5000 trees and 3.3 million nodes) has been used. The loading time dropped from 18 minutes to about 4 seconds. ### After <img width="1793" height="547" alt="Screenshot 2026-03-25 at 6 40 25 PM" src="https://github.com/user-attachments/assets/d7c00335-8246-4bd1-9e4d-b0e956d48cdd" /> ### Before <img width="1763" height="548" alt="Screenshot 2026-03-25 at 6 40 40 PM" src="https://github.com/user-attachments/assets/35683112-2919-4031-955c-922937f2df8f" />
1 parent b429dff commit 4f620c3

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

onnxruntime/core/providers/cpu/ml/tree_ensemble_common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class TreeEnsembleCommon : public TreeEnsembleCommonAttributes {
115115
const InlinedVector<size_t>& truenode_ids, const InlinedVector<size_t>& falsenode_ids, gsl::span<const int64_t> nodes_featureids,
116116
gsl::span<const ThresholdType> nodes_values_as_tensor, gsl::span<const float> node_values,
117117
gsl::span<const float> target_class_weights, gsl::span<const ThresholdType> target_class_weights_as_tensor,
118-
const InlinedVector<TreeNodeElementId>& node_tree_ids, InlinedVector<std::pair<TreeNodeElementId, uint32_t>> indices);
118+
const InlinedVector<TreeNodeElementId>& node_tree_ids, const InlinedVector<std::pair<TreeNodeElementId, uint32_t>>& indices);
119119
size_t AddNodes(const size_t i, const InlinedVector<NODE_MODE_ONNX>& cmodes, const InlinedVector<size_t>& truenode_ids,
120120
const InlinedVector<size_t>& falsenode_ids, gsl::span<const int64_t> nodes_featureids,
121121
gsl::span<const ThresholdType> nodes_values_as_tensor, gsl::span<const float> node_values,
@@ -383,7 +383,7 @@ bool TreeEnsembleCommon<InputType, ThresholdType, OutputType>::CheckIfSubtreesAr
383383
const InlinedVector<size_t>& truenode_ids, const InlinedVector<size_t>& falsenode_ids, gsl::span<const int64_t> nodes_featureids,
384384
gsl::span<const ThresholdType> nodes_values_as_tensor, gsl::span<const float> node_values,
385385
gsl::span<const float> target_class_weights, gsl::span<const ThresholdType> target_class_weights_as_tensor,
386-
const InlinedVector<TreeNodeElementId>& node_tree_ids, InlinedVector<std::pair<TreeNodeElementId, uint32_t>> indices) {
386+
const InlinedVector<TreeNodeElementId>& node_tree_ids, const InlinedVector<std::pair<TreeNodeElementId, uint32_t>>& indices) {
387387
if (left_id == right_id) {
388388
return true;
389389
}

0 commit comments

Comments
 (0)