WriteValue() no longer uses format() which creates temporary strings, instead if writes directly into allocated space in the target buffer using snprintf().

This commit is contained in:
Stephen Atherton 2018-09-10 00:44:09 -07:00
parent e9c86ee638
commit 309159ea23
2 changed files with 22 additions and 4 deletions

View File

@ -97,20 +97,33 @@ protected:
write(val ? "true" : "false");
}
template<typename T> 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) {

View File

@ -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<T>& toCopy ) : data( toCopy.data ), m_size( toCopy.m_size ), m_capacity( toCopy.m_capacity ) {}
//VectorRef<T>& operator=( const VectorRef<T>& );
@ -690,6 +691,10 @@ public:
return m_capacity;
}
void extendUnsafeNoReallocNoInit(int amount) {
m_size += amount;
}
private:
T* data;
int m_size, m_capacity;