Skip to content

Read each matrix row once in the encoder's inner loops - #246

Open
mattiasgeniar wants to merge 3 commits into
Bacon:mainfrom
mattiasgeniar:perf/matrix-row-access
Open

mattiasgeniar wants to merge 3 commits into
Bacon:mainfrom
mattiasgeniar:perf/matrix-row-access

Conversation

@mattiasgeniar

Copy link
Copy Markdown

The encoder's inner loops look up the matrix row again for every cell: through ByteMatrix::get() and set() while embedding data bits, and through $array[$y][$x] in mask penalty rules 2 and 3 and in clear(). These three commits fetch each row once and index into it. The matrix and mask pattern are identical for 264 inputs across all four error-correction levels, and the test suite has the same result as main (the two GD tests error on both here, without the extension).

Benchmark (an otpauth:// URL, the usual 2FA QR code) Before After % change
Encoder::encode() 4.37 ms 3.35 ms −23.3%
Encoder::encode(), first call 4.95 ms 3.94 ms −20.4%
Writer::writeString(), 400 px SVG 7.17 ms 6.26 ms −12.7%

Median of 15 fresh PHP 8.5 processes per side, run interleaved, opcache on. "First call" is the first run in a new process.

@mattiasgeniar
mattiasgeniar marked this pull request as ready for review November 3, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant