Skip to content

Keep track of last directory to improve performance for large multi-page files

Appending a new page to an existing TIFF file (via TIFFWriteDirectory) gets slower the more pages the TIFF file already has. This is because the pages are stored as a linked list without a reference to the last page, so every time one appends a new one the whole list must be traversed to find the end.

A quick benchmark gives the following results (with the latest master):

- Append page to file with 100 pages:  2.5 ms
- Append page to file with 1000 pages: 6.3 ms
- Append page to file with 2000 pages: 11 ms
- Append page to file with 3000 pages: 16 ms

This merge request contains a proposed fix: It adds a new tif_lastdiroff field to the internal TIFF data structure and uses it to store the offset of the last written directory. This field is updated every time the user appends a new directory to keep track of the last page inserted. If an existing file has just been opened, the first append will trigger a full traversal (in the same way as before) and will update the field for future appends.

The previous benchmark with the proposed fix shows the following results:

- Append page to file with 100 pages:  0.7 ms
- Append page to file with 1000 pages: 0.7 ms
- Append page to file with 2000 pages: 0.7 ms
- Append page to file with 3000 pages: 0.7 ms
Edited by Facundo Tuesca

Merge request reports