[triplet] Use pointer to instance-controlled strings and cache hashcode
MODIFY
include/triplet.h
include/triplet.h
namespace vcpkg
{
+ struct TripletInstance;
+
struct Triplet
{
+ public:
+ constexpr Triplet() : m_instance(&default_instance) {}
+
static Triplet from_canonical_name(const std::string& triplet_as_string);
static const Triplet X86_WINDOWS;
@@ -16,28 +21,23 @@ namespace vcpkg
const std::string& canonical_name() const;
const std::string& to_string() const;
+ size_t hash_code() const;
+
+ bool operator==(const Triplet& other) const;
private:
- std::string m_canonical_name;
- };
+ static const TripletInstance default_instance;
+
+ constexpr Triplet(const TripletInstance* ptr) : m_instance(ptr) {}
- bool operator==(const Triplet& left, const Triplet& right);
+ const TripletInstance* m_instance;
+ };
bool operator!=(const Triplet& left, const Triplet& right);
}
-namespace std
-{
template<>
- struct hash<vcpkg::Triplet>
- {
- size_t operator()(const vcpkg::Triplet& t) const
+struct std::hash<vcpkg::Triplet>
{
- std::hash<std::string> hasher;
- size_t hash = 17;
- hash = hash * 31 + hasher(t.canonical_name());
- return hash;
- }
+ size_t operator()(const vcpkg::Triplet& t) const { return t.hash_code(); }
};
-
-} // namespace std

MODIFY
src/triplet.cpp
src/triplet.cpp
namespace vcpkg
{
+ struct TripletInstance
+ {
+ TripletInstance(std::string&& s) : value(std::move(s)), hash(std::hash<std::string>()(value)) {}
+
+ const std::string value;
+ const size_t hash = 0;
+
+ bool operator==(const TripletInstance& o) const { return o.value == value; }
+ };
+ const TripletInstance Triplet::default_instance({});
+}
+
+template<>
+struct std::hash<vcpkg::TripletInstance>
+{
+ size_t operator()(const vcpkg::TripletInstance& t) const { return t.hash; }
+};
+
+namespace vcpkg
+{
+ static std::unordered_set<TripletInstance> g_triplet_instances;
+
const Triplet Triplet::X86_WINDOWS = from_canonical_name("x86-windows");
const Triplet Triplet::X64_WINDOWS = from_canonical_name("x64-windows");
const Triplet Triplet::X86_UWP = from_canonical_name("x86-uwp");
const Triplet Triplet::X64_UWP = from_canonical_name("x64-uwp");
const Triplet Triplet::ARM_UWP = from_canonical_name("arm-uwp");
- bool operator==(const Triplet& left, const Triplet& right)
- {
- return left.canonical_name() == right.canonical_name();
- }
+ bool Triplet::operator==(const Triplet& other) const { return this->m_instance == other.m_instance; }
bool operator!=(const Triplet& left, const Triplet& right) { return !(left == right); }
Triplet Triplet::from_canonical_name(const std::string& triplet_as_string)
{
- const std::string s(Strings::ascii_to_lowercase(triplet_as_string));
+ std::string s(Strings::ascii_to_lowercase(triplet_as_string));
auto it = std::find(s.cbegin(), s.cend(), '-');
Checks::check_exit(VCPKG_LINE_INFO, it != s.cend(), "Invalid triplet: %s", triplet_as_string);
- Triplet t;
- t.m_canonical_name = s;
- return t;
+ auto p = g_triplet_instances.emplace(std::move(s));
+ return &*p.first;
}
- const std::string& Triplet::canonical_name() const { return this->m_canonical_name; }
+ const std::string& Triplet::canonical_name() const { return this->m_instance->value; }
- const std::string& Triplet::to_string() const { return this->m_canonical_name; }
+ const std::string& Triplet::to_string() const { return this->canonical_name(); }
+ size_t Triplet::hash_code() const { return m_instance->hash; }
}

[triplet] Use pointer to instance-controlled strings and cache hashcode