Perform less work if printing integers.

This commit is contained in:
Bartosz Taudul 2020-01-31 02:26:38 +01:00
parent 1475635382
commit 289637384d
2 changed files with 92 additions and 39 deletions

View File

@ -214,44 +214,6 @@ const char* TimeToString( int64_t _ns )
return bufstart; return bufstart;
} }
const char* RealToString( double val )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
*PrintFloat( buf, buf+64, val ) = '\0';
auto ptr = buf;
if( *ptr == '-' ) ptr++;
const auto vbegin = ptr;
while( *ptr != '\0' && *ptr != '.' ) ptr++;
auto end = ptr;
while( *end != '\0' ) end++;
auto sz = end - ptr + 1;
while( ptr - vbegin > 3 )
{
ptr -= 3;
memmove( ptr+1, ptr, sz+3 );
*ptr = ',';
sz += 4;
}
while( *ptr != '\0' && *ptr != '.' ) ptr++;
if( *ptr == '\0' ) return buf;
while( *ptr != '\0' ) ptr++;
ptr--;
while( *ptr == '0' ) ptr--;
if( *ptr != '.' && *ptr != ',' ) ptr++;
*ptr = '\0';
return buf;
}
const char* MemSizeToString( int64_t val ) const char* MemSizeToString( int64_t val )
{ {
enum { Pool = 8 }; enum { Pool = 8 };
@ -328,4 +290,19 @@ const char* MemSizeToString( int64_t val )
return buf; return buf;
} }
namespace detail
{
char* RealToStringGetBuffer()
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
return buf;
}
}
} }

View File

@ -3,13 +3,63 @@
#if ( defined _MSC_VER && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L #if ( defined _MSC_VER && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L
# include <charconv> # include <charconv>
# include <type_traits>
#else #else
# include <stdio.h> # include <stdio.h>
#endif #endif
#include "../common/TracyForceInline.hpp"
namespace tracy namespace tracy
{ {
namespace detail
{
char* RealToStringGetBuffer();
static tracy_force_inline void RealToStringFloating( char* ptr, char* end )
{
if( *ptr == '-' ) ptr++;
const auto vbegin = ptr;
while( *ptr != '\0' && *ptr != '.' ) ptr++;
auto sz = end - ptr + 1;
while( ptr - vbegin > 3 )
{
ptr -= 3;
memmove( ptr+1, ptr, sz+3 );
*ptr = ',';
sz += 4;
}
while( *ptr != '\0' && *ptr != '.' ) ptr++;
if( *ptr == '\0' ) return;
while( *ptr != '\0' ) ptr++;
ptr--;
while( *ptr == '0' ) ptr--;
if( *ptr != '.' && *ptr != ',' ) ptr++;
*ptr = '\0';
}
static tracy_force_inline void RealToStringInteger( char* buf, char* end )
{
if( *buf == '-' ) buf++;
auto ptr = end;
auto sz = 1;
while( ptr - buf > 3 )
{
ptr -= 3;
memmove( ptr+1, ptr, sz+3 );
*ptr = ',';
sz += 4;
}
}
}
template<typename T> template<typename T>
static inline char* PrintFloat( char* begin, char* end, T value, int precision ) static inline char* PrintFloat( char* begin, char* end, T value, int precision )
{ {
@ -30,8 +80,34 @@ static inline char* PrintFloat( char* begin, char* end, T value )
#endif #endif
} }
#if ( defined _MSC_VER && _MSVC_LANG >= 201703L ) || __cplusplus >= 201703L
template<typename T>
static inline const char* RealToString( T val )
{
auto buf = detail::RealToStringGetBuffer();
auto end = std::to_chars( buf, buf+64, val ).ptr;
*end = '\0';
if constexpr ( std::is_integral_v<T> )
{
detail::RealToStringInteger( buf, end );
}
else
{
detail::RealToStringFloating( buf, end );
}
return buf;
}
#else
static inline const char* RealToString( double val )
{
auto buf = detail::RealToStringGetBuffer();
const auto sz = sprintf( buf, "%f", val );
detail::RealToStringFloating( buf, buf+sz );
return buf;
}
#endif
const char* TimeToString( int64_t ns ); const char* TimeToString( int64_t ns );
const char* RealToString( double val );
const char* MemSizeToString( int64_t val ); const char* MemSizeToString( int64_t val );
} }