Skip to content

squashfs: readMetadata improve memory usage allocate only size bytes instead of whole block - #461

Merged
deitch merged 1 commit into
diskfs:masterfrom
sap-nocops:optimize_walk
Nov 23, 2026
Merged

deitch merged 1 commit into
diskfs:masterfrom
sap-nocops:optimize_walk

Conversation

@sap-nocops

@sap-nocops sap-nocops commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Benchmarks

readMetadata used to copy from the requested offset to the end of the metadata block, up to 8 KiB, even when
the caller needed a few dozen bytes, e.g. for each inode of a directory being listed. It now copies exactly size
bytes.

Apple M4 Max, benchstat of 8 runs (ReadDir) and 6 runs (WalkImage).

BenchmarkReadDir: ReadDir of a directory nested 4 levels deep, image built with Finalize

entries cache time before time after Δ time memory before memory after Δ memory
10 warm 2.39 µs 2.04 µs -14.6% 10.4 KiB 6.3 KiB -39.6%
10 oneblock 9.33 µs 9.38 µs ~ 15.0 KiB 10.9 KiB -27.5%
100 warm 29.18 µs 12.02 µs -58.8% 235.9 KiB 48.8 KiB -79.3%
100 oneblock 37.87 µs 21.06 µs -44.4% 264.4 KiB 77.2 KiB -70.8%
800 warm 439.98 µs 91.40 µs -79.2% 3709.9 KiB 379.8 KiB -89.8%
800 oneblock 470.60 µs 112.50 µs -76.1% 3820.9 KiB 490.8 KiB -87.2%

Time per entry is now flat (~115 ns warm) instead of growing with the size of the directory (239 → 550 ns).
The number of allocations does not change: each one is just smaller.

BenchmarkWalkImage: fs.WalkDir over a whole image, Info() on every entry, and in walk+read every regular
file opened by path and read to the end. Synthetic Node.js application package built with mksquashfs (gzip):
278 MiB compressed, 1.3 GiB of data, 82,912 files, 14,061 directories

case time before time after Δ time memory before memory after Δ memory
walk 421.6 ms 271.5 ms -35.6% 2.23 GiB 0.91 GiB -59.3%
walk+read 5.69 s 5.02 s -11.8% 19.82 GiB 9.86 GiB -50.3%

If requested I can provide the code used for benchmarking

@deitch

deitch commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

I am trying to understand how this makes such a difference. Let's say (for simplicity's sake) that blocks are 100 bytes in size, and I am reading 300 bytes with starting offset of 50. So I would read:

  • block0[50:100] - 50 bytes
  • block1[0:100] - 100 bytes
  • block2[0:100] - 100 bytes
  • block3[0:50] - 50 bytes

The only difference between the existing code and this change seems to be that the last block, I read 50 instead of 100 bytes, because I limit reading the data from the block to whatever is left insize. I still read the entire block from the disk when I do fs.readMetaBlock(...). So how does this have such a big impact?

I do see why it is logically correct.

@sap-nocops

Copy link
Copy Markdown
Contributor Author

your example is correct. the real gain here is when reading inodes which are small.
for example:
an 8 KB metadata block and a 32 byte inode at offset 4000:

  • before: b = append(b, m[byteOffset:]...) copies block0[4000:8192], 4192 bytes, to return 32.
  • after: it copies block0[4000:4032], 32 bytes.

@deitch

deitch commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

So we save on the following places:

  1. copying onto the slice b []byte
  2. copying it out as a return from the func, but this is a pointer (as all references to slices are), so the size should not matter

Since we read the whole block from disk into memory anyways, shouldn't that dominate the first, and it will make no difference?

As before, this change is fine and works well. I still don't understand why it would make such a difference.

@sap-nocops

Copy link
Copy Markdown
Contributor Author

the block is not read from disk on every call.
readMetaBlock goes through fs.cache. The disk read and decompression of a metadata block happen once.
The cost that's left per call is the allocation and copy of b.

An 8 KB metadata block holds 250 inodes of 32 bytes. Listing a directory whose inodes are in that block costs:

  • disk read + decompression: 1 × 8 KiB, once
  • before: 250 × (8192 − offset) bytes allocated and copied, on average ~4 KB each, so ~1 MB per block to use 8 KB of inodes
  • after: 250 × 32 bytes, ~8 KiB per block

the block read is amortized over all the inodes it contains, the copy is not.

@deitch

deitch commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Aha! That makes sense!

@deitch
deitch merged commit 8803cc5 into diskfs:master Nov 23, 2026
20 checks passed
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.

2 participants