Skip to content

Commit 8abfc60

Browse files
committed
Optimize unix::Client::new: Avoid any alloc
By issuing multiple `write_all`. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent be9285f commit 8abfc60

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/unix.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct Acquired {
2121
}
2222

2323
impl Client {
24-
pub fn new(limit: usize) -> io::Result<Client> {
24+
pub fn new(mut limit: usize) -> io::Result<Client> {
2525
let client = unsafe { Client::mk()? };
2626

2727
// I don't think the character written here matters, but I could be
@@ -31,10 +31,13 @@ impl Client {
3131
// 128 cores.
3232
const BUFFER: [u8; 128] = [b'|'; 128];
3333

34-
if BUFFER.len() <= limit {
34+
while limit >= BUFFER.len() {
35+
(&client.write).write_all(&BUFFER)?;
36+
limit -= BUFFER.len();
37+
}
38+
39+
if limit > 0 {
3540
(&client.write).write_all(&BUFFER[..limit])?;
36-
} else {
37-
(&client.write).write_all(&vec![b'|'; limit])?;
3841
}
3942

4043
Ok(client)

0 commit comments

Comments
 (0)