Skip to content

Commit

Permalink
Avoid useless stream buffer copying and workaround with chunk_size ma…
Browse files Browse the repository at this point in the history
…nipulation.
  • Loading branch information
dstogov committed Jun 13, 2018
1 parent bc56bb8 commit fcfa006
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 2 additions & 3 deletions ext/mysqlnd/mysqlnd_vio.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ MYSQLND_METHOD(mysqlnd_vio, network_read)(MYSQLND_VIO * const vio, zend_uchar *
{
enum_func_status return_value = PASS;
php_stream * net_stream = vio->data->m.get_stream(vio);
size_t old_chunk_size = net_stream->chunk_size;
size_t to_read = count, ret;
zend_uchar * p = buffer;

DBG_ENTER("mysqlnd_vio::network_read");
DBG_INF_FMT("count="MYSQLND_SZ_T_SPEC, count);

net_stream->chunk_size = MIN(to_read, vio->data->options.net_read_buffer_size);
while (to_read) {
if (!(ret = php_stream_read(net_stream, (char *) p, to_read))) {
DBG_ERR_FMT("Error while reading header from socket");
Expand All @@ -97,7 +95,6 @@ MYSQLND_METHOD(mysqlnd_vio, network_read)(MYSQLND_VIO * const vio, zend_uchar *
to_read -= ret;
}
MYSQLND_INC_CONN_STATISTIC_W_VALUE(stats, STAT_BYTES_RECEIVED, count - to_read);
net_stream->chunk_size = old_chunk_size;
DBG_RETURN(return_value);
}
/* }}} */
Expand Down Expand Up @@ -265,6 +262,8 @@ MYSQLND_METHOD(mysqlnd_vio, post_connect_set_opt)(MYSQLND_VIO * const vio, const
/* TCP -> Set SO_KEEPALIVE */
mysqlnd_set_sock_keepalive(net_stream);
}

net_stream->chunk_size = vio->data->options.net_read_buffer_size;
}

DBG_VOID_RETURN;
Expand Down
4 changes: 3 additions & 1 deletion main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,9 @@ PHPAPI void _php_stream_fill_read_buffer(php_stream *stream, size_t size)

/* reduce buffer memory consumption if possible, to avoid a realloc */
if (stream->readbuf && stream->readbuflen - stream->writepos < stream->chunk_size) {
memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->readbuflen - stream->readpos);
if (stream->writepos > stream->readpos) {
memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos);
}
stream->writepos -= stream->readpos;
stream->readpos = 0;
}
Expand Down

0 comments on commit fcfa006

Please sign in to comment.