Skip to content

Commit c363e5f

Browse files
committed
Fix: Only create large stack size if enough memory
Small boxes do not have enough memory to create a large stack Conversely we want a large stack size for large boxes with a very highly nested directory structure. Version: New version
1 parent c148cd9 commit c363e5f

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

Cargo.lock

+40-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "du-dust"
33
description = "A more intuitive version of du"
4-
version = "0.8.2"
4+
version = "0.8.3"
55
authors = ["bootandy <bootandy@gmail.com>", "nebkor <code@ardent.nebcorp.com>"]
66
edition = "2021"
77
readme = "README.md"
@@ -39,6 +39,7 @@ regex = "1"
3939
config-file = "0.2"
4040
serde = { version = "1.0", features = ["derive"] }
4141
directories = "4"
42+
sysinfo = "0.15"
4243

4344
[target.'cfg(windows)'.dependencies]
4445
winapi-util = "0.1"

src/main.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ mod utils;
1212
use crate::cli::build_cli;
1313
use std::collections::HashSet;
1414
use std::process;
15+
use sysinfo::{System, SystemExt};
1516

1617
use self::display::draw_it;
1718
use clap::Values;
1819
use config::get_config;
1920
use dir_walker::{walk_it, WalkData};
2021
use filter::get_biggest;
2122
use filter_type::get_all_file_types;
23+
use rayon::ThreadPoolBuildError;
2224
use regex::Regex;
2325
use std::cmp::max;
2426
use std::path::PathBuf;
@@ -154,11 +156,7 @@ fn main() {
154156
by_filecount,
155157
ignore_hidden: config.get_ignore_hidden(&options),
156158
};
157-
// Larger stack size to handle cases with lots of nested directories
158-
rayon::ThreadPoolBuilder::new()
159-
.stack_size(usize::pow(1024, 3))
160-
.build_global()
161-
.unwrap();
159+
let _rayon = init_rayon();
162160

163161
let iso = config.get_iso(&options);
164162
let (top_level_nodes, has_errors) = walk_it(simplified_dirs, walk_data);
@@ -191,3 +189,19 @@ fn main() {
191189
)
192190
}
193191
}
192+
193+
fn init_rayon() -> Result<(), ThreadPoolBuildError> {
194+
let large_stack = usize::pow(1024, 3);
195+
// Warning: Creating System is slow, takes ~ 100ms
196+
let s = System::new();
197+
let available = s.get_available_memory() * 1024;
198+
199+
if available > large_stack.try_into().unwrap() {
200+
// Larger stack size to handle cases with lots of nested directories
201+
rayon::ThreadPoolBuilder::new()
202+
.stack_size(large_stack)
203+
.build_global()
204+
} else {
205+
Ok(())
206+
}
207+
}

0 commit comments

Comments
 (0)