Skip to content

Commit

Permalink
Zero copy (#3327)
Browse files Browse the repository at this point in the history
* Zero copy

* Use private member
  • Loading branch information
twose authored May 19, 2020
1 parent 6d8dfde commit bcf69e8
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions include/coroutine_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ class Socket
return write_buffer;
}

inline void set_zero_copy(bool enable)
{
zero_copy = enable;
}

#ifdef SW_USE_OPENSSL
inline bool is_ssl_enable()
{
Expand Down Expand Up @@ -392,6 +397,8 @@ class Socket
bool shutdown_write = false;
bool closed = false;

bool zero_copy = false;

static void timer_callback(swTimer *timer, swTimer_node *tnode);
static int readable_event_callback(swReactor *reactor, swEvent *event);
static int writable_event_callback(swReactor *reactor, swEvent *event);
Expand Down
2 changes: 1 addition & 1 deletion src/coroutine/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ bool Socket::wait_event(const enum swEvent_type event, const void **__buf, size_
}
else // if (event == SW_EVENT_WRITE)
{
if (sw_unlikely(__n > 0 && *__buf != get_write_buffer()->str))
if (sw_unlikely(!zero_copy && __n > 0 && *__buf != get_write_buffer()->str))
{
swString_clear(write_buffer);
if (swString_append_ptr(write_buffer, (const char *) *__buf, __n) != SW_OK)
Expand Down
2 changes: 2 additions & 0 deletions swoole_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ static Socket* client_coro_new(zval *zobject, int port)

zend_update_property_long(Z_OBJCE_P(zobject), zobject, ZEND_STRL("fd"), cli->get_fd());

cli->set_zero_copy(true);

#ifdef SW_USE_OPENSSL
if (type & SW_SOCK_SSL)
{
Expand Down
1 change: 1 addition & 0 deletions swoole_http2_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ bool http2_client::connect()
client = nullptr;
return false;
}
client->set_zero_copy(true);
#ifdef SW_USE_OPENSSL
client->open_ssl = ssl;
#endif
Expand Down
3 changes: 2 additions & 1 deletion swoole_mysql_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ bool mysql_client::connect(std::string host, uint16_t port, bool ssl)
socket = nullptr;
return false;
}
socket->set_zero_copy(true);
#ifdef SW_USE_OPENSSL
socket->open_ssl = ssl;
#endif
Expand Down Expand Up @@ -1415,7 +1416,7 @@ void mysql_statement::send_execute_request(zval *return_value, zval *params)
RETURN_FALSE;
}

swString *buffer = SwooleTG.buffer_stack;
swString *buffer = client->socket->get_write_buffer();
char *p = buffer->str;

memset(p, 0, 5);
Expand Down
2 changes: 2 additions & 0 deletions swoole_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,8 @@ static php_stream *socket_create(
return NULL;
}

sock->set_zero_copy(true);

abstract = (php_swoole_netstream_data_t*) ecalloc(1, sizeof(*abstract));
abstract->socket = sock;
abstract->stream.socket = sock->get_fd();
Expand Down
1 change: 1 addition & 0 deletions swoole_socket_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ static sw_inline void swoole_socket_coro_sync_properties(zval *zobject, socket_c

static void sw_inline php_swoole_init_socket(zval *zobject, socket_coro *sock)
{
sock->socket->set_zero_copy(true);
zend_update_property_long(swoole_socket_coro_ce, zobject, ZEND_STRL("fd"), sock->socket->get_fd());
}

Expand Down

0 comments on commit bcf69e8

Please sign in to comment.