Repository navigation
Closed
Description
Reproducer
Running this benchmark on a directory of tagged files:
use clap::Parser;
use lofty::probe::Probe;
use lofty::tag::{ItemValue, TagExt};
use lofty::file::TaggedFileExt;
use std::path::PathBuf;
use std::time::Instant;
use walkdir::WalkDir;
use crc32fast::Hasher;
#[derive(Parser)]
#[command(name = "lofty-benchmark")]
struct Cli {
/// Directory to scan for audio files
path: PathBuf,
/// Print each tag as it is read
#[arg(short, long)]
debug: bool,
}
fn main() {
let args = Cli::parse();
let start = Instant::now();
let mut hasher = Hasher::new();
let mut file_count = 0;
// Collect all files
let mut files: Vec<_> = WalkDir::new(&args.path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.collect();
// Sort alphabetically to ensure reproducible order
files.sort_by(|a, b| a.path().cmp(b.path()));
for entry in files {
// Attempt to read tags from the file
// Probe::open guesses the format, read() parses it.
// We ignore errors as the directory may contain non-audio files.
if let Ok(results) = Probe::open(entry.path())
.and_then(|p| p.read())
{
file_count += 1;
// Iterate over all tags found in the file (e.g. ID3v2, Vorbis, etc.)
for tag in results.tags() {
if args.debug {
println!("File: {:?}, Tag Type: {:?}, Items: {}", entry.path(), tag.tag_type(), tag.len());
}
// Collect and sort items by key for reproducible order
let mut items: Vec<_> = tag.items().collect();
items.sort_by(|a, b| format!("{:?}", a.key()).cmp(&format!("{:?}", b.key())));
for item in items {
if args.debug {
println!("\t{:?}: {:?}", item.key(), item.value());
}
// Update checksum with key
hasher.update(format!("{:?}", item.key()).as_bytes());
// Update checksum with value
match item.value() {
ItemValue::Text(s) => hasher.update(s.as_bytes()),
ItemValue::Locator(s) => hasher.update(s.as_bytes()),
ItemValue::Binary(b) => hasher.update(b),
}
}
}
}
}
let duration = start.elapsed();
println!("Scanned {} files in {:.3?}", file_count, duration);
println!("Checksum: {:08x}", hasher.finalize());
}Summary
➜ lofty-benchmark git:(master) cargo run ~/Desktop/music/opus/autechre/
Compiling lofty v0.22.4 (/Users/miles/c/lofty-rs/lofty)
Compiling lofty-benchmark v0.1.0 (/Users/miles/c/lofty-benchmark)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.65s
Running `target/debug/lofty-benchmark /Users/miles/Desktop/music/opus/autechre/`
Scanned 262 files in 6.362s
Checksum: d8b76029➜ lofty-benchmark git:(master) cargo run ~/Desktop/music/aac/autechre/
Compiling lofty v0.22.4 (/Users/miles/c/lofty-rs/lofty)
Compiling lofty-benchmark v0.1.0 (/Users/miles/c/lofty-benchmark)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.53s
Running `target/debug/lofty-benchmark /Users/miles/Desktop/music/aac/autechre/`
Scanned 262 files in 150.477ms
Checksum: 6f172600I also noticed TagLib is massively faster than lofty for this case.
I have an experimental branch that changes this code to read backwards from the end of the file that seems to bring opus speed in line with AAC. If you're interested I could put up a PR or just paste it here.
Lines 43 to 57 in 8f667a2
| fn find_last_page<R>(data: &mut R) -> Result<Page> | |
| where | |
| R: Read + Seek, | |
| { | |
| let mut last_page_header = PageHeader::read(data)?; | |
| data.seek(SeekFrom::Current(last_page_header.content_size() as i64))?; | |
| while let Ok(header) = PageHeader::read(data) { | |
| last_page_header = header; | |
| data.seek(SeekFrom::Current(last_page_header.content_size() as i64))?; | |
| } | |
| data.seek(SeekFrom::Start(last_page_header.start))?; | |
| Ok(Page::read(data)?) | |
| } |
Expected behavior
Opus tag reading shouldn't be this much slower than m4a.
Assets
No response
Activity
Serial-ATA commentedon Jan 11, 2026
Yeah, reading from the end makes more sense here. Feel free to make a PR if you can. :)
Just a note, for benchmarks you should be using release mode. It makes a huge difference. Just testing on my library (which is mostly Opus):
debug:
release:
milesegan commentedon Jan 11, 2026
Interesting I don't see a big difference here:
This is a Macbook M4 Pro, internal SSD.
If you think the reading from the end approach makes sense I'll put together a PR.
milesegan commentedon Jan 11, 2026
@Serial-ATA How large are the files in your library? I wonder if this is more i/o bound in my case?
Serial-ATA commentedon Jan 11, 2026
$ du -sh 58G .Average file size is ~32MB
Serial-ATA commentedon Jan 23, 2026
@milesegan are you working on this? No rush if you are, just want to know if I should pick it up or not.
milesegan commentedon Jan 23, 2026
@Serial-ATA Day job and trying to get out a big update of my music app kept me pretty busy this week but I should have more time soon.
That said, if you have bandwidth for this go ahead since I'm sure you'd do a better job than I would.
Serial-ATA commentedon Jan 23, 2026
All good, no rush. :)
I have other things to work on right now, so I'll probably pick this up next weekend if you don't get to it. Hoping to finally get 0.23.0 out in the next 2 weeks
Serial-ATA commentedon Jan 31, 2026
Hey @milesegan I'll pick this up so it makes it in 0.23.0
milesegan commentedon Jan 31, 2026
Sorry I haven't had time to polish this up into a proper PR this week. This change is working for me and shows a big speedup at least on my files on my machine:
main...milesegan:lofty-rs:opus-performance-improvements
This was AI generated so I'm not proposing it as a PR but it does seem to at least validate the concept.
Serial-ATA commentedon Feb 7, 2026
If you get a chance, could you try out #600? In my case it's a pretty significant improvement.
Before:
After:
milesegan commentedon Feb 7, 2026
#600 is a massive performance improvement for me here. I left some measurements on that PR thread.
Thanks for looking into this!