Repository navigation
Description
A directory in fat is a Directory: its clusters in memory and an index
of itself (cf. 1). The volume keeps the directories it read in an LRU
cache. There are four problems with them on main, and fixing them
touches the same code, so let's do it in one series.
(1) The bound of the cache leaves out the index.
DirectoryCache adds up data_len(), the bytes of every directory it
holds, and evicts once that's over 8 MiB (cf. 2, 3). The index of
each directory isn't counted. It keeps a copy of every entry, 72 bytes
on 64-bit plus its long name, in a BTreeMap, and for every folded name
of an entry, long and short, a Vec key with a Vec of slots
in a HashMap, each with a heap allocation of its own (cf. 4, 5).
Estimated from the types, that's a few hundred bytes per entry, for 32
bytes per slot on disk. The comment on the bound says the index is "a
few times that for long names" (cf. 2). With 8.3 names it's more.
Measured on #19 before the merge, release build: assembling empty files
in directories of 1000 from a manifest through a pipe peaked at 93.9,
94.0 and 94.7 MiB at 0.5, 2 and 4 million records. So the cache is
bounded, just not by 8 MiB. That wasn't measured again on main, where
the cache and the index work the same.
(2) Every operation walks from the root.
walk_with() starts at the root and takes every directory on the path
through the cache (cf. 6), and the assembly hands the volume a full
path for every directory and file it makes (cf. 7, 8), as populate
does. So every file touches every directory above it, and the cache
sees the same cycle over and over. Once the directories on one path
hold more than the bound together, LRU always evicts the directory
that's needed next, and every file reads all of them from disk again
and rebuilds their index. Found by reading the code, not measured on
main.
(3) Alias tails are searched again and again.
A long name that needs a numeric tail gets the smallest free one, as
mtools and Windows pick it. For every shape of tail the index keeps a
hint below which all tails are taken, so that a pick doesn't try them
again (cf. 9). But a directory read from disk starts without hints,
since they're only learned from picks (cf. 10). A replacement doesn't
record the hint it found (cf. 11). And taking the old entry away
lowers the hint to its tail (cf. 12), which is the tail the
replacement just took.
So populate --replace, or an assembly with replace, over a directory
that holds the files already searches from ~1 for every file it
replaces, and a new file after a replacement searches the taken tails
above it again. Both are quadratic in the number of names that share an
alias. Found by reading the code, not measured on main.
(4) Two files open in one directory can get the same alias.
begin_file() picks the alias and reserves the slots, and the entry is
written only when the file is finished (cf. 13). Until then the index
doesn't know that the tail is taken. So a second begin_file() for a
name with an alias of the same shape picks it again, and publish() only
checks the long name (cf. 14). The directory then holds two entries
with the same short name, which fsck reports as a duplicate name
(cf. 15). ufat's own commands write one file at a time, so it takes
the library API to get there. Found by reading the code, not reproduced
on main.
The plan:
- Count what the cache holds: the bytes and everything the index keeps,
the tail hints and the reservations included. - A lean index. The slots stay the only copy of the entries, and an
entry is parsed from them when it's needed. For every folded name the
index keeps a hash, keyed at random per directory, and the slot of
the entry. A lookup parses the entries at the slots of a hash and
compares the names. The free runs, the end position, the reservations
and the tail hints stay. The table can be narrow: a slot fits into 16
bits (cf. 16), and a hash that every hit is checked against doesn't
need 64. Two maximal directories should fit into the bound, so that a
directory being filled stays cached below a parent as large. - A cursor. ufat takes manifests in code point order only (cf. 17),
which lists the subtree of a directory right after it, and populate
walks the tree depth first (cf. 18). So both can keep the
directories of the path they're in open and touch only the one a file
goes into. - Tails that are searched once. For every shape the index keeps where
the search stopped, for replacements and for directories read from
disk alike, and the tails freed below that. A pick takes the smallest
freed tail, or for a replacement its own tail when that's smaller,
and otherwise goes on from where the search stopped. - The alias of an open file counts as taken until the file is finished
or aborted, like its reserved slots. - While at it, set_times() takes the dot entries from slots 0 and 1
instead of going through every entry of the directory (cf. 19).
Note that the benchmark in #48 can only show memory that
stays flat once (1) is done, and that the lean index changes what
check_index() has to compare (#44).
Activity