Skip to content

Commit 5bcc2d5

Browse files
committed
feat: optimize iter matching
There is no need to allocate a vector when matching a sequence of items
1 parent a067b58 commit 5bcc2d5

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

src/uu/chroot/src/chroot.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,20 @@ struct Options {
5454
/// The `spec` must be of the form `[USER][:[GROUP]]`, otherwise an
5555
/// error is returned.
5656
fn parse_userspec(spec: &str) -> UResult<UserSpec> {
57-
match &spec.splitn(2, ':').collect::<Vec<&str>>()[..] {
57+
let mut parts = spec.splitn(2, ':');
58+
match (parts.next(), parts.next()) {
5859
// ""
59-
[""] => Ok(UserSpec::NeitherGroupNorUser),
60+
(Some(""), None) => Ok(UserSpec::NeitherGroupNorUser),
6061
// "usr"
61-
[usr] => Ok(UserSpec::UserOnly(usr.to_string())),
62+
(Some(usr), None) => Ok(UserSpec::UserOnly(usr.to_string())),
6263
// ":"
63-
["", ""] => Ok(UserSpec::NeitherGroupNorUser),
64+
(Some(""), Some("")) => Ok(UserSpec::NeitherGroupNorUser),
6465
// ":grp"
65-
["", grp] => Ok(UserSpec::GroupOnly(grp.to_string())),
66+
(Some(""), Some(grp)) => Ok(UserSpec::GroupOnly(grp.to_string())),
6667
// "usr:"
67-
[usr, ""] => Ok(UserSpec::UserOnly(usr.to_string())),
68+
(Some(usr), Some("")) => Ok(UserSpec::UserOnly(usr.to_string())),
6869
// "usr:grp"
69-
[usr, grp] => Ok(UserSpec::UserAndGroup(usr.to_string(), grp.to_string())),
70+
(Some(usr), Some(grp)) => Ok(UserSpec::UserAndGroup(usr.to_string(), grp.to_string())),
7071
// everything else
7172
_ => Err(ChrootError::InvalidUserspec(spec.to_string()).into()),
7273
}

src/uu/nproc/src/nproc.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore (ToDO) NPROCESSORS nprocs numstr threadstr sysconf
6+
// spell-checker:ignore (ToDO) NPROCESSORS nprocs numstr sysconf
77

88
use clap::{Arg, ArgAction, Command};
99
use std::{env, thread};
@@ -47,7 +47,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4747
// Uses the OpenMP variable to limit the number of threads
4848
// If the parsing fails, returns the max size (so, no impact)
4949
// If OMP_THREAD_LIMIT=0, rejects the value
50-
Ok(threadstr) => match threadstr.parse() {
50+
Ok(threads) => match threads.parse() {
5151
Ok(0) | Err(_) => usize::MAX,
5252
Ok(n) => n,
5353
},
@@ -63,14 +63,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6363
match env::var("OMP_NUM_THREADS") {
6464
// Uses the OpenMP variable to force the number of threads
6565
// If the parsing fails, returns the number of CPU
66-
Ok(threadstr) => {
66+
Ok(threads) => {
6767
// In some cases, OMP_NUM_THREADS can be "x,y,z"
6868
// In this case, only take the first one (like GNU)
6969
// If OMP_NUM_THREADS=0, rejects the value
70-
let thread: Vec<&str> = threadstr.split_terminator(',').collect();
71-
match &thread[..] {
72-
[] => available_parallelism(),
73-
[s, ..] => match s.parse() {
70+
match threads.split_terminator(',').next() {
71+
None => available_parallelism(),
72+
Some(s) => match s.parse() {
7473
Ok(0) | Err(_) => available_parallelism(),
7574
Ok(n) => n,
7675
},

src/uu/split/src/strategy.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl NumberType {
108108
/// # Errors
109109
///
110110
/// If the string is not one of the valid number types,
111-
/// if `K` is not a nonnegative integer,
111+
/// if `K` is not a non-negative integer,
112112
/// or if `K` is 0,
113113
/// or if `N` is not a positive integer,
114114
/// or if `K` is greater than `N`
@@ -117,9 +117,9 @@ impl NumberType {
117117
fn is_invalid_chunk(chunk_number: u64, num_chunks: u64) -> bool {
118118
chunk_number > num_chunks || chunk_number == 0
119119
}
120-
let parts: Vec<&str> = s.split('/').collect();
121-
match &parts[..] {
122-
[n_str] => {
120+
let mut parts = s.splitn(4, '/');
121+
match (parts.next(), parts.next(), parts.next(), parts.next()) {
122+
(Some(n_str), None, None, None) => {
123123
let num_chunks = parse_size_u64(n_str)
124124
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
125125
if num_chunks > 0 {
@@ -128,7 +128,9 @@ impl NumberType {
128128
Err(NumberTypeError::NumberOfChunks(s.to_string()))
129129
}
130130
}
131-
[k_str, n_str] if !k_str.starts_with('l') && !k_str.starts_with('r') => {
131+
(Some(k_str), Some(n_str), None, None)
132+
if !k_str.starts_with('l') && !k_str.starts_with('r') =>
133+
{
132134
let num_chunks = parse_size_u64(n_str)
133135
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
134136
let chunk_number = parse_size_u64(k_str)
@@ -138,12 +140,12 @@ impl NumberType {
138140
}
139141
Ok(Self::KthBytes(chunk_number, num_chunks))
140142
}
141-
["l", n_str] => {
143+
(Some("l"), Some(n_str), None, None) => {
142144
let num_chunks = parse_size_u64(n_str)
143145
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
144146
Ok(Self::Lines(num_chunks))
145147
}
146-
["l", k_str, n_str] => {
148+
(Some("l"), Some(k_str), Some(n_str), None) => {
147149
let num_chunks = parse_size_u64(n_str)
148150
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
149151
let chunk_number = parse_size_u64(k_str)
@@ -153,12 +155,12 @@ impl NumberType {
153155
}
154156
Ok(Self::KthLines(chunk_number, num_chunks))
155157
}
156-
["r", n_str] => {
158+
(Some("r"), Some(n_str), None, None) => {
157159
let num_chunks = parse_size_u64(n_str)
158160
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
159161
Ok(Self::RoundRobin(num_chunks))
160162
}
161-
["r", k_str, n_str] => {
163+
(Some("r"), Some(k_str), Some(n_str), None) => {
162164
let num_chunks = parse_size_u64(n_str)
163165
.map_err(|_| NumberTypeError::NumberOfChunks(n_str.to_string()))?;
164166
let chunk_number = parse_size_u64(k_str)

0 commit comments

Comments
 (0)