Skip to content

Opus tag reading very slow #588

Closed

Description

@milesegan

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: 6f172600

I 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.

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

  1. added
    bug
    on Jan 11, 2026
  2. added this to the 0.23.0 milestone on Jan 11, 2026
  3. Serial-ATA commented on Jan 11, 2026

    @Serial-ATA
    Owner

    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:

    Scanned 3739 files in 22.694s
    Checksum: c2b79be8
    

    release:

    Scanned 3739 files in 900.429ms
    Checksum: c2b79be8
    
  4. milesegan commented on Jan 11, 2026

    @milesegan
    Author

    Interesting I don't see a big difference here:

      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.60s
         Running `target/debug/lofty-benchmark /Users/miles/Desktop/music/opus/autechre`
    Scanned 262 files in 6.089s
    Checksum: d8b76029
    ➜  lofty-benchmark git:(master) cargo run --release ~/Desktop/music/opus/autechre
        Finished `release` profile [optimized] target(s) in 0.03s
         Running `target/release/lofty-benchmark /Users/miles/Desktop/music/opus/autechre`
    Scanned 262 files in 5.417s
    Checksum: d8b76029
    ➜  lofty-benchmark git:(master) cargo run --release ~/Desktop/music/aac/autechre
        Finished `release` profile [optimized] target(s) in 0.03s
         Running `target/release/lofty-benchmark /Users/miles/Desktop/music/aac/autechre`
    Scanned 262 files in 115.584ms
    Checksum: 6f172600

    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.

  5. milesegan commented on Jan 11, 2026

    @milesegan
    Author

    @Serial-ATA How large are the files in your library? I wonder if this is more i/o bound in my case?

    ➜  lofty-benchmark git:(master) du -sk /Users/miles/Desktop/music/opus/autechre
    3823380 /Users/miles/Desktop/music/opus/autechre
  6. Serial-ATA commented on Jan 11, 2026

    @Serial-ATA
    Owner
    $ du -sh
    58G	.

    Average file size is ~32MB

  7. Serial-ATA commented on Jan 23, 2026

    @Serial-ATA
    Owner

    @milesegan are you working on this? No rush if you are, just want to know if I should pick it up or not.

  8. milesegan commented on Jan 23, 2026

    @milesegan
    Author

    @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.

  9. Serial-ATA commented on Jan 23, 2026

    @Serial-ATA
    Owner

    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

  10. Serial-ATA commented on Jan 31, 2026

    @Serial-ATA
    Owner

    Hey @milesegan I'll pick this up so it makes it in 0.23.0

  11. milesegan commented on Jan 31, 2026

    @milesegan
    Author

    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.

  12. Serial-ATA commented on Feb 7, 2026

    @Serial-ATA
    Owner

    If you get a chance, could you try out #600? In my case it's a pretty significant improvement.

    Before:

    Scanned 3739 files in 887.181ms
    Checksum: c2b79be8
    

    After:

    Scanned 3739 files in 432.561ms
    Checksum: de5db68b
    
  13. milesegan commented on Feb 7, 2026

    @milesegan
    Author

    #600 is a massive performance improvement for me here. I left some measurements on that PR thread.

    Thanks for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions