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

Avoid calling Math.pow if possible. #11963

Merged
merged 1 commit into from
Jun 7, 2020

Conversation

tamuratak
Copy link
Contributor

@tamuratak tamuratak commented Jun 2, 2020

Related to #9581.

When loading a high-resolution image, I have found that the most time-consuming part is converting sRGB to RGB. Especially calling Math.pow is expensive. The following profile of Chrome is the one when loading the PDF file of #9581.

スクリーンショット 2020-06-03 8 10 58

スクリーンショット 2020-06-03 8 11 41

In this PR, we avoid calling Math.pow if the value of color is close enough to 1. The threshold is determined to assure that the result is larger than 254.5.

> ((1 + 0.055) * 0.99554525**(1/2.4) - 0.055)*255
 254.50000003134699

The benchmark result for the PDF file of #9581 is significant.

$  node stats/statcmp.js stats/results/baseline.json stats/results/current.json
-- Grouped By browser, stat --
browser | stat         | Count | Baseline(ms) | Current(ms) |    +/- |     %  | Result(P<.05)
------- | ------------ | ----- | ------------ | ----------- | ------ | ------ | -------------
chrome  | Overall      |     3 |        26821 |       11648 | -15172 | -56.57 |        faster
chrome  | Page Request |     3 |            7 |           7 |      0 |   0.00 |              
chrome  | Rendering    |     3 |        26814 |       11641 | -15173 | -56.59 |        faster
firefox | Overall      |     3 |        14270 |        9383 |  -4887 | -34.25 |        faster
firefox | Page Request |     3 |            6 |           5 |     -1 | -16.67 |              
firefox | Rendering    |     3 |        14264 |        9378 |  -4886 | -34.26 |        faster

One of other possible solutions is to avoid converting sRGB to RGB if not necessary. I think it is not necessary when we just display images without transparency operations.

@tamuratak tamuratak changed the title Avoid to call Math.pow if possible. Avoid calling Math.pow if possible. Jun 2, 2020
@tamuratak tamuratak force-pushed the srgb_conv branch 2 times, most recently from dd974cd to 5a267a1 Compare June 2, 2020 23:54
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.

While #11963 (comment) contains a lot of nice information/context, please make sure that that information is also present in the actual commit message as well.

That way the information is generally available on e.g. the Git command line too (which is often useful when working with the code locally or when bisecting something). Also, explicitly mentioning that this applies to the CalRGB colorspace in the title/commit message probably doesn't hurt either :-)

@@ -1057,6 +1057,9 @@ const CalRGBCS = (function CalRGBCSClosure() {
if (color <= 0.0031308) {
return adjustToRange(0, 1, 12.92 * color);
}
if (color >= 0.99554525) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a comment above this line, explaining exactly how this value was determined!
As-is, this line looks quite wrong until you've actually read #11963 (comment)

@timvandermeij
Copy link
Contributor

Looks good with the comment above addressed.

Avoid calling Math.pow if possible when calculating the transfer
function of the CalRGB color space since calling Math.pow is expensive.

If the value of color is larger than the threshold, 0.99554525,
the final result of the transform is larger that 254.5
since ((1 + 0.055) * 0.99554525 ** (1 / 2.4) - 0.055) * 255 === 254.50000003134699
@tamuratak
Copy link
Contributor Author

Thank you for your responses. I have edited as requested.

@timvandermeij
Copy link
Contributor

/botio test

@pdfjsbot
Copy link

pdfjsbot commented Jun 7, 2020

From: Bot.io (Linux m4)


Received

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

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

@pdfjsbot
Copy link

pdfjsbot commented Jun 7, 2020

From: Bot.io (Windows)


Received

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

Live output at: http://54.215.176.217:8877/18d9e571f1c3cf1/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Jun 7, 2020

From: Bot.io (Linux m4)


Failed

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

Total script time: 25.67 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: FAILED

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

@pdfjsbot
Copy link

pdfjsbot commented Jun 7, 2020

From: Bot.io (Windows)


Failed

Full output at http://54.215.176.217:8877/18d9e571f1c3cf1/output.txt

Total script time: 29.96 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: FAILED

Image differences available at: http://54.215.176.217:8877/18d9e571f1c3cf1/reftest-analyzer.html#web=eq.log

@timvandermeij
Copy link
Contributor

/botio-linux preview

@pdfjsbot
Copy link

pdfjsbot commented Jun 7, 2020

From: Bot.io (Linux m4)


Received

Command cmd_preview from @timvandermeij received. Current queue size: 0

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

@pdfjsbot
Copy link

pdfjsbot commented Jun 7, 2020

From: Bot.io (Linux m4)


Success

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

Total script time: 3.41 mins

Published

@timvandermeij timvandermeij merged commit b779507 into mozilla:master Jun 7, 2020
@timvandermeij
Copy link
Contributor

Nice work; thank you for figuring this out!

@tamuratak tamuratak deleted the srgb_conv branch June 8, 2020 00:07
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