Skip to content

Commit

Permalink
Fix: Only create large stack size if enough memory
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bootandy committed Aug 31, 2022
1 parent c148cd9 commit c363e5f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
41 changes: 40 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "du-dust"
description = "A more intuitive version of du"
version = "0.8.2"
version = "0.8.3"
authors = ["bootandy <bootandy@gmail.com>", "nebkor <code@ardent.nebcorp.com>"]
edition = "2021"
readme = "README.md"
Expand Down Expand Up @@ -39,6 +39,7 @@ regex = "1"
config-file = "0.2"
serde = { version = "1.0", features = ["derive"] }
directories = "4"
sysinfo = "0.15"

[target.'cfg(windows)'.dependencies]
winapi-util = "0.1"
Expand Down
24 changes: 19 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ mod utils;
use crate::cli::build_cli;
use std::collections::HashSet;
use std::process;
use sysinfo::{System, SystemExt};

use self::display::draw_it;
use clap::Values;
use config::get_config;
use dir_walker::{walk_it, WalkData};
use filter::get_biggest;
use filter_type::get_all_file_types;
use rayon::ThreadPoolBuildError;
use regex::Regex;
use std::cmp::max;
use std::path::PathBuf;
Expand Down Expand Up @@ -154,11 +156,7 @@ fn main() {
by_filecount,
ignore_hidden: config.get_ignore_hidden(&options),
};
// Larger stack size to handle cases with lots of nested directories
rayon::ThreadPoolBuilder::new()
.stack_size(usize::pow(1024, 3))
.build_global()
.unwrap();
let _rayon = init_rayon();

let iso = config.get_iso(&options);
let (top_level_nodes, has_errors) = walk_it(simplified_dirs, walk_data);
Expand Down Expand Up @@ -191,3 +189,19 @@ fn main() {
)
}
}

fn init_rayon() -> Result<(), ThreadPoolBuildError> {
let large_stack = usize::pow(1024, 3);
// Warning: Creating System is slow, takes ~ 100ms
let s = System::new();
let available = s.get_available_memory() * 1024;

if available > large_stack.try_into().unwrap() {
// Larger stack size to handle cases with lots of nested directories
rayon::ThreadPoolBuilder::new()
.stack_size(large_stack)
.build_global()
} else {
Ok(())
}
}

0 comments on commit c363e5f

Please sign in to comment.