Skip to content

Commit

Permalink
tests: Add a benchmark for Delimiters::from_byte
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Sep 11, 2023
1 parent 12ceb37 commit adf08ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl Delimiters {
}

#[inline]
fn from_byte(byte: Option<u8>) -> Delimiters {
pub(crate) fn from_byte(byte: Option<u8>) -> Delimiters {
const TABLE: [Delimiters; 256] = {
let mut table = [Delimiter::None; 256];
table[b';' as usize] = Delimiter::Semicolon;
Expand Down
14 changes: 13 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use encoding_rs;
use serde_json::{self, json, Map, Value};

use crate::color::{parse_color_with, FromParsedColor};
use crate::{ColorParser, PredefinedColorSpace};
use crate::{ColorParser, PredefinedColorSpace, Delimiters};

#[cfg(feature = "bench")]
use self::test::Bencher;
Expand Down Expand Up @@ -922,6 +922,18 @@ impl<'a> ToJson for CowRcStr<'a> {
}
}

#[bench]
#[cfg(feature = "bench")]
fn delimiter_from_byte(b: &mut Bencher) {
b.iter(|| {
for _ in 0..1000 {
for i in 0..256 {
std::hint::black_box(Delimiters::from_byte(Some(i as u8)));
}
}
})
}

#[cfg(feature = "bench")]
const BACKGROUND_IMAGE: &'static str = include_str!("big-data-url.css");

Expand Down

0 comments on commit adf08ea

Please sign in to comment.