diff --git a/server/TracyFileRead.hpp b/server/TracyFileRead.hpp index b7758a27..234b07a6 100755 --- a/server/TracyFileRead.hpp +++ b/server/TracyFileRead.hpp @@ -4,6 +4,8 @@ #include #include +#include "../common/tracy_lz4.hpp" + namespace tracy { @@ -19,6 +21,7 @@ public: ~FileRead() { fclose( m_file ); + LZ4_freeStreamDecode( m_stream ); } void Read( void* ptr, size_t size ) @@ -28,12 +31,17 @@ public: { if( m_offset == BufSize ) { - fread( m_buf, 1, BufSize, m_file ); + m_active = 1 - m_active; m_offset = 0; + uint32_t sz; + fread( &sz, 1, sizeof( sz ), m_file ); + char lz4[LZ4Size]; + fread( lz4, 1, sz, m_file ); + LZ4_decompress_safe_continue( m_stream, lz4, m_buf[m_active], sz, BufSize ); } const auto sz = std::min( size, BufSize - m_offset ); - memcpy( dst, m_buf + m_offset, sz ); + memcpy( dst, m_buf[m_active] + m_offset, sz ); m_offset += sz; dst += sz; size -= sz; @@ -42,15 +50,20 @@ public: private: FileRead( FILE* f ) - : m_file( f ) + : m_stream( LZ4_createStreamDecode() ) + , m_file( f ) , m_offset( BufSize ) + , m_active( 1 ) {} enum { BufSize = 64 * 1024 }; + enum { LZ4Size = LZ4_COMPRESSBOUND( BufSize ) }; + LZ4_streamDecode_t* m_stream; FILE* m_file; - char m_buf[BufSize]; + char m_buf[2][BufSize]; size_t m_offset; + uint8_t m_active; }; } diff --git a/server/TracyFileWrite.hpp b/server/TracyFileWrite.hpp index 42ae1cf6..5f1ea39b 100755 --- a/server/TracyFileWrite.hpp +++ b/server/TracyFileWrite.hpp @@ -4,6 +4,8 @@ #include #include +#include "../common/tracy_lz4.hpp" + namespace tracy { @@ -20,16 +22,17 @@ public: { if( m_offset > 0 ) { - fwrite( m_buf, 1, m_offset, m_file ); + WriteLz4Block(); } fclose( m_file ); + LZ4_freeStream( m_stream ); } void Write( const void* ptr, size_t size ) { if( m_offset + size <= BufSize ) { - memcpy( m_buf + m_offset, ptr, size ); + memcpy( m_buf[m_active] + m_offset, ptr, size ); m_offset += size; } else @@ -38,15 +41,14 @@ public: while( size > 0 ) { const auto sz = std::min( size, BufSize - m_offset ); - memcpy( m_buf + m_offset, src, sz ); + memcpy( m_buf[m_active] + m_offset, src, sz ); m_offset += sz; src += sz; size -= sz; if( m_offset == BufSize ) { - fwrite( m_buf, 1, BufSize, m_file ); - m_offset = 0; + WriteLz4Block(); } } } @@ -54,15 +56,30 @@ public: private: FileWrite( FILE* f ) - : m_file( f ) + : m_stream( LZ4_createStream() ) + , m_file( f ) , m_offset( 0 ) + , m_active( 0 ) {} - enum { BufSize = 64 * 1024 }; + void WriteLz4Block() + { + char lz4[LZ4Size]; + const uint32_t sz = LZ4_compress_fast_continue( m_stream, m_buf[m_active], lz4, m_offset, LZ4Size, 1 ); + fwrite( &sz, 1, sizeof( sz ), m_file ); + fwrite( lz4, 1, sz, m_file ); + m_offset = 0; + m_active = 1 - m_active; + } + enum { BufSize = 64 * 1024 }; + enum { LZ4Size = LZ4_COMPRESSBOUND( BufSize ) }; + + LZ4_stream_t* m_stream; FILE* m_file; - char m_buf[BufSize]; + char m_buf[2][BufSize]; size_t m_offset; + uint8_t m_active; }; }