alamb opened a new issue, #27965:
URL: https://github.com/apache/datafusion/issues/27965

   ### Is your feature request related to a problem or challenge?
   
   Metric [`Label`] values are always strings: either a `&'static str` or an
   `Arc<str>` (see [`LabelValue`]). A label whose value is naturally an integer,
   such as a partition number, therefore has to be formatted into a freshly
   allocated string before it can be attached to a metric.
   
   This came up in
   [perf(repartition): lazily register send 
metrics](https://github.com/apache/datafusion/pull/27915)
   
([comment](https://github.com/apache/datafusion/pull/27915#discussion_r4773913155)).
   `RepartitionExec` labels each `send_time` metric with its output partition:
   
   
https://github.com/apache/datafusion/blob/465570597a98e882b6e0058c2895a90f127af8e6/datafusion/physical-plan/src/repartition/mod.rs#L1573-L1580
   
   For plans with many operators and many partitions, this can be large
   
   #### How many labels are integers today?
   
   Here are places where partition labels are created in non-test code on 
`main` (`465570597a`):
   
   | Label name | Where | Value | Integer? |
   | --- | --- | --- | --- |
   | `outputPartition` | 
[`RepartitionMetrics::new`](https://github.com/apache/datafusion/blob/465570597a98e882b6e0058c2895a90f127af8e6/datafusion/physical-plan/src/repartition/mod.rs#L1576)
 | `output_partition.to_string()` | **yes** |
   | `partition` | [`impl Display for 
Metric`](https://github.com/apache/datafusion/blob/465570597a98e882b6e0058c2895a90f127af8e6/datafusion/physical-expr-common/src/metrics/mod.rs#L92-L96)
 | `partition.to_string()` | **yes** |
   
   - `outputPartition` is the one that matters, because it is created per metric
     at execution time.
   - `partition` is a temporary `Label` built only to print a metric (the
     partition itself is already stored as `Option<usize>` on [`Metric`]), so it
     allocates once per metric each time metrics are displayed, e.g. in
     `EXPLAIN ANALYZE`.
   
   ### Describe the solution you'd like
   
   Let a [`Label`] be created from an integer without allocating, for example
   
   ```rust
   Label::new("outputPartition", output_partition) // usize
   ```
   
   via `impl From<usize> for LabelValue` (and probably `u64` / `u32`).
   
   Since [`Label::value`] and [`LabelValue::as_str`] return
   `&str`, the string form has to be stored somewhere in the label. One way to 
keep the
   public API unchanged is an inline variant that holds the formatted digits:
   
   ```rust
   enum LabelValueInner {
       Static(&'static str),
       Shared(Arc<str>),
       /// Short value stored inline, such as a formatted integer
       Inline { len: u8, buf: [u8; 22] },
   }
   ```
   
   `usize::MAX` is 20 decimal digits, so any integer fits, and the enum stays 
the
   same size (24 bytes on 64-bit, checked with `size_of`). `PartialEq`, `Hash`,
   `Display`, and the FFI conversion already go through `as_str()`, so they 
would
   not need to change, and a label made from `7usize` would still compare equal 
to
   one made from `"7"`. `LabelValueInner` is private, so adding a variant is 
not a
   breaking change.
   
   The same variant could also hold any short dynamic string (up to 22 bytes)
   without an `Arc` allocation, though that is not needed for this issue.
   
   Once that exists:
   
   - `RepartitionExec` can pass `output_partition` directly.
   - `impl Display for Metric` can stop building a temporary `Label` for the
     partition and write `partition={n}` straight to the formatter. This part 
does
     not depend on the new variant and could be done on its own.
   
   ### Describe alternatives you've considered
   
   1. Store the integer (`Usize(usize)`) and format it on demand. This is the
      smallest representation, but `Label::value() -> &str` and
      `LabelValue::as_str() -> &str` could no longer be implemented, so it would
      be a breaking API change (for example returning `Cow<str>` or adding a
      separate accessor). [`LabelValue`] has been public since 55.0.0
      ([Optimize metric label 
cloning](https://github.com/apache/datafusion/pull/22406)).
   2. A static table of pre-formatted small integers (`"0"`, `"1"`, ...), 
returned
      as `&'static str` and falling back to an allocation above the table size.
      No new variant, but it adds a table and still allocates for large values.
   3. Keep `LabelValue` as it is and have each caller build the labels once and
      clone them, which is what
      [perf(repartition): lazily register send 
metrics](https://github.com/apache/datafusion/pull/27915)
      does. This bounds the cost at one allocation per distinct integer and may 
be
      good enough, in which case this issue can be closed.
   
   ### Additional context
   
   The expected gain is modest and should be measured before merging anything:
   after the PR above, `RepartitionExec` allocates one label string per output
   partition per execution rather than one per input/output pair.
   
   A related source of metric string allocations that I noticed while looking,
   which is not an integer and so is out of scope here:
   
   - 
[`ParquetFileMetrics::new`](https://github.com/apache/datafusion/blob/465570597a98e882b6e0058c2895a90f127af8e6/datafusion/datasource-parquet/src/metrics.rs#L181-L189)
     shares one `Arc<str>` filename label across most of its metrics, but five
     metrics still call `.with_new_label("filename", filename.to_string())`, so
     the filename is copied five more times per file.
   
   [`Label`]: 
https://docs.rs/datafusion/latest/datafusion/physical_expr_common/metrics/struct.Label.html
   [`Label::value`]: 
https://docs.rs/datafusion/latest/datafusion/physical_expr_common/metrics/struct.Label.html#method.value
   [`LabelValue`]: 
https://docs.rs/datafusion/latest/datafusion/physical_expr_common/metrics/struct.LabelValue.html
   [`LabelValue::as_str`]: 
https://docs.rs/datafusion/latest/datafusion/physical_expr_common/metrics/struct.LabelValue.html#method.as_str
   [`Metric`]: 
https://docs.rs/datafusion/latest/datafusion/physical_expr_common/metrics/struct.Metric.html
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to