tar: stream file reads instead of buffering into Vec<u8>#72
Merged
Conversation
As part of debugging #52 I ended up adding an `alloc_tracker` feature. I initially thought about making it a permanent feature, but it seems unlikely that we'll need it often. So for now at least, let's just capture that knowledge into a skill. Assisted-by: Claude Opus 4.6
So this is a pretty embarrassing bug. When adding files to the tar builder, we were loading into memory the _whole_ file. What's worse, when writing the OCI archive itself, we were reading into memory the _whole_ tar layers, which is crazy. Anyway, fix this by doing the obvious thing and just passing fds. This reduces allocations related to the OCI building phase from ~6.5 GB to ~29 MB for a 3.2 GiB FCOS image (99.6% reduction). Peak RSS drops to ~75 MB. There's more memory savings in the RPM path handling area, but not sure if the juice is worth the squeeze complexity-wise. Fixes #52. Assisted-by: Claude Opus 4.6
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a significant memory consumption issue by switching from buffering entire files into memory to streaming them during the tar creation process. The changes in src/tar.rs are well-implemented and directly fix the problem described, leading to a massive reduction in memory allocations. The addition of memory profiling tools, including the alloc-tracker documentation and the --write-peak-mem-to flag, is a valuable enhancement for future performance analysis.
I've added a couple of suggestions to improve the robustness of the new memory usage parsing logic in src/utils.rs and the corresponding test script.
Add a hidden `--write-peak-mem-to` flag to the build command which reads `VmHWM` from `/proc/self/status` and writes it out. Then in the FCOS e2e test, use that knob and verify that it's under 200 MiB. This is just a soft guard against an egregious regression like #52. Assisted-by: Claude Opus 4.6
ea236d7 to
f3e8177
Compare
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.
So this is a pretty embarrassing bug. When adding files to the tar
builder, we were loading into memory the whole file. What's worse,
when writing the OCI archive itself, we were reading into memory the
whole tar layers, which is crazy.
Anyway, fix this by doing the obvious thing and just passing fds.
This reduces allocations related to the OCI building phase from ~6.5 GB
to ~29 MB for a 3.2 GiB FCOS image (99.6% reduction). Peak RSS drops to
~75 MB.
There's more memory savings in the RPM path handling area, but not sure
if the juice is worth the squeeze complexity-wise.
Fixes #52.
Assisted-by: Claude Opus 4.6