1
0
forked from 0ad/0ad

Don't use std::shared_ptr in CStrIntern

Now there is less allocation and reference counting.
This commit is contained in:
phosit 2024-08-24 12:09:45 +02:00 committed by phosit
parent 45984c905b
commit 62c0080e1b

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2021 Wildfire Games.
/* Copyright (C) 2024 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -85,30 +85,31 @@ struct StringsKeyProxyEq
}
};
static std::unordered_map<StringsKey, std::shared_ptr<CStrInternInternals>, StringsKeyHash> g_Strings;
namespace
{
std::unordered_map<StringsKey, CStrInternInternals, StringsKeyHash> g_Strings;
#define X(id) CStrIntern str_##id(#id);
#define X2(id, str) CStrIntern str_##id(str);
#include "CStrInternStatic.h"
#undef X
#undef X2
static CStrInternInternals* GetString(const char* str, size_t len)
CStrInternInternals* GetString(const char* str, size_t len)
{
// g_Strings is not thread-safe, so complain if anyone is using this
// type in non-main threads. (If that's desired, g_Strings should be changed
// to be thread-safe, preferably without sacrificing performance.)
ENSURE(Threading::IsMainThread());
std::unordered_map<StringsKey, std::shared_ptr<CStrInternInternals> >::iterator it = g_Strings.find(str);
const auto it = g_Strings.find(str);
if (it != g_Strings.end())
return it->second.get();
return &it->second;
std::shared_ptr<CStrInternInternals> internals = std::make_shared<CStrInternInternals>(str, len);
g_Strings.insert(std::make_pair(internals->data, internals));
return internals.get();
return &g_Strings.insert({str, {str, len}}).first->second;
}
}
#define X(id) CStrIntern str_##id(#id);
#define X2(id, str) CStrIntern str_##id(str);
#include "CStrInternStatic.h"
#undef X
#undef X2
CStrIntern::CStrIntern()
{