|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-02-23 06:16 UTC] tstarling@php.net
Description:
------------
The stream_set_write_buffer() manual entry indicates that fwrite() buffers its output. This is incorrect. For regular files, no buffering is ever done, each fwrite() call leads to a syscall.
This is a performance issue, especially for extensions like the CDB handler in DBA. I have a real-world script that uses almost as much system CPU as user CPU, because each dba_insert() call leads to multiple syscalls.
Test script:
---------------
$f = fopen( '/tmp/blah', 'w' );
stream_set_write_buffer( $f, 8192 );
for ( $i = 0; $i < 10; $i++ ) {
fwrite( $f, 'x' );
}
fclose( $f );
Expected result:
----------------
strace php write-tight-loop.php
...
write(3, "xxxxxxxxxx", 1) = 10
...
Actual result:
--------------
write(3, "x", 1) = 1
write(3, "x", 1) = 1
write(3, "x", 1) = 1
write(3, "x", 1) = 1
write(3, "x", 1) = 1
write(3, "x", 1) = 1
write(3, "x", 1) = 1
write(3, "x", 1) = 1
write(3, "x", 1) = 1
write(3, "x", 1) = 1
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 01:00:01 2025 UTC |
Perhaps this change in plain_wrapper.c would be a good idea: @@ -583,7 +583,13 @@ case PHP_STREAM_OPTION_WRITE_BUFFER: if (data->file == NULL) { - return -1; + char fixed_mode[5]; + php_stream_mode_sanitize_fdopen_fopencookie(stream, fixed_mode); + data->file = fdopen(data->fd, fixed_mode); + if (data->file == NULL) { + return -1; + } + data->fd = -1; } if (ptrparam) I'll have to think a bit more about the implications.