Skip to content

Commit 0d02663

Browse files
committed
uniq: merge per-line writes and grow input buffer
write_line() issued two separate write_all() calls per output line (line content, then the terminator byte), each going through the dynamically-dispatched Box<dyn Write> from open_output_file(). Merge them into a single write via a reused scratch buffer. Also match the input BufReader's capacity to the existing 128KB output buffer (previously the 8KB std default), for consistency. Measured on a 20x-repeated /usr/share/dict/words (~80MB, pinned to one CPU core to reduce noise): -w 1 dropped from 429.7ms to 395.5ms (~8%), -w 512 from 578.9ms to 497.8ms (~14%).
1 parent 279528f commit 0d02663

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

‎src/uu/uniq/src/uniq.rs‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl Uniq {
8888

8989
let mut next_buf = Vec::with_capacity(1024);
9090
let mut next_meta = LineMeta::default();
91+
let mut line_out = Vec::with_capacity(1024);
9192

9293
loop {
9394
if !Self::read_line(&mut reader, &mut next_buf, line_terminator)? {
@@ -98,7 +99,13 @@ impl Uniq {
9899

99100
if self.keys_are_equal(&current_buf, &current_meta, &next_buf, &next_meta) {
100101
if self.all_repeated {
101-
self.write_line(writer, &current_buf, group_count, first_line_printed)?;
102+
self.write_line(
103+
writer,
104+
&mut line_out,
105+
&current_buf,
106+
group_count,
107+
first_line_printed,
108+
)?;
102109
first_line_printed = true;
103110
std::mem::swap(&mut current_buf, &mut next_buf);
104111
std::mem::swap(&mut current_meta, &mut next_meta);
@@ -108,7 +115,13 @@ impl Uniq {
108115
if (group_count == 1 && !self.repeats_only)
109116
|| (group_count > 1 && !self.uniques_only)
110117
{
111-
self.write_line(writer, &current_buf, group_count, first_line_printed)?;
118+
self.write_line(
119+
writer,
120+
&mut line_out,
121+
&current_buf,
122+
group_count,
123+
first_line_printed,
124+
)?;
112125
first_line_printed = true;
113126
}
114127
std::mem::swap(&mut current_buf, &mut next_buf);
@@ -119,7 +132,13 @@ impl Uniq {
119132
}
120133

121134
if (group_count == 1 && !self.repeats_only) || (group_count > 1 && !self.uniques_only) {
122-
self.write_line(writer, &current_buf, group_count, first_line_printed)?;
135+
self.write_line(
136+
writer,
137+
&mut line_out,
138+
&current_buf,
139+
group_count,
140+
first_line_printed,
141+
)?;
123142
first_line_printed = true;
124143
}
125144
if (self.delimiters == Delimiters::Append || self.delimiters == Delimiters::Both)
@@ -265,6 +284,7 @@ impl Uniq {
265284
fn write_line(
266285
&self,
267286
writer: &mut impl Write,
287+
line_out: &mut Vec<u8>,
268288
line: &[u8],
269289
count: usize,
270290
first_line_printed: bool,
@@ -275,21 +295,20 @@ impl Uniq {
275295
write_line_terminator!(writer, line_terminator)?;
276296
}
277297

278-
let mut count_buf = [0u8; Self::COUNT_PREFIX_BUF_SIZE];
298+
line_out.clear();
279299

280300
if self.show_counts {
281-
// Call the associated function (no &self) after the refactor above.
301+
let mut count_buf = [0u8; Self::COUNT_PREFIX_BUF_SIZE];
282302
let prefix = Self::build_count_prefix(count, &mut count_buf);
283-
writer
284-
.write_all(prefix)
285-
.map_err_context(|| translate!("uniq-error-write-error"))?;
303+
line_out.extend_from_slice(prefix);
286304
}
287305

288-
writer
289-
.write_all(line)
290-
.map_err_context(|| translate!("uniq-error-write-error"))?;
306+
line_out.extend_from_slice(line);
307+
line_out.push(line_terminator);
291308

292-
write_line_terminator!(writer, line_terminator)
309+
writer
310+
.write_all(line_out)
311+
.map_err_context(|| translate!("uniq-error-write-error"))
293312
}
294313

295314
const COUNT_PREFIX_WIDTH: usize = 7;
@@ -827,7 +846,7 @@ fn open_input_file(in_file_name: Option<&OsStr>) -> UResult<Box<dyn BufRead>> {
827846
let in_file = File::open(path).map_err_context(
828847
|| translate!("uniq-error-could-not-open", "path" => path.maybe_quote()),
829848
)?;
830-
Box::new(BufReader::new(in_file))
849+
Box::new(BufReader::with_capacity(OUTPUT_BUFFER_CAPACITY, in_file))
831850
}
832851
_ => Box::new(stdin().lock()),
833852
})

0 commit comments

Comments
 (0)