Skip to content

PdfPig takes many minutes on GetPage(...) for certain document #1390

Description

@mikethea1

Thanks for creating and maintaining this great library!

I'm running into an issue where one particular document causes PdfPig to spin effectively forever just calling document.GetPage(int).

Debugging into the library, I determined that time is all taken in ResourceStore.LoadResourceDictionary. In particular, the loop over extGStateDictionaryToken.Data loops ~30,000 times.

This in and of itself isn't a huge issue, though, since each overall call to LoadResourceDictionary takes ~300ms to complete.

The real issue is that LoadResourceDictionary seems to be called for every single one of the page's ~4200 operations. As far as I can tell, the loads are entirely redundant in that every DictionaryToken passed to LoadResourceDictionary has the same GetHashCode() value and the same extGStateDictionaryToken.Data loop iterations. This makes the page load extremely slow.

Is there something we can do to optimize here and avoid needing to load the same dictionary over and over? FWIW the file opens quickly in Chrome.

As a side note, given that loading PDF data can be kind of unbounded even for reasonably-sized PDFs, it would be nice if PdfPig functions offered a CancellationToken so that we can bail out after a certain time period (kind of like .NET regex timeout). That seems like potentially larger and more controversial build, though.

I'd love to provide the file but unfortunately it's user-provided and not mine to share. However, I can easily test any proposed fix.

Activity

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions