diff --git a/fdbclient/JsonString.h b/fdbclient/JsonString.h index 30e0ae2a3a..11d2d578ac 100755 --- a/fdbclient/JsonString.h +++ b/fdbclient/JsonString.h @@ -97,20 +97,33 @@ protected: write(val ? "true" : "false"); } + template inline void writeFormat(const char *fmt, const T &val) { + VString &dst = jsonText.back(); + const int limit = 30; + dst.reserve(arena, dst.size() + limit); + int len = snprintf(dst.end(), limit, fmt, val); + if(len > 0 && len < limit) { + dst.extendUnsafeNoReallocNoInit(len); + } + else { + write(format(fmt, val)); + } + } + void writeValue(const int64_t& val) { - write(format("%lld",val)); + writeFormat("%lld", val); } void writeValue(const uint64_t& val) { - write(format("%llu",val)); + writeFormat("%llu", val); } void writeValue(const int& val) { - write(format("%d",val)); + writeFormat("%d", val); } void writeValue(const double& val) { - write(format("%g",val)); + writeFormat("%g", val); } bool shouldEscape(char c) { diff --git a/flow/Arena.h b/flow/Arena.h index 29505e7e64..b776b72289 100644 --- a/flow/Arena.h +++ b/flow/Arena.h @@ -595,6 +595,7 @@ public: } VectorRef( T* data, int size ) : data(data), m_size(size), m_capacity(size) {} + VectorRef( T* data, int size, int capacity ) : data(data), m_size(size), m_capacity(capacity) {} //VectorRef( const VectorRef& toCopy ) : data( toCopy.data ), m_size( toCopy.m_size ), m_capacity( toCopy.m_capacity ) {} //VectorRef& operator=( const VectorRef& ); @@ -690,6 +691,10 @@ public: return m_capacity; } + void extendUnsafeNoReallocNoInit(int amount) { + m_size += amount; + } + private: T* data; int m_size, m_capacity;