Skip to content

TagLib::FileRef and MPEG::File very slow destructor #1010

Open

Description

@mapons

I'm using taglib for a media player
Is very fast to get mp3 tags and embedded pixmaps, but destructor is very slow:
in Ryzen 3900X /linux:
parse time arround 1-25ms per mp3
destructor is arround 100ms !!!

watcher->setFuture(QtConcurrent::run([filename](){
QStringList ret;
        QElapsedTimer timer;
        timer.start();
        qDebug() << "-- TAG (CHECK) --"<<filename ;
        {
            TagLib::FileRef f(filename.toStdString().c_str(),true,TagLib::AudioProperties::Fast);
            if(!f.isNull() && f.tag()) {
                TagLib::Tag *tag = f.tag();
                ret.push_back(QString::fromUtf8(tag->artist().toCString(true)));
                ret.push_back(QString::fromUtf8((tag->title().toCString(true))));
                ret.push_back(QString::fromUtf8((tag->album().toCString(true))));
            }
             if(!f.isNull() && f.audioProperties()) {
                TagLib::AudioProperties *properties = f.audioProperties();
                ret.push_back(QString::number(properties->length()*1000));
            }
            qDebug() << "----> parse interval"<<timer.elapsed()<<"ms";
            timer.start();
        **}  //TagLib::FileRef  DESTRUCTIOR**
        qDebug() << "----> destructor interval "<<timer.elapsed()<<"ms";
        ...

----> parse interval 15 ms
----> parse interval 21 ms
----> parse interval 24 ms
----> parse interval 25 ms
----> parse interval 35 ms
----> parse interval 34 ms
----> parse interval 40 ms
----> destructor interval 105 ms
----> destructor interval 109 ms
----> destructor interval 106 ms
----> destructor interval 107 ms
----> destructor interval 102 ms
----> destructor interval 106 ms

any idea?

Activity

  1. ufleisch commented on May 8, 2021

    @ufleisch
    Contributor

    The type of audio file could be relevant to answer this question, because the concrete destructor implementations differ. I would not expect that closing the files takes so long when you are only reading from them. Could you please profile the situation further, e.g. using Valgrind (menu Analyze/Valgrind Function Profiler in Qt Creator). You could also try KCachegrind. I can also recommend Gperftools.

  2. scotchi commented on May 8, 2021

    @scotchi
    Member

    It's worth noting that what you may be timing is not the destructor, but the debugging statement that's in the block; often printing is slow. Also, the timer is cumulative, so it's more like the time between the first and second print statements is 65-90 ms.

  3. mapons commented on May 10, 2021

    @mapons
    Author

    Now I using readonly version, and is faster:

               TagLib::FileStream fstream(filename.toStdString().c_str(), true); //READ ONLY
               TagLib::FileRef f(&fstream,true,TagLib::AudioProperties::Fast);
                //TagLib::FileRef f(filename.toStdString().c_str(),true,TagLib::AudioProperties::Fast);//RW
    
    same for 
    
                TagLib::FileStream fstream(filename.toStdString().c_str(), true); //READ ONLY
                MPEG::File f(&fstream,ID3v2::FrameFactory::instance() );
                //MPEG::File f(filename.toStdString().c_str()); //RW
    

    RO 1-15ms
    RW 100 -125ms

  4. ufleisch commented on May 14, 2021

    @ufleisch
    Contributor

    I had a look at this issue. When I open a file on my internal SSD, the time spent in the destructor is small. However, if I open a file on a USB stick, I can confirm that the time spent in the destructor is above 100ms. Tracking the calls down, it can be seen that most of the time is spent in fclose(). I can just instrument closeFile() in tfilestream.cpp to print the time in us (using #include <sys/time.h> on Linux):

      void closeFile(FileHandle file)
      {
        timeval start, stop;
        gettimeofday(&start, 0);
        fclose(file);
        gettimeofday(&stop, 0);
        debug("closeFile: " + TagLib::String::number(
                stop.tv_sec * 1000000 + stop.tv_usec -
                start.tv_sec * 1000000 - start.tv_usec));
      }
    

    If the file is opened in read-only mode, fclose() is fast. When editing tags, it is possible that it is not known in advance if a file has to be modified. So an idea for improvement could be to always open files in read-only mode (fopen() with mode rb) but store the information if a file is allowed to be written and then change the file mode to rb+ using freopen(0, "rb+", d->file) when allowed and necessary, i.e. before a call to the writeFile() function in tfilestream.cpp. However, I am not sure if this is a good idea, especially the sentence

    It is implementation-defined which changes of mode are permitted

    in the freopen man page makes me doubt.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions