1
0
mirror of https://github.com/wolfpld/tracy synced 2025-04-29 12:23:53 +00:00

Buffer data from recv() calls.

This reduces cost of socket reads measured in a test run from 47ms to
8.7ms.
This commit is contained in:
Bartosz Taudul 2018-07-15 19:34:47 +02:00
parent c6ea032de3
commit ea4470b26e
2 changed files with 48 additions and 2 deletions

View File

@ -1,3 +1,4 @@
#include <algorithm>
#include <assert.h> #include <assert.h>
#include <new> #include <new>
#include <stdio.h> #include <stdio.h>
@ -47,6 +48,9 @@ static __wsinit InitWinSock()
Socket::Socket() Socket::Socket()
: m_sock( -1 ) : m_sock( -1 )
, m_buf( (char*)tracy_malloc( BufSize ) )
, m_bufPtr( nullptr )
, m_bufLeft( 0 )
{ {
#ifdef _MSC_VER #ifdef _MSC_VER
InitWinSock(); InitWinSock();
@ -55,11 +59,15 @@ Socket::Socket()
Socket::Socket( int sock ) Socket::Socket( int sock )
: m_sock( sock ) : m_sock( sock )
, m_buf( (char*)tracy_malloc( BufSize ) )
, m_bufPtr( nullptr )
, m_bufLeft( 0 )
{ {
} }
Socket::~Socket() Socket::~Socket()
{ {
tracy_free( m_buf );
if( m_sock != -1 ) if( m_sock != -1 )
{ {
Close(); Close();
@ -130,6 +138,36 @@ int Socket::Send( const void* _buf, int len )
return int( buf - start ); return int( buf - start );
} }
int Socket::RecvBuffered( void* buf, int len, const timeval* tv )
{
if( len <= m_bufLeft )
{
memcpy( buf, m_bufPtr, len );
m_bufPtr += len;
m_bufLeft -= len;
return len;
}
if( m_bufLeft > 0 )
{
memcpy( buf, m_bufPtr, m_bufLeft );
const auto ret = m_bufLeft;
m_bufLeft = 0;
return ret;
}
if( len >= BufSize ) return Recv( buf, len, tv );
m_bufLeft = Recv( m_buf, BufSize, tv );
if( m_bufLeft <= 0 ) return m_bufLeft;
const auto sz = std::min( len, m_bufLeft );
memcpy( buf, m_buf, sz );
m_bufPtr = m_buf + sz;
m_bufLeft -= sz;
return sz;
}
int Socket::Recv( void* _buf, int len, const timeval* tv ) int Socket::Recv( void* _buf, int len, const timeval* tv )
{ {
auto buf = (char*)_buf; auto buf = (char*)_buf;
@ -161,7 +199,7 @@ bool Socket::Read( void* _buf, int len, const timeval* tv, std::function<bool()>
while( len > 0 ) while( len > 0 )
{ {
if( exitCb() ) return false; if( exitCb() ) return false;
const auto sz = Recv( buf, len, tv ); const auto sz = RecvBuffered( buf, len, tv );
switch( sz ) switch( sz )
{ {
case 0: case 0:

View File

@ -10,6 +10,8 @@ namespace tracy
class Socket class Socket
{ {
enum { BufSize = 128 * 1024 };
public: public:
Socket(); Socket();
Socket( int sock ); Socket( int sock );
@ -19,7 +21,6 @@ public:
void Close(); void Close();
int Send( const void* buf, int len ); int Send( const void* buf, int len );
int Recv( void* buf, int len, const timeval* tv );
bool Read( void* buf, int len, const timeval* tv, std::function<bool()> exitCb ); bool Read( void* buf, int len, const timeval* tv, std::function<bool()> exitCb );
bool HasData(); bool HasData();
@ -30,7 +31,14 @@ public:
Socket& operator=( Socket&& ) = delete; Socket& operator=( Socket&& ) = delete;
private: private:
int RecvBuffered( void* buf, int len, const timeval* tv );
int Recv( void* buf, int len, const timeval* tv );
int m_sock; int m_sock;
char* m_buf;
char* m_bufPtr;
int m_bufLeft;
}; };
class ListenSocket class ListenSocket