From be005b63e3cecec614b0aa9d6e29df6603e8fb7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Dee=20=28J=C5=8Dshin=29?= Date: Tue, 18 Jun 2024 16:12:58 -0700 Subject: [PATCH] Move isbig() into header Takes 1ns off most benchmarks where the destructor is frequently called. --- ctl/string.cc | 21 ++++++++++----------- ctl/string.h | 9 ++++++++- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/ctl/string.cc b/ctl/string.cc index a7552e86e..2879d2fcd 100644 --- a/ctl/string.cc +++ b/ctl/string.cc @@ -23,20 +23,19 @@ namespace ctl { -string::~string() noexcept +void +string::destroy_big() noexcept { - if (isbig()) { - auto* b = big(); - if (b->n) { - if (b->n >= b->c) - __builtin_trap(); - if (b->p[b->n]) - __builtin_trap(); - } - if (b->c && !b->p) + auto* b = big(); + if (b->n) { + if (b->n >= b->c) + __builtin_trap(); + if (b->p[b->n]) __builtin_trap(); - free(b->p); } + if (b->c && !b->p) + __builtin_trap(); + free(b->p); } string::string(const char* s) noexcept : string() diff --git a/ctl/string.h b/ctl/string.h index 42fc14dc3..4776568dd 100644 --- a/ctl/string.h +++ b/ctl/string.h @@ -48,7 +48,12 @@ class string using const_iterator = const char*; static constexpr size_t npos = -1; - ~string() /* noexcept */; + ~string() /* noexcept */ + { + if (isbig()) + destroy_big(); + } + string(string_view) noexcept; string(const char*) noexcept; string(const string&) noexcept; @@ -277,6 +282,8 @@ class string } private: + void destroy_big() noexcept; + inline bool isbig() const noexcept { return *(blob + __::sso_max) & 0x80;