Skip to content

Fix GH-23457: imagebmp() is extremely slow when writing to a file - #23460

Closed
lazerg wants to merge 1 commit into
php:PHP-8.4from
lazerg:fix/gh-23457-gd-stream-write-buffer
Closed

Fix GH-23457: imagebmp() is extremely slow when writing to a file#23460
lazerg wants to merge 1 commit into
php:PHP-8.4from
lazerg:fix/gh-23457-gd-stream-write-buffer

Conversation

@lazerg

@lazerg lazerg commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

imagebmp() writes its pixel data a byte at a time, and the gd stream context turned each of those bytes into its own php_stream_write() call. PHP streams do no write buffering, so a 1920x1080 truecolor image cost about six million write syscalls. libgd's own FILE context does not show this because stdio buffers for it.

Buffering the stream context in 8 KB chunks takes that image from 9.5s to 0.02s here, with byte-identical output. imagewbmp(), imagegd() and imagegd2() go through the same context and were writing per byte too, so they get the same fix. imagexbm() goes through the same context but writes its output via putBuf rather than per-byte putC, so it was not affected by this bug and sees no change from this patch.

Fixes #23457

@devnexen

Copy link
Copy Markdown
Member

is it a bug fix ? no, let s target master here. Thanks

@divinity76

Copy link
Copy Markdown
Contributor

@devnexen using 40 seconds for something that should go in <0.1 seconds is a bug in my book

@devnexen

Copy link
Copy Markdown
Member

ah ok I just read the bug report, ok that makes sense then.

@devnexen

Copy link
Copy Markdown
Member

Might be best to rephrase the PR description a bit, e.g. imagexbm does not look fixed to me. As far as imagebmp goes however, it s looking good.

@lazerg

lazerg commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

You're right, imagexbm() writes through putBuf, not putC, so it never had this bug. Reworded the description to say that instead of grouping it with the others. NEWS and the fix itself only ever named imagebmp() so those didn't need a change.

@devnexen devnexen closed this in d777841 Aug 26, 2026
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