LCOV - code coverage report
Current view: top level - source/ps - CStrIntern.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 32 35 91.4 %
Date: 2026-12-03 01:19:39 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "CStrIntern.h"
      21             : 
      22             : #include "lib/debug.h"
      23             : #include "lib/fnv_hash.h"
      24             : #include "ps/ThreadUtil.h"
      25             : 
      26             : #include <cstring>
      27             : #include <unordered_map>
      28             : #include <utility>
      29             : 
      30           0 : class CStrInternInternals
      31             : {
      32             : public:
      33         182 :     CStrInternInternals(const char* str, size_t len)
      34         182 :         : data(str, str+len), hash(fnv_hash(str, len))
      35             :     {
      36             : //      LOGWARNING("New interned string '{}'", data);
      37         182 :     }
      38             : 
      39         364 :     CStrInternInternals(const CStrInternInternals& b) = default;
      40             : 
      41             :     bool operator==(const CStrInternInternals& b) const
      42             :     {
      43             :         // Compare hash first for quick rejection of inequal strings
      44             :         return (hash == b.hash && data == b.data);
      45             :     }
      46             : 
      47             :     const std::string data;
      48             :     const std::uint32_t hash; // fnv_hash of data
      49             : 
      50             : private:
      51             :     CStrInternInternals& operator=(const CStrInternInternals&);
      52             : };
      53             : 
      54             : // Interned strings are stored in a hash table, indexed by string:
      55             : 
      56             : using StringsKey = std::string;
      57             : 
      58             : struct StringsKeyHash
      59             : {
      60        3551 :     size_t operator()(const StringsKey& key) const
      61             :     {
      62        3551 :         return fnv_hash(key.c_str(), key.length());
      63             :     }
      64             : };
      65             : 
      66             : // To avoid std::string memory allocations when GetString does lookups in the
      67             : // hash table of interned strings, we make use of std::unordered_map's ability
      68             : // to do lookups with a functionally equivalent proxy object:
      69             : 
      70             : struct StringsKeyProxy
      71             : {
      72             :     const char* str;
      73             :     size_t len;
      74             : };
      75             : 
      76             : struct StringsKeyProxyHash
      77             : {
      78             :     size_t operator()(const StringsKeyProxy& key) const
      79             :     {
      80             :         return fnv_hash(key.str, key.len);
      81             :     }
      82             : };
      83             : 
      84             : struct StringsKeyProxyEq
      85             : {
      86             :     bool operator()(const StringsKeyProxy& proxy, const StringsKey& key) const
      87             :     {
      88             :         return (proxy.len == key.length() && memcmp(proxy.str, key.c_str(), proxy.len) == 0);
      89             :     }
      90             : };
      91             : 
      92             : namespace
      93             : {
      94             : std::unordered_map<StringsKey, CStrInternInternals, StringsKeyHash> g_Strings;
      95             : 
      96        3370 : CStrInternInternals* GetString(const char* str, size_t len)
      97             : {
      98             :     // g_Strings is not thread-safe, so complain if anyone is using this
      99             :     // type in non-main threads. (If that's desired, g_Strings should be changed
     100             :     // to be thread-safe, preferably without sacrificing performance.)
     101        3370 :     ENSURE(Threading::IsMainThread());
     102             : 
     103        6740 :     const auto it = g_Strings.find(str);
     104             : 
     105        3370 :     if (it != g_Strings.end())
     106        3188 :         return &it->second;
     107             : 
     108         464 :     return &g_Strings.insert({str, {str, len}}).first->second;
     109             : }
     110             : }
     111             : 
     112             : #define X(id) CStrIntern str_##id(#id);
     113             : #define X2(id, str) CStrIntern str_##id(str);
     114             : #include "CStrInternStatic.h"
     115             : #undef X
     116             : #undef X2
     117             : 
     118        5132 : CStrIntern::CStrIntern()
     119             : {
     120        5132 :     *this = str__emptystring;
     121        5132 : }
     122             : 
     123         210 : CStrIntern::CStrIntern(const char* str)
     124             : {
     125         210 :     m = GetString(str, strlen(str));
     126         210 : }
     127             : 
     128         518 : CStrIntern::CStrIntern(const std::string& str)
     129             : {
     130         518 :     m = GetString(str.c_str(), str.length());
     131         518 : }
     132             : 
     133        2642 : CStrIntern::CStrIntern(const std::string_view str)
     134             : {
     135        2642 :     m = GetString(str.data(), str.length());
     136        2642 : }
     137             : 
     138        2900 : std::uint32_t CStrIntern::GetHash() const
     139             : {
     140        2900 :     return m->hash;
     141             : }
     142             : 
     143          12 : const char* CStrIntern::c_str() const
     144             : {
     145          12 :     return m->data.c_str();
     146             : }
     147             : 
     148           0 : size_t CStrIntern::length() const
     149             : {
     150           0 :     return m->data.length();
     151             : }
     152             : 
     153        2642 : bool CStrIntern::empty() const
     154             : {
     155        2642 :     return m->data.empty();
     156             : }
     157             : 
     158        2918 : const std::string& CStrIntern::string() const
     159             : {
     160        2918 :     return m->data;
     161             : }

Generated by: LCOV version 1.16