Repository navigation
Description
Reproducer
Write or modify a Vorbis comment on a large FLAC using Lofty's normal public save API and measure the process's peak resident memory.
On Lofty 0.25.0, the FLAC writer currently reads the complete file into memory before replacing the metadata range.
I reproduced this with a generated 357,463,057-byte (~341 MiB) FLAC.
Peak RSS while performing the metadata write:
- Lofty 0.25.0: ~532,952 KiB
- bounded-I/O prototype: ~8,240 KiB
Both outputs passed flac -t and preserved the FLAC audio MD5/signature.
Summary
Writing metadata to a FLAC currently makes memory usage scale with the size of the entire audio file rather than the size of the metadata.
The current FLAC writer reads the whole file into a Vec, modifies the metadata region, truncates the original file, and writes the full buffer back.
This is noticeable with large FLAC files. A ~341 MiB test file caused roughly 533 MiB peak RSS for a simple metadata write.
I prototyped an alternative that keeps Lofty's existing FLAC parsing and metadata encoding, but replaces the final whole-file buffer/splice with an in-place range replacement using a fixed 64 KiB movement buffer.
On the same file that reduced peak RSS to about 8 MiB while preserving audio integrity.
This seems related to #445, although the narrower issue here is whole-file memory buffering rather than full FLAC padding optimization.
Expected behavior
FLAC metadata writes should not require buffering the complete encoded audio payload in memory.
Memory usage should primarily depend on the metadata being parsed/encoded plus a bounded I/O buffer.
Assets
AI disclosure: I used AI assistance during the investigation, prototyping, test design, and drafting of this report. The measurements and validation above were reproduced locally. If this proceeds to a PR, I intend to review and understand the patch myself and participate directly in the review rather than treating the AI output as authoritative. I'm sorry if this is annoying, tbh i'll have to patch this in my code everytime you guys update, otherwise i probably wouldn't bother y'all. cheers.
No copyrighted media is required to reproduce this.
The test file was generated from random PCM data.
I also have a tested one-file prototype patch against Lofty 0.25.0 that:
- preserves the existing public API;
- adds no dependencies;
- adds no unsafe code;
- keeps Lofty's existing FLAC metadata parsing/encoding;
- moves the remaining file contents in bounded 64 KiB chunks;
- passes Rust 1.89 MSRV and the current test, Clippy, fmt and docs CI commands;
- passes real FLAC grow/shrink tests with preserved audio MD5/signature.
I can submit the patch as a PR if this approach is acceptable.
Activity
Serial-ATA commentedon Aug 12, 2026
Hello @sxntaxis! Sure, feel free to make a PR. FLAC writes actually used to be lighter a few versions ago, but it ended up being buggy (#549, #640). I didn't have much time to spend on it, so I just dropped back to the simplest approach so I didn't have to think about it.