Skip to content

mysqlnd: Do not compress packets smaller than 512 bytes - #24702

Open
lohmag wants to merge 2 commits into
php:masterfrom
lohmag:mysqlnd-compression-threshold
Open

lohmag wants to merge 2 commits into
php:masterfrom
lohmag:mysqlnd-compression-threshold

Conversation

@lohmag

@lohmag lohmag commented Oct 25, 2026

Copy link
Copy Markdown

With MYSQLI_CLIENT_COMPRESS, mysqlnd runs compress() on every packet it sends, however small. Each call sets up a new deflate stream, and below a few hundred bytes the packet barely shrinks, so the client pays the full setup cost for almost nothing.

This sends packets smaller than 512 bytes uncompressed inside the compressed envelope (uncompressed length 0), which the protocol already allows and which mysqlnd already does for packets that do not shrink. libmysqlclient uses the same mechanism with a 50 byte threshold.

Client CPU per query, release build, one pinned core, MySQL 8.0.46 on the same host, SELECT '<n bytes>' over a compressed connection, median of 5 runs of 50000 queries:

Query size Before After
100 B 7.72 µs 4.76 µs
300 B 8.20 µs 4.86 µs
600 B 8.97 µs 8.96 µs
2000 B 11.31 µs 11.39 µs

The new test sends payloads on both sides of the threshold, including random data that does not compress, and checks that each one comes back intact.

Comment thread ext/mysqli/tests/mysqli_compression_packet_sizes.phpt Outdated
@Sjord

Sjord commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Looks good to me.

MySQL 8.0.46 on the same host

I think it would be interesting to get performance information when the connection goes over the internet. Compression would be more benificial on slow links, but I guess it would not make much of a difference as long as the query fits in one packet.

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.

2 participants