## Description

`BaseParser` keeps `keyCache` in an **instance** field:

```java
private final Map<Long, COSObjectKey> keyCache = new HashMap<>();
```

`getObjectKey(long, int)` fills it by copying every key of the document's
xref table whenever `xrefTable.size() > keyCache.size()`.

`COSParser.parseObjectStreamObject` creates a **new**
`PDFObjectStreamParser` (a `BaseParser`) for each object stream it has to
read. Each new parser
starts with an empty `keyCache`, so the first call to `getObjectKey` in
`PDFObjectStreamParser.parseAllObjects` copies the complete xref table
again.

Cost is therefore (number of object streams read) x (number of xref
entries). For a document with N objects in N/100 object streams this is
O(N^2 / 100) map insertions.

## Impact

A real-world 22 MB, 99-page PDF with 1,479,409 xref entries in 15,143
object streams (most of them a corrupt /PageLabels number tree) could not be
loaded-and-saved in over 17 minutes; a thread dump always shows:

```
java.util.HashMap.putVal
java.util.HashMap.putIfAbsent
org.apache.pdfbox.pdfparser.BaseParser.getObjectKey(BaseParser.java:195)
org.apache.pdfbox.pdfparser.PDFObjectStreamParser.parseAllObjects(PDFObjectStreamParser.java:142)
org.apache.pdfbox.pdfparser.COSParser.parseObjectStreamObject(COSParser.java:830)
org.apache.pdfbox.pdfparser.COSParser.parseObjectDynamically(COSParser.java:677)
org.apache.pdfbox.pdfparser.COSParser.dereferenceCOSObject(COSParser.java:625)
org.apache.pdfbox.cos.COSObject.getObject(COSObject.java:117)
org.apache.pdfbox.pdfwriter.compress.COSWriterCompressionPool.addElements(COSWriterCompressionPool.java:219)
```

Timings for load + `protect()` + `save(file,
CompressParameters.DEFAULT_COMPRESSION)`, Java 25, -Xmx4g:

| Document | Objects | 3.0.3 / 3.0.8 | With the cache shared per document |
|---|---|---|---|
| Synthetic, tiny dictionaries | 100,000 | 6 s | 1 s |
| Synthetic, tiny dictionaries | 400,000 | 67 s (3.0.8), > 90 s (3.0.3) |
4–7 s |
| Synthetic, tiny dictionaries | 1,500,000 | > 90 s | 17–27 s |
| Real 22 MB file | 1,479,409 | > 17 min | 17 s |

## Steps to reproduce

1. Create a document whose catalog references ~400,000 small indirect
dictionaries (e.g. a tree of `<< /S /Span /P ref /K n >>`), and save it with
   default compression so they land in object streams.
2. `Loader.loadPDF(file)` then `doc.save(out)`.
3. Observe the time, and the thread dump above.

(A generator for the synthetic file can be attached; the real file is
proprietary.)

## Suggested fix

Share the key cache between all parsers of the same `COSDocument` (e.g.
hold it in `COSDocument`, or pass the parent parser's cache to
`PDFObjectStreamParser`). We verified the idea with a
`WeakHashMap<COSDocument, Map<Long, COSObjectKey>>` in `BaseParser`;
results are in the last
column above.

## Related observation (separate issue?)

Saving the same documents with `CompressParameters.NO_COMPRESSION` is also
quadratic: `COSWriter.addObjectToWrite` calls
`objectsToWrite.contains(object)` on an `ArrayDeque` (linear scan) for
every object.

Reply via email to