Optimize llformat Even MORE, and clean up some code.
Thanks for the help, Aru!
This commit is contained in:
@@ -36,21 +36,35 @@
|
|||||||
#include "llformat.h"
|
#include "llformat.h"
|
||||||
|
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
|
#include <boost/align/aligned_allocator.hpp>
|
||||||
|
|
||||||
// common used function with va_list argument
|
// common used function with va_list argument
|
||||||
// wrapper for vsnprintf to be called from llformatXXX functions.
|
// wrapper for vsnprintf to be called from llformatXXX functions.
|
||||||
static void va_format(std::string& out, const char *fmt, va_list va)
|
static void va_format(std::string& out, const char *fmt, va_list& va)
|
||||||
{
|
{
|
||||||
constexpr auto smallsize = 1024;
|
typedef typename std::vector<char, boost::alignment::aligned_allocator<char, 1>> vec_t;
|
||||||
std::vector<char> charvector(smallsize); // Evolves into charveleon
|
static thread_local vec_t charvector(1024); // Evolves into charveleon
|
||||||
|
#define vsnprintf(va) std::vsnprintf(charvector.data(), charvector.capacity(), fmt, va)
|
||||||
|
#ifdef LL_WINDOWS // We don't have to copy on windows
|
||||||
|
#define va2 va
|
||||||
|
#else
|
||||||
va_list va2;
|
va_list va2;
|
||||||
va_copy(va2, va);
|
va_copy(va2, va);
|
||||||
const auto size = std::vsnprintf(charvector.data(), charvector.size(), fmt, va);
|
#endif
|
||||||
if (size >= smallsize)
|
const auto smallsize(charvector.capacity());
|
||||||
|
const auto size = vsnprintf(va);
|
||||||
|
if (size < 0)
|
||||||
|
{
|
||||||
|
LL_ERRS() << "Encoding failed, code " << size << ". String hint:" << out << '/' << fmt << LL_ENDL;
|
||||||
|
}
|
||||||
|
else if (static_cast<vec_t::size_type>(size) >= smallsize) // Resize if we need more space
|
||||||
{
|
{
|
||||||
charvector.resize(1+size); // Use the String Stone
|
charvector.resize(1+size); // Use the String Stone
|
||||||
std::vsnprintf(charvector.data(), charvector.size(), fmt, va2);
|
vsnprintf(va2);
|
||||||
}
|
}
|
||||||
|
#ifndef LL_WINDOWS
|
||||||
|
va_end(va2);
|
||||||
|
#endif
|
||||||
out.assign(charvector.data());
|
out.assign(charvector.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user