Skip to content

Commit be9285f

Browse files
committed
Optimize unix::Client::new: Avoid alloc
for machines with less than or equal to 128 cores. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent e1e1ae5 commit be9285f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/unix.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@ pub struct Acquired {
2323
impl Client {
2424
pub fn new(limit: usize) -> io::Result<Client> {
2525
let client = unsafe { Client::mk()? };
26+
2627
// I don't think the character written here matters, but I could be
2728
// wrong!
28-
let v: Vec<u8> = vec![b'|'; limit];
29-
(&client.write).write_all(&v)?;
29+
//
30+
// Also, it is highly unlikely to have a machine with more than
31+
// 128 cores.
32+
const BUFFER: [u8; 128] = [b'|'; 128];
33+
34+
if BUFFER.len() <= limit {
35+
(&client.write).write_all(&BUFFER[..limit])?;
36+
} else {
37+
(&client.write).write_all(&vec![b'|'; limit])?;
38+
}
39+
3040
Ok(client)
3141
}
3242

0 commit comments

Comments
 (0)