upd: [imp] optimize the size of the fmt context

This commit is contained in:
mutouyun 2022-12-06 23:14:23 +08:00
parent 217bde2674
commit 9f51c017e7
3 changed files with 44 additions and 36 deletions

View File

@ -7,7 +7,6 @@
#pragma once #pragma once
#include <string> #include <string>
#include <array>
#include <cstddef> #include <cstddef>
#include "libimp/def.h" #include "libimp/def.h"
@ -18,14 +17,10 @@
LIBIMP_NAMESPACE_BEG_ LIBIMP_NAMESPACE_BEG_
constexpr std::size_t fmt_context_aligned_size = 512U;
/** /**
* @brief The context of fmt. * @brief The context of fmt.
*/ */
class LIBIMP_EXPORT fmt_context { class LIBIMP_EXPORT fmt_context {
std::array<char, fmt_context_aligned_size> sbuf_;
std::string &joined_; std::string &joined_;
std::size_t offset_; std::size_t offset_;

View File

@ -19,8 +19,18 @@ LIBIMP_NAMESPACE_BEG_
*/ */
namespace { namespace {
constexpr std::size_t roundup(std::size_t sz) noexcept { struct sfmt_policy {
return (sz & ~(fmt_context_aligned_size - 1)) + fmt_context_aligned_size; constexpr static std::size_t aligned_size = 32U;
};
struct sbuf_policy {
constexpr static std::size_t aligned_size = 2048U;
};
template <typename Policy = sfmt_policy>
span<char> local_fmt_sbuf() noexcept {
thread_local std::array<char, Policy::aligned_size> sbuf;
return sbuf;
} }
span<char const> normalize(span<char const> const &a) { span<char const> normalize(span<char const> const &a) {
@ -52,19 +62,14 @@ span<char> sbuf_cat(span<char> const &sbuf, std::initializer_list<span<char cons
return sbuf.first(sz); return sbuf.first(sz);
} }
span<char> local_fmt_str() noexcept {
thread_local std::array<char, fmt_context_aligned_size> sbuf;
return sbuf;
}
char const *as_cstr(span<char const> const &a) { char const *as_cstr(span<char const> const &a) {
if (a.empty()) return ""; if (a.empty()) return "";
if (a.back() == '\0') return a.data(); if (a.back() == '\0') return a.data();
return sbuf_cpy(local_fmt_str(), a).data(); return sbuf_cpy(local_fmt_sbuf(), a).data();
} }
span<char> fmt_of(span<char const> const &fstr, span<char const> const &s) { span<char> fmt_of(span<char const> const &fstr, span<char const> const &s) {
return sbuf_cat(local_fmt_str(), {"%", fstr, s}); return sbuf_cat(local_fmt_sbuf(), {"%", fstr, s});
} }
span<char> fmt_of_unsigned(span<char const> fstr, span<char const> const &l) { span<char> fmt_of_unsigned(span<char const> fstr, span<char const> const &l) {
@ -76,8 +81,8 @@ span<char> fmt_of_unsigned(span<char const> fstr, span<char const> const &l) {
case 'o': case 'o':
case 'x': case 'x':
case 'X': case 'X':
case 'u': return sbuf_cat(local_fmt_str(), {"%", fstr.first(fstr.size() - 1), l, fstr.last(1)}); case 'u': return sbuf_cat(local_fmt_sbuf(), {"%", fstr.first(fstr.size() - 1), l, fstr.last(1)});
default : return sbuf_cat(local_fmt_str(), {"%", fstr, l, "u"}); default : return sbuf_cat(local_fmt_sbuf(), {"%", fstr, l, "u"});
} }
} }
@ -91,7 +96,7 @@ span<char> fmt_of_signed(span<char const> fstr, span<char const> const &l) {
case 'x': case 'x':
case 'X': case 'X':
case 'u': return fmt_of_unsigned(fstr, l); case 'u': return fmt_of_unsigned(fstr, l);
default : return sbuf_cat(local_fmt_str(), {"%", fstr, l, "d"}); default : return sbuf_cat(local_fmt_sbuf(), {"%", fstr, l, "d"});
} }
} }
@ -104,8 +109,8 @@ span<char> fmt_of_float(span<char const> fstr, span<char const> const &l) {
case 'e': case 'e':
case 'E': case 'E':
case 'g': case 'g':
case 'G': return sbuf_cat(local_fmt_str(), {"%", fstr.first(fstr.size() - 1), l, fstr.last(1)}); case 'G': return sbuf_cat(local_fmt_sbuf(), {"%", fstr.first(fstr.size() - 1), l, fstr.last(1)});
default : return sbuf_cat(local_fmt_str(), {"%", fstr, l, "f"}); default : return sbuf_cat(local_fmt_sbuf(), {"%", fstr, l, "f"});
} }
} }
@ -137,6 +142,10 @@ bool sprintf(fmt_context &ctx, F fop, span<char const> const &fstr, span<char co
} }
} }
span<char> fmt_context_sbuf() noexcept {
return local_fmt_sbuf<sbuf_policy>();
}
} // namespace } // namespace
/// @brief The context of fmt. /// @brief The context of fmt.
@ -146,7 +155,9 @@ fmt_context::fmt_context(std::string &j) noexcept
, offset_(0) {} , offset_(0) {}
std::size_t fmt_context::capacity() noexcept { std::size_t fmt_context::capacity() noexcept {
return (offset_ < sbuf_.size()) ? sbuf_.size() : joined_.size(); return (offset_ < fmt_context_sbuf().size())
? fmt_context_sbuf().size()
: joined_.size();
} }
void fmt_context::reset() noexcept { void fmt_context::reset() noexcept {
@ -155,8 +166,8 @@ void fmt_context::reset() noexcept {
bool fmt_context::finish() noexcept { bool fmt_context::finish() noexcept {
LIBIMP_TRY { LIBIMP_TRY {
if (offset_ < sbuf_.size()) { if (offset_ < fmt_context_sbuf().size()) {
joined_.assign(sbuf_.data(), offset_); joined_.assign(fmt_context_sbuf().data(), offset_);
} else { } else {
joined_.resize(offset_); joined_.resize(offset_);
} }
@ -167,13 +178,18 @@ bool fmt_context::finish() noexcept {
} }
span<char> fmt_context::buffer(std::size_t sz) noexcept { span<char> fmt_context::buffer(std::size_t sz) noexcept {
auto roundup = [](std::size_t sz) noexcept {
constexpr std::size_t fmt_context_aligned_size = 512U;
return (sz & ~(fmt_context_aligned_size - 1)) + fmt_context_aligned_size;
};
auto sbuf = fmt_context_sbuf();
LIBIMP_TRY { LIBIMP_TRY {
if (offset_ < sbuf_.size()) { if (offset_ < sbuf.size()) {
if ((offset_ + sz) < sbuf_.size()) { if ((offset_ + sz) < sbuf.size()) {
return make_span(sbuf_).subspan(offset_); return sbuf.subspan(offset_);
} else { } else {
/// @remark switch the cache to std::string /// @remark switch the cache to std::string
joined_.assign(sbuf_.data(), offset_); joined_.assign(sbuf.data(), offset_);
joined_.resize(roundup(offset_ + sz)); joined_.resize(roundup(offset_ + sz));
} }
} else if ((offset_ + sz) >= joined_.size()) { } else if ((offset_ + sz) >= joined_.size()) {

View File

@ -92,23 +92,20 @@ TEST(fmt, to_string) {
} }
TEST(fmt, fmt) { TEST(fmt, fmt) {
char const txt[] = "hello world.";
/// @brief hello world /// @brief hello world
auto s = imp::fmt("hello", " ", "world", "."); auto s = imp::fmt("hello", " ", "world", ".");
EXPECT_EQ(s, "hello world."); EXPECT_EQ(s, txt);
/// @brief chrono /// @brief chrono
std::cout << imp::fmt('[', std::chrono::system_clock::now(), "] ", s) << "\n"; std::cout << imp::fmt('[', std::chrono::system_clock::now(), "] ", s) << "\n";
/// @brief long string /// @brief long string
s = imp::fmt(imp::spec("1024")("hello world.")); s = imp::fmt(imp::spec("4096")(txt));
EXPECT_EQ(s, " " std::string test(4096, ' ');
" " std::memcpy(&test[test.size() - sizeof(txt) + 1], txt, sizeof(txt) - 1);
" " EXPECT_EQ(s, test);
" "
" "
" "
" "
" hello world.");
} }
namespace { namespace {