Skip to content

Commit

Permalink
perf: skip while (#24)
Browse files Browse the repository at this point in the history
* refactor bench to return parsed value

* use `for ... { ... by += 1 ... }` instead of `.iter().take_while(...).count()`
  • Loading branch information
kanarus authored Jul 8, 2024
1 parent 98f9bc4 commit ef8cd6f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "byte_reader"
version = "3.1.0"
version = "3.1.1"
edition = "2021"
authors = ["kanarus <[email protected]>"]
documentation = "https://docs.rs/byte_reader"
Expand Down
4 changes: 3 additions & 1 deletion package/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ impl<'r> Reader<'r> {

/// Skip next byte while `condition` holds on it
#[inline] pub fn skip_while(&mut self, condition: impl Fn(&u8)->bool) {
let by = self.remaining().iter().take_while(|b| condition(b)).count();
let mut by = 0; for b in self.remaining() {
if condition(b) {by += 1} else {break}
}
self.advance_unchecked_by(by)
}
/// `skip_while(u8::is_ascii_whitespace)`
Expand Down
33 changes: 20 additions & 13 deletions test/benches/prisma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ datasource db {
#[bench] fn read_a_schema_dot_prisma(b: &mut test::Bencher) {
use prisma::*;

b.iter(|| for _ in 0..10 {
let mut r = byte_reader::Reader::new(SCHEMA.as_bytes());
assert_eq!(Schema::parse(&mut r), Schema {
generator_client: GeneratorClient {
provider: "qujila",
output: "../src/my_db_module",
},
datasource: Datasouce {
name: "db",
provider: "postgresql",
url: "DATABASE_URL",
}
})
let mut r = byte_reader::Reader::new(SCHEMA.as_bytes());
assert_eq!(Schema::parse(&mut r), Schema {
generator_client: GeneratorClient {
provider: "qujila",
output: "../src/my_db_module",
},
datasource: Datasouce {
name: "db",
provider: "postgresql",
url: "DATABASE_URL",
}
});

b.iter(|| {
let mut buf = Vec::with_capacity(10);
for _ in 0..10 {
let mut r = byte_reader::Reader::new(SCHEMA.as_bytes());
buf.push(Schema::parse(&mut r))
}
buf
})
}

Expand Down

0 comments on commit ef8cd6f

Please sign in to comment.