Skip to content

Commit f438cac

Browse files
oech3cakebaker
authored andcommitted
split: remove buffering
1 parent 3b0bed6 commit f438cac

4 files changed

Lines changed: 21 additions & 31 deletions

File tree

src/uu/split/src/platform/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn instantiate_current_writer(
3131
input: &std::ffi::OsStr,
3232
filename: &std::ffi::OsStr,
3333
is_new: bool,
34-
) -> std::io::Result<std::io::BufWriter<Box<dyn std::io::Write>>> {
34+
) -> std::io::Result<Box<dyn std::io::Write>> {
3535
// Refuse to truncate/overwrite the input. WASI cannot do the fd-based check
3636
// unix/windows use, so this is a best-effort path comparison.
3737
if paths_refer_to_same_file(input, filename) {
@@ -50,9 +50,7 @@ pub fn instantiate_current_writer(
5050
.append(true)
5151
.open(std::path::Path::new(filename))?
5252
};
53-
Ok(std::io::BufWriter::new(
54-
Box::new(file) as Box<dyn std::io::Write>
55-
))
53+
Ok(Box::new(file) as Box<dyn std::io::Write>)
5654
}
5755

5856
#[cfg(unix)]

src/uu/split/src/platform/unix.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// file that was distributed with this source code.
55
use std::env;
66
use std::ffi::{OsStr, OsString};
7-
use std::io::{BufWriter, Error, Result};
7+
use std::io::{Error, Result};
88
use std::io::{ErrorKind, Write};
99
use std::path::Path;
1010
use std::process::{Child, Command, Stdio};
@@ -131,7 +131,7 @@ pub fn instantiate_current_writer(
131131
input: &OsStr,
132132
filename: &OsStr,
133133
is_new: bool,
134-
) -> Result<BufWriter<Box<dyn Write>>> {
134+
) -> Result<Box<dyn Write>> {
135135
match filter {
136136
None => {
137137
let file = if is_new {
@@ -156,12 +156,12 @@ pub fn instantiate_current_writer(
156156

157157
file
158158
};
159-
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))
159+
Ok(Box::new(file) as Box<dyn Write>)
160160
}
161-
Some(filter_command) => Ok(BufWriter::new(Box::new(
161+
Some(filter_command) => Ok(Box::new(
162162
// spawn a shell command and write to it
163163
FilterWriter::new(filter_command, filename)?,
164-
) as Box<dyn Write>)),
164+
) as Box<dyn Write>),
165165
}
166166
}
167167

src/uu/split/src/platform/windows.rs

Lines changed: 3 additions & 3 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
use std::ffi::OsStr;
6-
use std::io::{BufWriter, Error, Result};
6+
use std::io::{Error, Result};
77
use std::io::{ErrorKind, Write};
88
use std::path::Path;
99
use uucore::display::Quotable;
@@ -19,7 +19,7 @@ pub fn instantiate_current_writer(
1919
input: &OsStr,
2020
filename: &OsStr,
2121
is_new: bool,
22-
) -> Result<BufWriter<Box<dyn Write>>> {
22+
) -> Result<Box<dyn Write>> {
2323
let file = if is_new {
2424
create_or_truncate_output_file(input, filename)?
2525
} else {
@@ -41,7 +41,7 @@ pub fn instantiate_current_writer(
4141

4242
file
4343
};
44-
Ok(BufWriter::new(Box::new(file) as Box<dyn Write>))
44+
Ok(Box::new(file) as Box<dyn Write>)
4545
}
4646

4747
fn create_or_truncate_output_file(input: &OsStr, filename: &OsStr) -> Result<std::fs::File> {

src/uu/split/src/split.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::env;
1717
use std::ffi::{OsStr, OsString};
1818
use std::fs::{File, metadata};
1919
use std::io;
20-
use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Read, Seek, SeekFrom, Write, stdin};
20+
use std::io::{BufRead, BufReader, ErrorKind, Read, Seek, SeekFrom, Write, stdin};
2121
use std::path::Path;
2222
use thiserror::Error;
2323
use uucore::display::Quotable;
@@ -543,7 +543,7 @@ impl Settings {
543543
&self,
544544
filename: &OsStr,
545545
is_new: bool,
546-
) -> io::Result<BufWriter<Box<dyn Write>>> {
546+
) -> io::Result<Box<dyn Write>> {
547547
if platform::paths_refer_to_same_file(&self.input, filename) {
548548
return Err(io::Error::other(
549549
translate!("split-error-would-overwrite-input", "file" => filename.quote()),
@@ -710,7 +710,7 @@ struct ByteChunkWriter<'a> {
710710
/// Once the number of bytes written to this writer exceeds
711711
/// `chunk_size`, a new writer is initialized and assigned to this
712712
/// field.
713-
inner: BufWriter<Box<dyn Write>>,
713+
inner: Box<dyn Write>,
714714

715715
/// Iterator that yields filenames for each chunk.
716716
filename_iterator: FilenameIterator<'a>,
@@ -831,7 +831,7 @@ struct LineChunkWriter<'a> {
831831
/// Once the number of lines written to this writer exceeds
832832
/// `chunk_size`, a new writer is initialized and assigned to this
833833
/// field.
834-
inner: BufWriter<Box<dyn Write>>,
834+
inner: Box<dyn Write>,
835835

836836
/// Iterator that yields filenames for each chunk.
837837
filename_iterator: FilenameIterator<'a>,
@@ -854,7 +854,7 @@ impl<'a> LineChunkWriter<'a> {
854854
fn start_new_chunk(
855855
settings: &Settings,
856856
filename_iterator: &mut FilenameIterator,
857-
) -> io::Result<BufWriter<Box<dyn Write>>> {
857+
) -> io::Result<Box<dyn Write>> {
858858
let filename = filename_iterator.next().ok_or_else(|| {
859859
io::Error::other(translate!("split-error-output-file-suffixes-exhausted"))
860860
})?;
@@ -919,7 +919,7 @@ impl Write for LineChunkWriter<'_> {
919919
/// Output file parameters
920920
struct OutFile {
921921
filename: OsString,
922-
maybe_writer: Option<BufWriter<Box<dyn Write>>>,
922+
maybe_writer: Option<Box<dyn Write>>,
923923
is_new: bool,
924924
}
925925

@@ -932,7 +932,7 @@ trait ManageOutFiles {
932932
&mut self,
933933
idx: usize,
934934
settings: &Settings,
935-
) -> UResult<&mut BufWriter<Box<dyn Write>>>;
935+
) -> UResult<&mut Box<dyn Write>>;
936936
/// Initialize a new set of output files
937937
/// Each [`OutFile`] is generated with filename, while the writer for it could be
938938
/// optional, to be instantiated later by the calling function as needed.
@@ -951,11 +951,7 @@ trait ManageOutFiles {
951951
/// are flagged as `is_new=false`, so they can be re-opened for appending
952952
/// instead of created anew if we need to keep writing into them later,
953953
/// i.e. in case of round robin distribution as in [`n_chunks_by_line_round_robin`]
954-
fn get_writer(
955-
&mut self,
956-
idx: usize,
957-
settings: &Settings,
958-
) -> UResult<&mut BufWriter<Box<dyn Write>>>;
954+
fn get_writer(&mut self, idx: usize, settings: &Settings) -> UResult<&mut Box<dyn Write>>;
959955
}
960956

961957
impl ManageOutFiles for OutFiles {
@@ -1001,7 +997,7 @@ impl ManageOutFiles for OutFiles {
1001997
&mut self,
1002998
idx: usize,
1003999
settings: &Settings,
1004-
) -> UResult<&mut BufWriter<Box<dyn Write>>> {
1000+
) -> UResult<&mut Box<dyn Write>> {
10051001
let mut count = 0;
10061002
// Use-case for doing multiple tries of closing fds:
10071003
// E.g. split running in parallel to other processes (e.g. another split) doing similar stuff,
@@ -1048,11 +1044,7 @@ impl ManageOutFiles for OutFiles {
10481044
}
10491045
}
10501046

1051-
fn get_writer(
1052-
&mut self,
1053-
idx: usize,
1054-
settings: &Settings,
1055-
) -> UResult<&mut BufWriter<Box<dyn Write>>> {
1047+
fn get_writer(&mut self, idx: usize, settings: &Settings) -> UResult<&mut Box<dyn Write>> {
10561048
if self[idx].maybe_writer.is_some() {
10571049
Ok(self[idx].maybe_writer.as_mut().unwrap())
10581050
} else {
@@ -1472,7 +1464,7 @@ where
14721464
// to be overwritten for sure at the beginning of the loop below
14731465
// because we start with `remaining == 0`, indicating that a new
14741466
// chunk should start.
1475-
let mut writer: BufWriter<Box<dyn Write>> = BufWriter::new(Box::new(io::Cursor::new(vec![])));
1467+
let mut writer: Box<dyn Write> = Box::new(io::Cursor::new(vec![]));
14761468

14771469
let mut remaining = 0;
14781470
for line in lines_with_sep(reader, settings.separator) {

0 commit comments

Comments
 (0)