Skip to content

Avoid to create any subarrays when optimizing 'save, transform, constructPath, restore' (bug 1961107) #19825

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

Merged
merged 1 commit into from
Apr 17, 2025

Conversation

calixteman
Copy link
Contributor

Removing those subarraycalls helps to improve performance by a factor 6 on Linux and by a factor of 3
on Windows 11.

@calixteman
Copy link
Contributor Author

/botio test

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

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

Live output at: http://54.241.84.105:8877/4e0b914673636bc/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

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

Live output at: http://54.193.163.58:8877/2e4b6141cfb520e/output.txt

@Snuffleupagus
Copy link
Collaborator

Snuffleupagus commented Apr 17, 2025

Do we "need" new methods for this, can't we just change the existing ones to accept an optional pos argument instead?
E.g. something like the following (although untested):

diff --git a/src/core/operator_list.js b/src/core/operator_list.js
index 5b1885ef9..f643f181a 100644
--- a/src/core/operator_list.js
+++ b/src/core/operator_list.js
@@ -524,11 +524,11 @@ addState(
       switch (buffer[k++]) {
         case DrawOPS.moveTo:
         case DrawOPS.lineTo:
-          Util.applyTransform(buffer.subarray(k), transform);
+          Util.applyTransform(buffer, transform, k);
           k += 2;
           break;
         case DrawOPS.curveTo:
-          Util.applyTransformToBezier(buffer.subarray(k), transform);
+          Util.applyTransformToBezier(buffer, transform, k);
           k += 6;
           break;
       }
diff --git a/src/shared/util.js b/src/shared/util.js
index fd2d5fcb3..1d1cefc3b 100644
--- a/src/shared/util.js
+++ b/src/shared/util.js
@@ -730,15 +730,15 @@ class Util {
   }
 
   // For 2d affine transforms
-  static applyTransform(p, m) {
-    const p0 = p[0];
-    const p1 = p[1];
-    p[0] = p0 * m[0] + p1 * m[2] + m[4];
-    p[1] = p0 * m[1] + p1 * m[3] + m[5];
+  static applyTransform(p, m, pos = 0) {
+    const p0 = p[pos];
+    const p1 = p[pos + 1];
+    p[pos] = p0 * m[0] + p1 * m[2] + m[4];
+    p[pos + 1] = p0 * m[1] + p1 * m[3] + m[5];
   }
 
   // For 2d affine transforms
-  static applyTransformToBezier(p, transform) {
+  static applyTransformToBezier(p, transform, pos = 0) {
     const m0 = transform[0];
     const m1 = transform[1];
     const m2 = transform[2];
@@ -746,10 +746,10 @@ class Util {
     const m4 = transform[4];
     const m5 = transform[5];
     for (let i = 0; i < 6; i += 2) {
-      const pI = p[i];
-      const pI1 = p[i + 1];
-      p[i] = pI * m0 + pI1 * m2 + m4;
-      p[i + 1] = pI * m1 + pI1 * m3 + m5;
+      const pI = p[pos + i];
+      const pI1 = p[pos + i + 1];
+      p[pos + i] = pI * m0 + pI1 * m2 + m4;
+      p[pos + i + 1] = pI * m1 + pI1 * m3 + m5;
     }
   }

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/4e0b914673636bc/output.txt

Total script time: 30.74 mins

  • Unit tests: Passed
  • Integration Tests: FAILED
  • Regression tests: Passed

@calixteman
Copy link
Contributor Author

/botio-linux browsertest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_browsertest from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/667fed790b2349e/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/2e4b6141cfb520e/output.txt

Total script time: 58.84 mins

  • Unit tests: Passed
  • Integration Tests: FAILED
  • Regression tests: Passed

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/667fed790b2349e/output.txt

Total script time: 16.29 mins

  • Regression tests: Passed

@Snuffleupagus
Copy link
Collaborator

Could we also replace this existing code

const [a, b, c, d, e, f] = this.currentTransform;
for (let i = 0, ii = args.length; i < ii; i += 2) {
const x = args[i];
const y = args[i + 1];
args[i] = a * x + c * y + e;
args[i + 1] = b * x + d * y + f;
}

with something like the following now?

for (let i = 0, ii = args.length; i < ii; i += 2) { 
   Util.applyTransform(args, this.currentTransform, i);
 } 

…ructPath, restore' (bug 1961107)

Removing those `subarray`calls helps to improve performance by a factor 6 on Linux and by a factor of 3
on Windows 11.
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, thank you.

@calixteman
Copy link
Contributor Author

/botio-linux browsertest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_browsertest from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/098491421bf0993/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/098491421bf0993/output.txt

Total script time: 16.21 mins

  • Regression tests: Passed

@calixteman calixteman merged commit 4b1875c into mozilla:master Apr 17, 2025
9 checks passed
@calixteman calixteman deleted the bug1961107 branch April 17, 2025 17:42
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.

3 participants