From 2882cb728fdae28f69659e24d9ca2b87bd9a484d Mon Sep 17 00:00:00 2001 From: Lirusaito Date: Sat, 23 Feb 2019 04:08:55 -0500 Subject: [PATCH] Optimize llformat Even MORE, and clean up some code. Thanks for the help, Aru! --- indra/llcommon/llformat.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/indra/llcommon/llformat.cpp b/indra/llcommon/llformat.cpp index 319afda94..01b02191f 100644 --- a/indra/llcommon/llformat.cpp +++ b/indra/llcommon/llformat.cpp @@ -36,21 +36,35 @@ #include "llformat.h" #include +#include // 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 charvector(smallsize); // Evolves into charveleon + typedef typename std::vector> 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(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()); }