Optimize llformat Even MORE, and clean up some code.

Thanks for the help, Aru!
This commit is contained in:
Lirusaito
2019-02-23 04:08:55 -05:00
parent f9e3afaad2
commit 2882cb728f

View File

@@ -36,21 +36,35 @@
#include "llformat.h"
#include <cstdarg>
#include <boost/align/aligned_allocator.hpp>
// common used function with va_list argument
// 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;
std::vector<char> charvector(smallsize); // Evolves into charveleon
typedef typename std::vector<char, boost::alignment::aligned_allocator<char, 1>> vec_t;
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_copy(va2, va);
const auto size = std::vsnprintf(charvector.data(), charvector.size(), fmt, va);
if (size >= smallsize)
#endif
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
std::vsnprintf(charvector.data(), charvector.size(), fmt, va2);
vsnprintf(va2);
}
#ifndef LL_WINDOWS
va_end(va2);
#endif
out.assign(charvector.data());
}