Skip to content

Commit

Permalink
stats: use extend_from_slice when possible
Browse files Browse the repository at this point in the history
its more readable, and a tad more performant than multiple pushes.
Also since empty()/String::new() is a const fn, the compiler "may" make it more efficient/performant

- also made it plain that from_bytes is using the simd accelerated from_utf8
  • Loading branch information
jqnatividad committed Apr 29, 2023
1 parent dbcea39 commit c71ad4e
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions src/cmd/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,12 +730,14 @@ impl Args {
fields.push("cardinality");
}
if self.flag_mode || all {
fields.push("mode");
fields.push("mode_count");
fields.push("mode_occurrences");
fields.push("antimode");
fields.push("antimode_count");
fields.push("antimode_occurrences");
fields.extend_from_slice(&[
"mode",
"mode_count",
"mode_occurrences",
"antimode",
"antimode_count",
"antimode_occurrences",
]);
}
csv::StringRecord::from(fields)
}
Expand Down Expand Up @@ -1047,34 +1049,25 @@ impl Stats {
.as_ref()
.and_then(|mm| mm.show(typ, round_places))
{
pieces.push(mm.0);
pieces.push(mm.1);
pieces.push(mm.2);
pieces.extend_from_slice(&[mm.0, mm.1, mm.2]);
} else {
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.extend_from_slice(&[empty(), empty(), empty()]);
}

// min/max length
if typ == FieldType::TDate || typ == FieldType::TDateTime {
// returning min/max length for dates doesn't make sense
// especially since we convert the date stats to rfc3339 format
pieces.push(empty());
pieces.push(empty());
pieces.extend_from_slice(&[empty(), empty()]);
} else if let Some(mm) = self.minmax.as_ref().and_then(TypedMinMax::len_range) {
pieces.push(mm.0);
pieces.push(mm.1);
pieces.extend_from_slice(&[mm.0, mm.1]);
} else {
pieces.push(empty());
pieces.push(empty());
pieces.extend_from_slice(&[empty(), empty()]);
}

// mean, stddev & variance
if typ == TString || typ == TNull {
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.extend_from_slice(&[empty(), empty(), empty()]);
} else if let Some(ref v) = self.online {
if self.typ == TFloat || self.typ == TInteger {
pieces.push(util::round_num(v.mean(), round_places));
Expand All @@ -1098,9 +1091,7 @@ impl Stats {
pieces.push(empty());
}
} else {
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.extend_from_slice(&[empty(), empty(), empty()]);
}

// nullcount
Expand Down Expand Up @@ -1163,15 +1154,17 @@ impl Stats {
}) {
None => {
if self.which.quartiles {
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.extend_from_slice(&[
empty(),
empty(),
empty(),
empty(),
empty(),
empty(),
empty(),
empty(),
empty(),
]);
}
}
Some((q1, q2, q3)) => {
Expand Down Expand Up @@ -1242,10 +1235,7 @@ impl Stats {
pieces.push(empty());
}
if self.which.mode {
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.push(empty());
pieces.extend_from_slice(&[empty(), empty(), empty(), empty()]);
}
}
Some(ref mut v) => {
Expand Down Expand Up @@ -1653,7 +1643,7 @@ impl Commute for TypedMinMax {
#[allow(clippy::inline_always)]
#[inline(always)]
fn from_bytes<T: std::str::FromStr>(bytes: &[u8]) -> Option<T> {
if let Ok(x) = from_utf8(bytes) {
if let Ok(x) = simdutf8::basic::from_utf8(bytes) {
x.parse().ok()
} else {
None
Expand Down

0 comments on commit c71ad4e

Please sign in to comment.