Repository navigation
perf: store the md5 etag in an xattr instead of hashing on every read - #1771
Open
Heman10x-NGU wants to merge 1 commit into
Open
Heman10x-NGU wants to merge 1 commit into
Heman10x-NGU wants to merge 1 commit into
Conversation
The file backend hashed the whole object with md5 on every GET, HEAD and range read before sending a byte. The checksum is now stored in an xattr with the file's inode, mtime and size, and reused while all three match. A copy over an existing destination drops its cached checksum.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The file backend now keeps the md5 ETag of each object in an xattr. GET, HEAD and range reads reuse it instead of hashing the whole file again. The ETag value does not change.
This affects deployments that use the file backend: self-hosted Supabase and the local CLI stack. Hosted Supabase uses the S3 backend.
Refs #612
Why
With the default
md5ETag algorithm, every read hashes the full object before the response starts. A HEAD, or a range read of the first 1 KB, of a 1 GB file reads all 1 GB first. #612 reported this. #613 addedmtimeas an opt-in and keptmd5as the default for compatibility. Caching keeps md5 ETags valid for existing clients and caches.How
etag()now callscachedMd5(). It reads a new xattr (user.supabase.md5on Linux,com.apple.metadata.supabase.md5on macOS) holding<inode>-<mtime>-<size>:<md5>. If all three match the file, the stored md5 is returned. If not, the file is hashed as before and the xattr is written.headFile(). The xattr is written there and moves with the file on rename.copyObjectwrites withcopyFile, which can keep the destination's inode. It removes the destination's cached md5 right after the copy.mtimealgorithm is unchanged.Limits and side effects:
touch -r,rsync --inplace -t, or a rewrite in the same mtime tick.IN_ATTRIB, so ctime-based backups see each old object change once.Benchmark
The script below uploads one object through
uploadObject()and does one warm-up read. It then timesheadObject(),getObject()until the body is returned, and aRange: bytes=0-1023read with the body consumed. It also counts the bytes read throughfs.createReadStreambefore each response. The same script ran onmasterand on this branch.M4 MacBook Air, 16 GB, APFS, Node 22.21.0. 1 GB object, 7 runs, median (min to max):
The ETag was the same md5 on both branches. Upload time is unchanged.
Caveats: the page cache was warm, so these numbers mostly measure md5 CPU time. Cold cache, slower disks and Linux were not measured.
Benchmark script
Run from a checkout so
tsxpicks up the repo's path aliases:STORAGE_FILE_BACKEND_PATH=<tmp dir> npx tsx bench-file-etag.ts <repo dir> <size MB> <runs>Tests
New tests in
file.test.tsuse an xattr mock that keys values by inode.master, pass here: reads after an upload do not hash the file again.npx vitest run --config vitest.unit.config.ts: 224 test files, 4493 tests pass.npx tsc --noEmitandnpx biome check .(850 files) pass.Not tested
fs-xattrcalls the backend already uses for content type and cache control.FileBackendonly.package.jsonasks for. Tests ran on Node 22.21.0.