Skip to content

Commit

Permalink
Merge branch 'master' into leebenson/tap2
Browse files Browse the repository at this point in the history
  • Loading branch information
leebenson committed Mar 26, 2021
2 parents d121814 + c9a12b3 commit 36b297f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/vrl/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ criterion = "0.3"
[[bench]]
name = "path"
harness = false

[[bench]]
name = "kind"
harness = false
34 changes: 34 additions & 0 deletions lib/vrl/compiler/benches/kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use std::fmt;
use vrl_compiler::value::Kind;

struct Parameters {
basis: Kind,
}

static PARAMETERS: [Parameters; 4] = [
Parameters { basis: Kind::Bytes },
Parameters { basis: Kind::Array },
Parameters { basis: Kind::Regex },
Parameters { basis: Kind::Null },
];

impl fmt::Display for Parameters {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.basis)
}
}

fn benchmark_kind_display(c: &mut Criterion) {
let mut group = c.benchmark_group("vrl_compiler::value::kind::display");
for param in &PARAMETERS {
group.bench_with_input(BenchmarkId::from_parameter(param), &param, |b, &param| {
b.iter(|| format!("{}", param.basis))
});
}
}

criterion_group!(name = vrl_compiler_kind;
config = Criterion::default();
targets = benchmark_kind_display);
criterion_main!(vrl_compiler_kind);
35 changes: 32 additions & 3 deletions lib/vrl/compiler/src/value/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,29 @@ impl Kind {
self == self.scalar()
}

pub(crate) fn quoted(self) -> String {
format!(r#""{}""#, self.as_str())
/// Returns a quoted variant of `as_str`
///
/// This function is a close duplicate of `as_str`, returning the same
/// underlying str but with quotes. We avoid the obvious `format!("{}",
/// self.as_str())` here as that incurs an allocation cost and the `Display`
/// of `Kind` is sometimes in the hot path.
///
/// See https://github.com/timberio/vector/pull/6878 for details.
pub(crate) fn quoted(self) -> &'static str {
match self {
Kind::Bytes => "\"string\"",
Kind::Integer => "\"integer\"",
Kind::Float => "\"float\"",
Kind::Boolean => "\"boolean\"",
Kind::Object => "\"object\"",
Kind::Array => "\"array\"",
Kind::Timestamp => "\"timestamp\"",
Kind::Regex => "\"regex\"",
Kind::Null => "\"null\"",
_ if self.is_all() => "\"unknown type\"",
_ if self.is_empty() => "\"none\"",
_ => "\"multiple\"",
}
}

pub fn as_str(self) -> &'static str {
Expand Down Expand Up @@ -137,7 +158,7 @@ macro_rules! impl_kind {
return write!(f, "{}", self.quoted())
}

let mut kinds = vec![];
let mut kinds = Vec::with_capacity(16);
$(paste::paste! {
if self.[<contains_ $name>]() {
kinds.push(Kind::$kind.quoted())
Expand Down Expand Up @@ -229,6 +250,14 @@ impl From<&Value> for Kind {
mod tests {
use super::*;

#[test]
fn quoted() {
for i in 0..u16::MAX {
let kind = Kind::new(i);
assert_eq!(kind.quoted(), format!(r#""{}""#, kind.as_str()));
}
}

#[test]
fn kind_is_scalar() {
let scalars = vec![
Expand Down

0 comments on commit 36b297f

Please sign in to comment.