Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve caching of shading patterns. (bug 1721949) #13808

Merged
merged 1 commit into from
Jul 28, 2021

Conversation

brendandahl
Copy link
Contributor

The PDF in bug 1721949 uses many unique pattern objects
that references the same shading many times. This caused
a new canvas pattern to be created and cached many times
driving up memory use.

To fix, I've changed the cache in the worker to key off the
shading object and instead send the shading and matrix
separately. While that worked well to fix the above bug,
there could be PDFs that use many shading that could
cause memory issues, so I've also added a LRU cache
on the main thread for canvas patterns. This should prevent
memory use from getting too high.

@brendandahl
Copy link
Contributor Author

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @brendandahl received. Current queue size: 0

Live output at: http://54.67.70.0:8877/ff14baf4af73c25/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @brendandahl received. Current queue size: 0

Live output at: http://3.101.106.178:8877/09e4f6a7ed401ca/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.67.70.0:8877/ff14baf4af73c25/output.txt

Total script time: 26.98 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  errors: 602
  different ref/snapshot: 8
  different first/second rendering: 1

Image differences available at: http://54.67.70.0:8877/ff14baf4af73c25/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://3.101.106.178:8877/09e4f6a7ed401ca/output.txt

Total script time: 38.12 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: FAILED
  • Regression tests: FAILED
  different ref/snapshot: 11
  different first/second rendering: 1

Image differences available at: http://3.101.106.178:8877/09e4f6a7ed401ca/reftest-analyzer.html#web=eq.log

@brendandahl
Copy link
Contributor Author

/botio-linux test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @brendandahl received. Current queue size: 0

Live output at: http://54.67.70.0:8877/d009884031f0799/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.67.70.0:8877/d009884031f0799/output.txt

Total script time: 27.65 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  errors: 584
  different ref/snapshot: 7
  different first/second rendering: 1

Image differences available at: http://54.67.70.0:8877/d009884031f0799/reftest-analyzer.html#web=eq.log

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me, with two small comments; thank you!

* by last insertion.
*/
class LRUCache {
constructor(maxSize) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that you have a this._maxSize <= 0 check further down, you probably want to set a numerical default value here (since (undefined <= 0) === false):

Suggested change
constructor(maxSize) {
constructor(maxSize = 0) {

Comment on lines 90 to 94
if (!shadingFill && this.cachedCanvasPatterns.has(this)) {
pattern = this.cachedCanvasPatterns.get(this);
} else {
if (!shadingFill) {
const tmpCanvas = owner.cachedCanvases.getCanvas(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Unless I'm completely misreading this code, it'd feel slightly easier to reason about the conditions with the following small change:

Suggested change
if (!shadingFill && this.cachedCanvasPatterns.has(this)) {
pattern = this.cachedCanvasPatterns.get(this);
} else {
if (!shadingFill) {
const tmpCanvas = owner.cachedCanvases.getCanvas(
if (!shadingFill) {
if (this.cachedCanvasPatterns.has(this)) {
pattern = this.cachedCanvasPatterns.get(this);
} else {
const tmpCanvas = owner.cachedCanvases.getCanvas(

The PDF in bug 1721949 uses many unique pattern objects
that references the same shading many times. This caused
a new canvas pattern to be created and cached many times
driving up memory use.

To fix, I've changed the cache in the worker to key off the
shading object and instead send the shading and matrix
separately. While that worked well to fix the above bug,
there could be PDFs that use many shading that could
cause memory issues, so I've also added a LRU cache
on the main thread for canvas patterns. This should prevent
memory use from getting too high.
@brendandahl
Copy link
Contributor Author

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @brendandahl received. Current queue size: 0

Live output at: http://54.67.70.0:8877/b9636a4283dfbfb/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @brendandahl received. Current queue size: 0

Live output at: http://3.101.106.178:8877/dd1c5e96ce9c865/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.67.70.0:8877/b9636a4283dfbfb/output.txt

Total script time: 33.48 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 10
  different first/second rendering: 1

Image differences available at: http://54.67.70.0:8877/b9636a4283dfbfb/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://3.101.106.178:8877/dd1c5e96ce9c865/output.txt

Total script time: 38.80 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 485
  different first/second rendering: 1

Image differences available at: http://3.101.106.178:8877/dd1c5e96ce9c865/reftest-analyzer.html#web=eq.log

@Snuffleupagus
Copy link
Collaborator

Hmm, it looks like makeref failed in a previous PR, so the "failures" in #13808 (comment) should be unrelated.

@brendandahl brendandahl merged commit 4ad5c5d into mozilla:master Jul 28, 2021
@brendandahl
Copy link
Contributor Author

/botio makreref

@pdfjsbot
Copy link

From: Bot.io (Windows)


Invalid

Command not implemented: makreref.

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Invalid

Command not implemented: makreref.

@brendandahl
Copy link
Contributor Author

/botio makeref

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_makeref from @brendandahl received. Current queue size: 1

Live output at: http://54.67.70.0:8877/f64f67868399425/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_makeref from @brendandahl received. Current queue size: 2

Live output at: http://3.101.106.178:8877/b8ea7dc718f5d1d/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/f64f67868399425/output.txt

Total script time: 30.30 mins

  • Lint: Passed
  • Make references: Passed
  • Check references: Passed

@pdfjsbot
Copy link

From: Bot.io (Windows)


Success

Full output at http://3.101.106.178:8877/b8ea7dc718f5d1d/output.txt

Total script time: 36.42 mins

  • Lint: Passed
  • Make references: Passed
  • Check references: Passed

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

Successfully merging this pull request may close these issues.

4 participants