tree: a node is 88 bytes instead of 120

A home directory of 2.6 million directories and 17 million files is
19.7 million nodes, so every field of Node is paid that many times: its
120 bytes were 2.36 GB of the scan's 3.27 GB peak.

- own_bytes and own_files are no longer stored. After aggregate a leaf's
  own figures always equal its totals, and only the selection line reads
  a directory's, so Node::own_bytes() and own_files() sum the direct
  children when asked. A leaf's bytes is now the measured size and the
  only input; de-duplication zeroes it for a second name, as it zeroed
  own_bytes before.
- dirs is a u32, which packs beside the one-byte fields.
- inode is Option<(u64, NonZeroU64)>: no file has inode or file record
  zero, so None fits in the niche and the pair costs 16 bytes, not 24.
  Node::identity() gives the plain pair.

On the same home directory, peak memory for a scan falls from 3,267 MB
to 2,588 MB and node storage from 2,359 MB to 1,731 MB. Trees are
identical to upstream's with de-duplication off (1.25 million nodes
across ~/src, /Applications and /System/Applications, both size modes),
and the totals are identical with it on. a_node_stays_small holds the
size on 64-bit targets.
This commit is contained in:
Joshua Gosse
2026-09-28 09:38:59 -04:00
parent fdc2eb330d
commit 48fc31af48
5 changed files with 94 additions and 66 deletions
+7 -4
View File
@@ -73,10 +73,13 @@ and `cargo build --release` directly; CI runs the gate on both systems.
allocation, alternate data streams included since they go when the file
goes, so for a file with alternate streams it can exceed the walk's
number.
2. **`own_bytes`/`own_files` are derived, never tracked.** `tree::aggregate`
computes the totals from the children. Hardlink de-duplication zeroes a
duplicate leaf's weight while that pass runs; anything that patches
`bytes` directly will be overwritten.
2. **Totals are derived, never tracked.** A leaf's `bytes` is the only
input: `tree::aggregate` computes every directory's `bytes`, `files` and
`dirs` from its children, and `own_bytes()`/`own_files()` read the direct
children when asked. Hardlink de-duplication zeroes a duplicate leaf's
`bytes` while that pass runs; anything that patches a directory's `bytes`
directly will be overwritten. `Node` is paid for tens of millions of
times, so `a_node_stays_small` holds its size.
3. **A directory is only built when its own scan *and* every subdirectory task
has finished.** That is the `+1` sentinel in `PendingDir::pending`. Building
early silently drops whole subtrees — it has happened once.
+4 -3
View File
@@ -1009,7 +1009,7 @@ fn scan_totals(app: &Disktree, theme: &Theme) -> Div {
let tree = app.tree();
let bytes = tree.map_or(app.progress.bytes, |node| node.bytes);
let files = tree.map_or(app.progress.files, |node| node.files);
let dirs = tree.map_or(app.progress.dirs, |node| node.dirs);
let dirs = tree.map_or(app.progress.dirs, |node| u64::from(node.dirs));
let errors = app.progress.errors;
div()
.flex()
@@ -3205,9 +3205,10 @@ fn node_card(
"{} files · {} dirs · {} direct",
widgets::human_count(node.files),
widgets::human_count(
node.dirs.saturating_sub(u64::from(node.is_dir()))
u64::from(node.dirs)
.saturating_sub(u64::from(node.is_dir()))
),
human_bytes(node.own_bytes)
human_bytes(node.own_bytes())
)),
);
+4 -2
View File
@@ -20,6 +20,7 @@
use std::fs::{File, OpenOptions};
use std::io;
use std::num::NonZeroU64;
use std::os::windows::fs::{FileExt as _, OpenOptionsExt as _};
use std::path::Path;
@@ -1145,7 +1146,8 @@ impl Table<'_> {
let mut node = Node::entry(name, kind, size);
node.modified = info.modified;
if info.names > 1 {
node.inode = Some((0, u64::from(entry.child)));
node.inode = NonZeroU64::new(u64::from(entry.child))
.map(|record| (0, record));
}
Ok(Some(node))
}
@@ -1685,7 +1687,7 @@ mod tests {
assert_eq!(found, expected, "include_hidden {include_hidden}");
// Only a file with a second name needs its identity kept.
let inode = |name: &str| root.child_named(name).unwrap().inode;
let inode = |name: &str| root.child_named(name).unwrap().identity();
assert_eq!(inode("linked.txt"), Some((0, 31)));
assert_eq!(inode("visible.txt"), None);
}
+12 -11
View File
@@ -19,6 +19,7 @@
use std::fs::DirEntry;
use std::fs::{self, Metadata};
use std::io;
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
use std::sync::mpsc::{self, Receiver, TryRecvError};
@@ -774,18 +775,16 @@ impl PendingDir {
/// Turn a completed directory into a node. Only called when `pending` has
/// reached zero, so every child is already in `self.partial`.
///
/// `bytes` and `own_bytes` are left at zero on purpose: the walk cannot
/// know the aggregate, and [`crate::tree::aggregate`] derives both from the
/// children once every child is present.
/// `bytes` is left at zero on purpose: the walk cannot know the
/// aggregate, and [`crate::tree::aggregate`] derives it from the children
/// once every child is present.
fn build(&self) -> Node {
let partial = std::mem::take(&mut *lock(&self.partial));
Node {
name: partial.name,
kind: NodeKind::Directory,
bytes: 0,
own_bytes: 0,
files: 0,
own_files: 0,
dirs: 1,
inode: None,
read_error: self.read_error.load(Ordering::Relaxed),
@@ -842,7 +841,7 @@ fn scan_blocking(root: &Path, context: &Arc<WalkContext>) -> io::Result<Node> {
let node = finish_tree(node, &context.options);
// The reader counted every file on the volume; the tree may
// hold fewer.
progress.settle(node.files, node.dirs, node.bytes);
progress.settle(node.files, u64::from(node.dirs), node.bytes);
return Ok(node);
}
// The reader gave up, or never started: the walk counts from nothing.
@@ -920,7 +919,7 @@ impl Tally {
if node.is_dir() {
// A subtree taken whole from an earlier scan.
self.files += node.files;
self.dirs += node.dirs;
self.dirs += node.dirs as u64;
} else {
// A link counts as a file on the meter, as it always has,
// though the tree does not count it.
@@ -1065,7 +1064,9 @@ fn leaf_node(
) -> Node {
let mut node = Node::entry(name, kind, facts.size);
if track {
node.inode = facts.identity;
node.inode = facts
.identity
.and_then(|(device, file)| Some((device, NonZeroU64::new(file)?)));
}
node.modified = facts.modified;
node
@@ -1288,8 +1289,8 @@ mod tests {
write(root, "sub/deep.bin", 300);
let tree = scan_dir(root, &options());
assert_eq!(tree.own_bytes, 700);
assert_eq!(tree.own_files, 1);
assert_eq!(tree.own_bytes(), 700);
assert_eq!(tree.own_files(), 1);
assert_eq!(tree.bytes, 1000);
assert_eq!(tree.files, 2);
}
@@ -1448,7 +1449,7 @@ mod tests {
..options()
},
);
assert_eq!(tree.own_bytes, 10);
assert_eq!(tree.own_bytes(), 10);
let a = child(&tree, "a");
assert_eq!(
a.bytes, 20,
+67 -46
View File
@@ -1,5 +1,6 @@
//! The scanned tree.
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{LazyLock, Mutex, OnceLock};
@@ -54,28 +55,26 @@ impl Metric {
/// One entry in the scanned tree.
///
/// Totals and direct figures are both kept: `own_bytes` and `own_files` are
/// what sits directly in a directory, `bytes` and `files` are the subtree
/// totals the treemap draws. The selection line needs both, and keeping them means
/// no second traversal when one of them is displayed.
/// `bytes` and `files` are the subtree totals the treemap draws; what sits
/// directly in a directory is [`Self::own_bytes`] and [`Self::own_files`],
/// read from its children when the selection line asks. A disk holds tens
/// of millions of nodes, so every field is paid that many times: the size
/// is held by a test.
#[derive(Clone, Debug)]
pub struct Node {
pub name: Box<str>,
pub kind: NodeKind,
/// Subtree total: direct contents plus every descendant.
/// Subtree total: direct contents plus every descendant. A file's own
/// size, or nothing for a second name of a file already charged.
pub bytes: u64,
/// Bytes of the leaf entries directly in this directory, or this file's
/// own size. Derived by [`aggregate`].
pub own_bytes: u64,
/// Files at or beneath this node; `1` for a file.
pub files: u64,
/// Files directly in this directory; `1` for a file. Derived by
/// [`aggregate`].
pub own_files: u64,
/// Directories at or beneath this node; `1` for a directory.
pub dirs: u64,
/// `(device, inode)` for files, used to de-duplicate hardlinks.
pub inode: Option<(u64, u64)>,
/// Directories at or beneath this node; `1` for a directory. Four
/// billion is more than any volume holds.
pub dirs: u32,
/// `(device, inode)` for files, used to de-duplicate hardlinks. No file
/// has inode zero, which lets `None` cost nothing.
pub inode: Option<(u64, NonZeroU64)>,
/// The directory could not be read; its contents are unknown.
pub read_error: bool,
/// Newest write time at or beneath this node, in Unix seconds; `0` when
@@ -101,9 +100,7 @@ impl Node {
name: name.into(),
kind: NodeKind::Directory,
bytes: 0,
own_bytes: 0,
files: 0,
own_files: 0,
dirs: 1,
inode: None,
read_error: false,
@@ -124,9 +121,7 @@ impl Node {
name: name.into(),
kind,
bytes,
own_bytes: bytes,
files: u64::from(kind == NodeKind::File),
own_files: u64::from(kind == NodeKind::File),
dirs: 0,
inode: None,
read_error: false,
@@ -141,6 +136,35 @@ impl Node {
self.kind.is_dir()
}
/// Bytes of the leaf entries directly in this directory, or this file's
/// own size.
pub fn own_bytes(&self) -> u64 {
if !self.is_dir() {
return self.bytes;
}
self.children
.iter()
.filter(|child| !child.is_dir())
.fold(0, |sum, child| child.bytes.saturating_add(sum))
}
/// Files directly in this directory; `1` for a file.
pub fn own_files(&self) -> u64 {
if !self.is_dir() {
return self.files;
}
self.children
.iter()
.filter(|child| !child.is_dir())
.map(|child| child.files)
.sum()
}
/// [`Self::inode`] as the plain pair a platform reports.
pub fn identity(&self) -> Option<(u64, u64)> {
self.inode.map(|(device, file)| (device, file.get()))
}
/// The value a treemap should weight this node by.
pub const fn value(&self, metric: Metric) -> u64 {
match metric {
@@ -231,14 +255,14 @@ impl Node {
}
}
/// Recompute `bytes`, `files`, `dirs` and the direct totals bottom-up, then
/// order children by `metric`, largest first.
/// Recompute `bytes`, `files` and `dirs` bottom-up, then order children by
/// `metric`, largest first.
///
/// `bytes` and `files` are the subtree totals; `own_bytes` and `own_files` are
/// the direct contents, derived from the leaf children rather than tracked
/// separately. Deriving them is what keeps the two consistent: hardlink
/// de-duplication rewrites a leaf's weight, and every total above it —
/// including its parent's "direct" figure — follows without a second pass.
/// A leaf's `bytes` is its measured size and the only input; every total is
/// derived from the leaves, as are the direct figures [`Node::own_bytes`]
/// and [`Node::own_files`]. Deriving them is what keeps them consistent:
/// hardlink de-duplication rewrites a leaf's weight, and every total above
/// it follows without a second pass.
pub fn aggregate(node: &mut Node, metric: Metric) {
aggregate_at(node, metric, 0, None);
}
@@ -331,24 +355,19 @@ fn aggregate_at(
if !node.is_dir() {
// Every name of a file has the file's size, so one that weighs
// nothing need not be remembered to be charged once.
if node.own_bytes > 0
if node.bytes > 0
&& let Some(seen) = seen
&& let Some(key) = node.inode
&& let Some(key) = node.identity()
&& !seen.insert(key)
{
node.own_bytes = 0;
node.bytes = 0;
}
node.bytes = node.own_bytes;
node.files = node.own_files;
node.dirs = 0;
return;
}
let mut bytes = 0;
let mut files = 0;
let mut own_bytes = 0;
let mut own_files = 0;
let mut dirs: u64 = 1;
let mut dirs: u32 = 1;
let mut modified = 0;
if depth < PARALLEL_LEVELS {
node.children
@@ -365,15 +384,9 @@ fn aggregate_at(
bytes = child.bytes.saturating_add(bytes);
files += child.files;
dirs += child.dirs;
if !child.is_dir() {
own_bytes = child.bytes.saturating_add(own_bytes);
own_files += child.files;
}
}
node.bytes = bytes;
node.files = files;
node.own_bytes = own_bytes;
node.own_files = own_files;
node.dirs = dirs;
node.modified = modified;
@@ -414,6 +427,14 @@ mod tests {
Node::entry(name, NodeKind::File, bytes)
}
/// A home directory is tens of millions of nodes: every byte here is
/// tens of megabytes there.
#[test]
#[cfg(target_pointer_width = "64")]
fn a_node_stays_small() {
assert_eq!(size_of::<Node>(), 88);
}
#[test]
fn seen_charges_an_identity_once_in_either_store() {
let seen = Seen::new();
@@ -443,7 +464,7 @@ mod tests {
("newer again", 8192, newer),
] {
let mut file = leaf(name, bytes);
file.inode = Some(key);
file.inode = Some((key.0, NonZeroU64::new(key.1).unwrap()));
root.children.push(file);
}
aggregate_deduped(&mut root, Metric::Bytes, &Seen::new());
@@ -460,12 +481,12 @@ mod tests {
root.children.push(leaf("small", 9));
aggregate(&mut root, Metric::Bytes);
assert_eq!(root.own_bytes, 5 + 9, "direct leaves only");
assert_eq!(root.own_bytes(), 5 + 9, "direct leaves only");
assert_eq!(root.bytes, 5 + 9 + 7);
assert_eq!(root.files, 3);
assert_eq!(root.own_files, 2);
assert_eq!(root.own_files(), 2);
assert_eq!(root.dirs, 2);
assert_eq!(child_named(&root, "child").own_bytes, 7);
assert_eq!(child_named(&root, "child").own_bytes(), 7);
assert_eq!(&*root.children[0].name, "small", "9 bytes, largest first");
assert_eq!(&*root.children[2].name, "direct");
@@ -473,7 +494,7 @@ mod tests {
let mut single = leaf("solo", 3);
aggregate(&mut single, Metric::Bytes);
assert_eq!(single.bytes, 3);
assert_eq!(single.own_bytes, 3);
assert_eq!(single.own_bytes(), 3);
assert_eq!(single.files, 1);
assert_eq!(single.dirs, 0);
}