Skip to content

Lookup hash fix - #397

Open
wrighton wants to merge 2 commits into
plougher:masterfrom
wrighton:lookup_hash_fix
Open

Lookup hash fix#397
wrighton wants to merge 2 commits into
plougher:masterfrom
wrighton:lookup_hash_fix

Conversation

@wrighton

Copy link
Copy Markdown

Eliminate n^2 runtime (in file count) when building from an archive(s)

Michael Wrighton added 2 commits July 9, 2026 13:34
When building the in-memory directory tree from an archive (mksquashfs -zip /
-tar), add_zipfile()/add_tarfile() call lookup_name() once per path component
of every entry. lookup_name() scanned the directory's entry list (dir->list)
linearly, so populating a directory of K entries cost O(K^2). For archives with
very many files this dominates the whole run and pins a single CPU while the
parallel compressor threads sit idle: a 300k-file zip took ~76s at ~100% CPU,
with compression under 2% of profile samples and ~50% in lookup_name()/strcmp().

Add a per-directory hash table keyed by entry name:
- struct dir_info gains name_hash[]/name_hash_size; struct dir_ent gains
  name_hash_next.
- The table is built lazily once a directory exceeds DIR_HASH_THRESHOLD entries
  (small directories keep the cheaper linear scan) and doubled when the load
  factor exceeds one.
- add_dir_entry() inserts into the hash; lookup_name() consults it when present,
  otherwise falls back to the linear scan.
- free_dir_entry() unlinks the entry from its bucket (reached via
  dir_ent->our_dir) so removals (e.g. prune actions) stay consistent, and
  free_dir() drops the table up front so full-directory teardown skips the
  per-entry unlink.

Lookups become O(1) amortized and directory-tree construction O(K). The 300k
-file zip above now builds in ~3.9s at ~1000% CPU (~20x faster) with identical
output: unsquashfs extraction byte-compared, inode/file counts unchanged, zero
duplicates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant