You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by ag...@apache.org on 2026/05/05 15:25:50 UTC

(datafusion-comet) branch main updated: perf: reduce per-node allocations in to_native_metric_node (#4075)

This is an automated email from the ASF dual-hosted git repository.

andygrove pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-comet.git


The following commit(s) were added to refs/heads/main by this push:
     new 21ab33be1 perf: reduce per-node allocations in to_native_metric_node (#4075)
21ab33be1 is described below

commit 21ab33be117cc1e6fdb4a4ed03e788a8ac4ac48b
Author: Andy Grove <ag...@apache.org>
AuthorDate: Tue May 5 09:25:44 2026 -0600

    perf: reduce per-node allocations in to_native_metric_node (#4075)
---
 native/core/src/execution/metrics/utils.rs | 33 +++++++++++++++---------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/native/core/src/execution/metrics/utils.rs b/native/core/src/execution/metrics/utils.rs
index eb7e10bfc..94cc5892a 100644
--- a/native/core/src/execution/metrics/utils.rs
+++ b/native/core/src/execution/metrics/utils.rs
@@ -45,11 +45,6 @@ pub(crate) fn update_comet_metric(
 pub(crate) fn to_native_metric_node(
     spark_plan: &Arc<SparkPlan>,
 ) -> Result<NativeMetricNode, CometError> {
-    let mut native_metric_node = NativeMetricNode {
-        metrics: HashMap::new(),
-        children: Vec::new(),
-    };
-
     let node_metrics = if spark_plan.additional_native_plans.is_empty() {
         spark_plan.native_plan.metrics()
     } else {
@@ -68,18 +63,24 @@ pub(crate) fn to_native_metric_node(
         Some(metrics.aggregate_by_name())
     };
 
-    // add metrics
-    node_metrics
-        .unwrap_or_default()
-        .iter()
-        .map(|m| m.value())
-        .map(|m| (m.name(), m.as_usize() as i64))
-        .for_each(|(name, value)| {
-            native_metric_node.metrics.insert(name.to_string(), value);
-        });
+    let children = spark_plan.children();
+    let mut native_metric_node = NativeMetricNode {
+        // Most operator metric maps are well under 20 entries (e.g. hash-join: 9,
+        // native-scan: ~20). Pre-sizing to 16 avoids the default-capacity rehash.
+        metrics: HashMap::with_capacity(16),
+        children: Vec::with_capacity(children.len()),
+    };
+
+    if let Some(metrics) = node_metrics {
+        for m in metrics.iter() {
+            let value = m.value();
+            native_metric_node
+                .metrics
+                .insert(value.name().to_string(), value.as_usize() as i64);
+        }
+    }
 
-    // add children
-    for child_plan in spark_plan.children() {
+    for child_plan in children {
         let child_node = to_native_metric_node(child_plan)?;
         native_metric_node.children.push(child_node);
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org