Skip to content

Commit bd916b4

Browse files
committed
shuf: use memchr in split_seps
1 parent 9d502ec commit bd916b4

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/shuf/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ path = "src/shuf.rs"
2020

2121
[dependencies]
2222
clap = { workspace = true }
23+
fluent = { workspace = true }
2324
itoa = { workspace = true }
25+
memchr = { workspace = true }
2426
rand = { workspace = true }
2527
rand_chacha = { workspace = true }
28+
rustc-hash = { workspace = true }
2629
sha3 = { workspace = true }
2730
uucore = { workspace = true }
28-
fluent = { workspace = true }
29-
rustc-hash = { workspace = true }
3031

3132
[[bin]]
3233
name = "shuf"

src/uu/shuf/src/shuf.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
1313
use std::str::FromStr;
1414

1515
use clap::{Arg, ArgAction, Command, builder::ValueParser};
16+
use memchr::memchr_iter;
1617
use rand::{
1718
RngExt as _,
1819
rngs::ThreadRng,
@@ -267,14 +268,13 @@ fn read_input_file(filename: &Path) -> UResult<Vec<u8>> {
267268
}
268269
}
269270

270-
fn split_seps(data: &[u8], sep: u8) -> Vec<&[u8]> {
271+
pub fn split_seps(data: &[u8], sep: u8) -> Vec<&[u8]> {
271272
// A single trailing separator is ignored.
272273
// If data is empty (and does not even contain a single 'sep'
273274
// to indicate the presence of an empty element), then behave
274275
// as if the input contained no elements at all.
275-
const PREDICTED_LINE_LENGTH: usize = 64;
276-
let predicted_capacity = data.len() / PREDICTED_LINE_LENGTH;
277-
let mut elements = Vec::with_capacity(predicted_capacity);
276+
let sep_count = memchr_iter(sep, data).count();
277+
let mut elements = Vec::with_capacity(sep_count + 1);
278278
elements.extend(data.split(|&b| b == sep));
279279
let _ = elements.pop_if(|e| e.is_empty());
280280
elements

0 commit comments

Comments
 (0)