Skip to content

Commit

Permalink
Optimization: remove unnecessary string concatenation in ODS dynamic …
Browse files Browse the repository at this point in the history
…stats.

Summary:
Current macro does an extra join operation when prefix is available(most common case), this diff eliminates this operation to construct the key string in one shot eliminating a extra temp string allocation+free and the resulting fragmentation.

this operation is about 30% of the key generation (join + free):

https://fburl.com/strobelight/nr0vzg42

{F1974221233}

Motivated by the inefficiency from D67604234 in Re, but the app that will be benefit the most will be fb_monitord (a wdb) this optimization should account for ~0.23% CPU util improvement for fb_monitord. (the eliminated operation takes about 0.23% of the strobe-light samples)

__key_generator cost is currently around [40k $CPU-T1-equiv/year Q4 2024](https://fburl.com/strobelight/nk9vzdjd) so this is a 10k$/year optimization if I understand [the doc](https://www.internalfb.com/wiki/Capacity_Management_Internal/Capacity_Efficiency_pillar/Efficiency_Management_Framework/Measuring_Horizontal_CPU_Wins/) correctly.

Reviewed By: edward-shen

Differential Revision: D67913458

fbshipit-source-id: 08ddaa4de59f43744d202f4c71339580431290b6
  • Loading branch information
zolyfarkas-fb authored and facebook-github-bot committed Jan 9, 2025
1 parent 441dc5d commit 4da8e37
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion shed/stats/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,62 @@ macro_rules! define_stats {
#[doc(hidden)]
#[macro_export]
macro_rules! __define_key_generator {
($name:ident($prefix:literal, $key:expr; $( $placeholder:ident: $type:ty ),+)) => (
fn $name(&($( ref $placeholder, )+): &($( $type, )+)) -> String {
if $prefix.is_empty() {
format!($key, $( $placeholder ),+)
} else {
format!(concat!($prefix, ".", $key), $( $placeholder ),+)
}
}
);
($name:ident($prefix:expr, $key:expr)) => (
fn $name() -> String {
if $prefix.is_empty() {
$key
} else {
format!("{0}.{1}", $prefix, $key)
}
}
);
($name:ident($prefix:expr, $key:expr; $placeholder:ident: $type:ty )) => (
fn $name(&( ref $placeholder, ): &( $type, )) -> String {
if $prefix.is_empty() {
format!($key, $placeholder)
} else {
format!(concat!("{1}.", $key), $placeholder, $prefix )
}
}
);
($name:ident($prefix:expr, $key:expr; $placeholder1:ident: $type1:ty, $placeholder2:ident: $type2:ty )) => (
fn $name(&( ref $placeholder1, ref $placeholder2, ): &( $type1, $type2, )) -> String {
if $prefix.is_empty() {
format!($key, $placeholder1, $placeholder2)
} else {
format!(concat!("{2}.", $key), $placeholder1, $placeholder2, $prefix )
}
}
);
($name:ident($prefix:expr, $key:expr; $placeholder1:ident: $type1:ty, $placeholder2:ident: $type2:ty, $placeholder3:ident: $type3:ty )) => (
fn $name(&( ref $placeholder1, ref $placeholder2, ref $placeholder3,): &( $type1, $type2, $type3,)) -> String {
if $prefix.is_empty() {
format!($key, $placeholder1, $placeholder2, $placeholder3)
} else {
format!(concat!("{3}.", $key), $placeholder1, $placeholder2, $placeholder3, $prefix )
}
}
);
($name:ident($prefix:expr, $key:expr; $( $placeholder:ident: $type:ty ),+)) => (
fn $name(&($( ref $placeholder, )+): &($( $type, )+)) -> String {
let key = format!($key, $( $placeholder ),+);
if $prefix.is_empty() {
key
} else {
[$prefix, &key].join(".")
format!("{0}.{1}", $prefix, key)
}
}
);

}

#[doc(hidden)]
Expand Down

0 comments on commit 4da8e37

Please sign in to comment.