Skip to content

Commit 61bd00a

Browse files
committed
refactor: Set max output buffer size in Config
1 parent b62eb09 commit 61bd00a

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ pub struct Config {
7171
/// `max_buffer_time`.
7272
pub max_buffer_time: Option<Duration>,
7373

74+
/// Maximum size of the output buffer before flushing results to the console.
75+
pub max_buffer_size: usize,
76+
7477
/// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines
7578
/// how to style different filetypes.
7679
pub ls_colors: Option<LsColors>,

src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::filetypes::FileTypes;
3434
use crate::filter::OwnerFilter;
3535
use crate::filter::TimeFilter;
3636
use crate::regex_helper::{pattern_has_uppercase_char, pattern_matches_strings_with_leading_dot};
37+
use crate::walk::DEFAULT_MAX_BUFFER_LENGTH;
3738

3839
// We use jemalloc for performance reasons, see https://github.com/sharkdp/fd/pull/481
3940
// FIXME: re-enable jemalloc on macOS, see comment in Cargo.toml file for more infos
@@ -275,10 +276,15 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
275276
prune: opts.prune,
276277
threads: opts.threads().get(),
277278
max_buffer_time: match opts.sort {
278-
// If sorting is enabled, the set max_buffer_time to practically infinity.
279-
Some(_) => Some(Duration::from_secs(u64::MAX / 2)),
279+
// If sorting is enabled, then set max_buffer_time to practically infinity.
280+
Some(_) => Some(Duration::from_hours(24 * 365 * 10)), // 10 years - arbitrarily-large.
280281
None => opts.max_buffer_time,
281282
},
283+
max_buffer_size: match opts.sort {
284+
// If sorting is enabled, allow a practically infinite buffer size.
285+
Some(_) => usize::MAX,
286+
None => DEFAULT_MAX_BUFFER_LENGTH,
287+
},
282288
ls_colors,
283289
hyperlink,
284290
interactive_terminal,

src/walk.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ impl BatchSender {
122122
}
123123

124124
/// Maximum size of the output buffer before flushing results to the console
125-
const MAX_BUFFER_LENGTH: usize = 1000;
125+
pub const DEFAULT_MAX_BUFFER_LENGTH: usize = 1000;
126+
126127
/// Default duration until output buffering switches to streaming.
127128
const DEFAULT_MAX_BUFFER_TIME: Duration = Duration::from_millis(100);
128129

@@ -165,7 +166,7 @@ impl<'a, W: Write> ReceiverBuffer<'a, W> {
165166
stdout,
166167
mode: ReceiverMode::Buffering,
167168
deadline,
168-
buffer: Vec::with_capacity(MAX_BUFFER_LENGTH),
169+
buffer: Vec::with_capacity(config.max_buffer_size.min(10_000)),
169170
num_results: 0,
170171
}
171172
}
@@ -208,7 +209,7 @@ impl<'a, W: Write> ReceiverBuffer<'a, W> {
208209
match self.mode {
209210
ReceiverMode::Buffering => {
210211
self.buffer.push(dir_entry);
211-
if self.buffer.len() > MAX_BUFFER_LENGTH {
212+
if self.buffer.len() > self.config.max_buffer_size {
212213
self.stream()?;
213214
}
214215
}

0 commit comments

Comments
 (0)