Skip to content

Commit d7cbda6

Browse files
committed
Avoid to create any subarrays when optimizing 'save, transform, constructPath, 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.
1 parent 2f7d163 commit d7cbda6

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

src/core/font_renderer.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -745,12 +745,9 @@ class Commands {
745745

746746
add(cmd, args) {
747747
if (args) {
748-
const [a, b, c, d, e, f] = this.currentTransform;
748+
const { currentTransform } = this;
749749
for (let i = 0, ii = args.length; i < ii; i += 2) {
750-
const x = args[i];
751-
const y = args[i + 1];
752-
args[i] = a * x + c * y + e;
753-
args[i + 1] = b * x + d * y + f;
750+
Util.applyTransform(args, currentTransform, i);
754751
}
755752
this.cmds.push(`${cmd}${args.join(" ")}`);
756753
} else {

src/core/operator_list.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,11 @@ addState(
524524
switch (buffer[k++]) {
525525
case DrawOPS.moveTo:
526526
case DrawOPS.lineTo:
527-
Util.applyTransform(buffer.subarray(k), transform);
527+
Util.applyTransform(buffer, transform, k);
528528
k += 2;
529529
break;
530530
case DrawOPS.curveTo:
531-
Util.applyTransformToBezier(buffer.subarray(k), transform);
531+
Util.applyTransformToBezier(buffer, transform, k);
532532
k += 6;
533533
break;
534534
}

src/shared/util.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -730,26 +730,25 @@ class Util {
730730
}
731731

732732
// For 2d affine transforms
733-
static applyTransform(p, m) {
734-
const p0 = p[0];
735-
const p1 = p[1];
736-
p[0] = p0 * m[0] + p1 * m[2] + m[4];
737-
p[1] = p0 * m[1] + p1 * m[3] + m[5];
733+
static applyTransform(p, m, pos = 0) {
734+
const p0 = p[pos];
735+
const p1 = p[pos + 1];
736+
p[pos] = p0 * m[0] + p1 * m[2] + m[4];
737+
p[pos + 1] = p0 * m[1] + p1 * m[3] + m[5];
738738
}
739739

740-
// For 2d affine transforms
741-
static applyTransformToBezier(p, transform) {
740+
static applyTransformToBezier(p, transform, pos = 0) {
742741
const m0 = transform[0];
743742
const m1 = transform[1];
744743
const m2 = transform[2];
745744
const m3 = transform[3];
746745
const m4 = transform[4];
747746
const m5 = transform[5];
748747
for (let i = 0; i < 6; i += 2) {
749-
const pI = p[i];
750-
const pI1 = p[i + 1];
751-
p[i] = pI * m0 + pI1 * m2 + m4;
752-
p[i + 1] = pI * m1 + pI1 * m3 + m5;
748+
const pI = p[pos + i];
749+
const pI1 = p[pos + i + 1];
750+
p[pos + i] = pI * m0 + pI1 * m2 + m4;
751+
p[pos + i + 1] = pI * m1 + pI1 * m3 + m5;
753752
}
754753
}
755754

0 commit comments

Comments
 (0)