From 0ea6b02cc859c173c737b0fff028f5f59dd8dc23 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Tue, 21 Mar 2023 09:48:06 -0700 Subject: [PATCH 1/2] rust: use more idiomatic ascii comparisons Surprisingly, this tiny change yields a 15% speed up. --- README.md | 2 +- main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f5fb0a1..e5fab4c 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ implementations compare to each other. | grep | 0m18.034s | 0m15.713s | 0m2.257s | | ripgrep | 0m1.709s | 0m1.541s | 0m0.147s | | simple (Go) | 0m1.889s | 0m1.679s | 0m0.211s | -| simple (Rust) | 0m2.169s | 0m1.943s | 0m0.219s | +| simple (Rust) | 0m1.623s | 0m1.415s | 0m0.204s | | simple (Node) | 0m6.458s | 0m6.043s | 0m0.627s | | custom (C) | **0m0.222s** | **0m0.079s** | **0m0.141s** | diff --git a/main.rs b/main.rs index 87b13c6..f9540b6 100644 --- a/main.rs +++ b/main.rs @@ -26,7 +26,7 @@ fn scan_slice(inb: &[u8]) -> usize { let mut count = 0; let len = inb.len(); for (i, &b) in inb.into_iter().enumerate() { - if b >= b'0' && b <= b'9' || b >= b'a' && b <= b'f' { + if b.is_ascii_digit() || (b'a'..=b'f').contains(&b) { count += 1; continue } From 21243323cee05fd5a946f0738de760113c265850 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Tue, 21 Mar 2023 09:53:54 -0700 Subject: [PATCH 2/2] rust: reduce read buffer size Reduce the size of the read buffer from 64MB to 256K which is much friendlier to the CPU cache. This is the same size as used by the optimized C version and it turns out to yield a small performance boost here as well. In fact, it provides enough of a speed up that the naive implementation in Rust is now consistently faster than ripgrep, which is a little surprising. --- README.md | 2 +- main.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e5fab4c..081b8da 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ implementations compare to each other. | grep | 0m18.034s | 0m15.713s | 0m2.257s | | ripgrep | 0m1.709s | 0m1.541s | 0m0.147s | | simple (Go) | 0m1.889s | 0m1.679s | 0m0.211s | -| simple (Rust) | 0m1.623s | 0m1.415s | 0m0.204s | +| simple (Rust) | 0m1.461s | 0m1.325s | 0m0.131s | | simple (Node) | 0m6.458s | 0m6.043s | 0m0.627s | | custom (C) | **0m0.222s** | **0m0.079s** | **0m0.141s** | diff --git a/main.rs b/main.rs index f9540b6..3acb970 100644 --- a/main.rs +++ b/main.rs @@ -43,7 +43,7 @@ fn scan_slice(inb: &[u8]) -> usize { } fn sscan(mut input: impl Read) { - let mut backbuf = vec![0u8; 64*1024*1024]; + let mut backbuf = vec![0u8; 64*4096]; let bbuf = backbuf.as_mut_slice(); // let mut bbuf = [0u8; 2*1024*1024]; let mut off = 0;