Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[oximeter] use Cow<'static, str> for FieldValue::String #5670

Merged
merged 3 commits into from May 8, 2024

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Apr 30, 2024

Fixes #5664.

Currently, Oximeter FieldValue::String fields are always represented
by a String value. This is somewhat unfortunate, as it requires
allocating a new String for every string field value that's emitted to
Oximeter, even when the string value is fixed at compile-time --- for
example, when an enum is used to generate a metric label with &'static str values, they must be alloced into new Strings to record the
metric.

This commit changes FieldValue::String from holding a String to
holding a Cow<'static, str>, so that static string constants need not
be heap-allocated.

Additionally, I've changed Oximeter's self_stats module to use
Cow<'static, str> to represent the field value generated by the
FailureReason enum. This way, a heap-allocated string is only
necessary for dynamically formatted variants (the FailureReason::Other
variant, which owns an HTTP StatusCode). For the
FailureReason::Unreachable and FailureReason::Deserialization
variants, we can just emit a &'static str without allocating.
Hopefully, this reduces resident memory for the collector process
somewhat when encountering a lot of failures.

Currently, Oximeter `FieldValue::String` fields are always represented
by a `String` value. This is somewhat unfortunate, as it requires
allocating a new `String` for every string field value that's emitted to
Oximeter, even when the string value is fixed at compile-time --- for
example, when an enum is used to generate a metric label with
`&'static str` values, they must be alloced into new `String`s to record
the metric.

This commit changes `FieldValue::String` from holding a `String` to
holding a `Cow<'static, str>`, so that static string constants need not
be heap-allocated.
This commit changes Oximeter's `self_stats` module to use `Cow<'static,
str>` to represent the field value generated by the `FailureReason`
enum. This way, a heap-allocated string is only necessary for
dynamically formatted variants (the `FailureReason::Other` variant,
which owns an HTTP `StatusCode`). For the `FailureReason::Unreachable`
and `FailureReason::Deserialization` variants, we can just emit a
`&'static str` without allocating. Hopefully, this reduces resident
memory for the collector process somewhat when encountering a lot of
failures.
@hawkw hawkw requested a review from bnaecker April 30, 2024 16:52
Copy link
Collaborator

@bnaecker bnaecker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience while I was handling some new customer features. This all looks good to me, thanks!

@hawkw
Copy link
Member Author

hawkw commented May 8, 2024

Thanks for your patience while I was handling some new customer features. This all looks good to me, thanks!

No worries --- thanks for taking a look!

@hawkw hawkw merged commit 1486971 into main May 8, 2024
20 checks passed
@hawkw hawkw deleted the eliza/oximeter-static-str branch May 8, 2024 22:02
hawkw added a commit that referenced this pull request May 9, 2024
Fixes #5664.

Currently, Oximeter `FieldValue::String` fields are always represented
by a `String` value. This is somewhat unfortunate, as it requires
allocating a new `String` for every string field value that's emitted to
Oximeter, even when the string value is fixed at compile-time --- for
example, when an enum is used to generate a metric label with `&'static
str` values, they must be alloced into new `String`s to record the
metric.

This commit changes `FieldValue::String` from holding a `String` to
holding a `Cow<'static, str>`, so that static string constants need not
be heap-allocated.

Additionally, I've changed Oximeter's `self_stats` module to use
`Cow<'static, str>` to represent the field value generated by the
`FailureReason` enum. This way, a heap-allocated string is only
necessary for dynamically formatted variants (the `FailureReason::Other`
variant, which owns an HTTP `StatusCode`). For the
`FailureReason::Unreachable` and `FailureReason::Deserialization`
variants, we can just emit a `&'static str` without allocating.
Hopefully, this reduces resident memory for the collector process
somewhat when encountering a lot of failures.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

oximeter: want to be able to use &'static strs (and Cows) in derive(Metric)
2 participants