CGPDFDocumentAppendMRUPage parses pages needlessly

Originator:lievendekeyser
Number:rdar://35125028 Date Originated:October 23 2017
Status:Open Resolved:No
Product:iOS Product Version:11.0
Classification:Performance Reproducible:Always
 
Area:
Core Graphics

Summary:

iOS 11 introduced a cache used by CGPDFDocumentGetPage, but it has unwanted performance implications.

When a new page is requested, it's added to the cache (CGPDFDocumentAppendMRUPage). The least recently used page is then probably removed and old images are purged (CGPDFPagePurgeImages)

To know which images can be purged, the old page is probably parsed again (CGPDFScannerScan), but for heavy PDF documents, this can be very CPU- and memory intensive.

The worst thing is, this even happens when only metadata is requested, e.g. looping over all CGPDFPageRefs in a document and requesting their document dictionary (CGPDFPageGetDictionary). In this case, CGPDFPagePurgeImages won't even need to do anything. 


Steps to Reproduce:

Open a complex multi-page PDF document
Loop over all pages, and get their page dictionaries


Expected Results:

Like on iOS 10, this should be a linear task, not relative to the complexity of the pages.


Actual Results:

A lot of memory and CPU is consumed for no obvious reason.


Version/Build:

iOS 11.0.1


Configuration:

Device and simulator

Comments

Screenshot of CPU trace: https://twitter.com/lieven/status/922486399483424768

By lievendekeyser at Oct. 23, 2017, 3:35 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!