refactor(snapshots): reduce per-file heap allocations during snapshot (minor perf improvement) - #5571
Open
jkowalski wants to merge 1 commit into
Open
refactor(snapshots): reduce per-file heap allocations during snapshot (minor perf improvement)#5571jkowalski wants to merge 1 commit into
jkowalski wants to merge 1 commit into
Conversation
Snapshotting a directory tree performs a small number of heap
allocations for every single file, independent of its size. This change
removes two of those per-file allocations. They are individually tiny,
but they scale linearly with the number of files, so on large trees they
add up to a measurable reduction in total allocation count.
The two changes:
1. localfs: use DirEntry.Info() instead of os.Lstat(prefix + name)
While iterating a directory, toDirEntryOrNil() called
os.Lstat(prefix + n) for every entry, which required building the
full path string on each iteration. DirEntry.Info() returns the same
information (it is an lstat of the entry) without the caller having to
construct the path, and it also correctly handles the case where the
entry is removed between ReadDir() and the stat. This removes one
string concatenation per directory entry.
2. upload: use the bare filename as the object writer description
The uploader built the object writer description as "FILE:" + name
(and "SYMLINK:" / "STREAMFILE:" for the other types), allocating a
new concatenated string per file. The description is only used in
error and log messages, where the bare path is already clear and the
surrounding error context identifies the file. Passing the name
directly removes three more concatenations.
Measured impact
Benchmarked by snapshotting two datasets with otherwise-identical
baseline and patched binaries, reading runtime.MemStats around the
upload:
- The Kopia source tree (~34,500 files): allocation count dropped by
~34,000 mallocs (~1.6%), about one fewer allocation per file.
- A synthetic tree of 1,000,000 small files: allocation count dropped
by ~950,000 mallocs (~2.0%), again about one fewer allocation per
file.
Total bytes allocated and peak heap are effectively unchanged: these were
small, short-lived objects, so removing them reduces GC work (fewer
objects to scan and collect) but not the overall memory footprint, which
is dominated by content hashing, compression, and the repository index.
The changes are behavior-preserving; the existing test suites for
fs/localfs and snapshot/upload pass.
There was a problem hiding this comment.
Pull request overview
Reduces per-file allocations during snapshot uploads.
Changes:
- Uses
DirEntry.Info()during directory iteration. - Removes object-type prefixes from writer descriptions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
fs/localfs/local_fs_os.go |
Changes directory-entry metadata lookup. |
snapshot/upload/upload.go |
Uses filenames directly as writer descriptions. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+99
to
+102
| // DirEntry.Info() is equivalent to Lstat(prefix+n) but is implemented by | ||
| // the os package without exposing the path; it also handles the | ||
| // entry-deleted-between-ReadDir-and-Info case. | ||
| switch fi, err := dirEntry.Info(); { |
Contributor
Author
There was a problem hiding this comment.
this is actually fine, we're not requiring fresh lstat.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #5571 +/- ##
==========================================
+ Coverage 75.86% 77.84% +1.98%
==========================================
Files 470 553 +83
Lines 37301 31746 -5555
==========================================
- Hits 28299 24714 -3585
+ Misses 7071 4966 -2105
- Partials 1931 2066 +135 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
julio-lopez
approved these changes
Aug 18, 2026
jkowalski
enabled auto-merge (squash)
August 19, 2026 14:33
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.
Snapshotting a directory tree performs a small number of heap allocations for every single file, independent of its size. This change removes two of those per-file allocations. They are individually tiny, but they scale linearly with the number of files, so on large trees they add up to a measurable reduction in total allocation count.
The two changes:
localfs: use DirEntry.Info() instead of os.Lstat(prefix + name)
While iterating a directory, toDirEntryOrNil() called
os.Lstat(prefix + n) for every entry, which required building the
full path string on each iteration. DirEntry.Info() returns the same
information (it is an lstat of the entry) without the caller having to
construct the path, and it also correctly handles the case where the
entry is removed between ReadDir() and the stat. This removes one
string concatenation per directory entry.
upload: use the bare filename as the object writer description
The uploader built the object writer description as "FILE:" + name
(and "SYMLINK:" / "STREAMFILE:" for the other types), allocating a
new concatenated string per file. The description is only used in
error and log messages, where the bare path is already clear and the
surrounding error context identifies the file. Passing the name
directly removes three more concatenations.
Measured impact
Benchmarked by snapshotting two datasets with otherwise-identical baseline and patched binaries, reading runtime.MemStats around the upload:
Total bytes allocated and peak heap are effectively unchanged: these were small, short-lived objects, so removing them reduces GC work (fewer objects to scan and collect) but not the overall memory footprint, which is dominated by content hashing, compression, and the repository index.
The changes are behavior-preserving; the existing test suites for fs/localfs and snapshot/upload pass.