From bc9e590727211d803cce7be84c1cbc026179b841 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Mon, 23 Sep 2024 19:19:40 +0200 Subject: gc unused refactor (#165) * optimize IoHash and OId comparisions * refactor filtering of unused references * add attachment filtering to gc --- src/zencore/include/zencore/iohash.h | 8 ++++---- src/zencore/include/zencore/memory.h | 35 +++++++++++++++++++++++++++++++++++ src/zencore/include/zencore/uid.h | 4 +++- src/zencore/iohash.cpp | 6 +++++- 4 files changed, 47 insertions(+), 6 deletions(-) (limited to 'src/zencore') diff --git a/src/zencore/include/zencore/iohash.h b/src/zencore/include/zencore/iohash.h index ff902399e..a8fc9e6c1 100644 --- a/src/zencore/include/zencore/iohash.h +++ b/src/zencore/include/zencore/iohash.h @@ -1,5 +1,4 @@ // Copyright Epic Games, Inc. All Rights Reserved. -// Copyright Epic Games, Inc. All Rights Reserved. #pragma once @@ -55,10 +54,11 @@ struct IoHash StringBuilderBase& ToHexString(StringBuilderBase& outBuilder) const; std::string ToHexString() const; - static const int StringLength = 40; - typedef char String_t[StringLength + 1]; + static constexpr int StringLength = 40; + typedef char String_t[StringLength + 1]; static const IoHash Zero; // Initialized to all zeros + static const IoHash Max; // Initialized to all ones inline auto operator<=>(const IoHash& rhs) const = default; inline bool operator==(const IoHash& rhs) const @@ -75,7 +75,7 @@ struct IoHash return LhsHash[0] != RhsHash[0] || LhsHash[1] != RhsHash[1] || LhsHash[2] != RhsHash[2] || LhsHash[3] != RhsHash[3] || LhsHash[4] != RhsHash[4]; } - inline bool operator<(const IoHash& rhs) const { return memcmp(Hash, rhs.Hash, sizeof Hash) < 0; } + inline bool operator<(const IoHash& rhs) const { return MemCmpFixed(Hash, rhs.Hash) < 0; } struct Hasher { diff --git a/src/zencore/include/zencore/memory.h b/src/zencore/include/zencore/memory.h index 6419252ff..7a893d3ab 100644 --- a/src/zencore/include/zencore/memory.h +++ b/src/zencore/include/zencore/memory.h @@ -402,6 +402,41 @@ MakeMutableMemoryView(void* Data, void* DataEnd) return MutableMemoryView(Data, DataEnd); } +template +inline int +MemCmpFixed(const void* a1, const void* a2) +{ + auto const s1 = reinterpret_cast(a1); + auto const s2 = reinterpret_cast(a2); + auto const diff = *s1 - *s2; + return diff ? diff : MemCmpFixed(s1 + 1, s2 + 1); +} + +template<> +inline int +MemCmpFixed<0>(const void*, const void*) +{ + return 0; +} + +template +inline int +MemCmpFixed(const void* a1, const void* a2) +{ + ZEN_ASSERT_SLOW((uintptr_t(a1) & (sizeof(EQTYPE) - 1)) == 0); + ZEN_ASSERT_SLOW((uintptr_t(a2) & (sizeof(EQTYPE) - 1)) == 0); + auto const s1 = reinterpret_cast(a1); + auto const s2 = reinterpret_cast(a2); + return (*s1 != *s2) ? MemCmpFixed(s1, s2) : MemCmpFixed(s1 + 1, s2 + 1); +} + +template<> +inline int +MemCmpFixed<0, uint32_t>(const void*, const void*) +{ + return 0; +} + void memory_forcelink(); // internal } // namespace zen diff --git a/src/zencore/include/zencore/uid.h b/src/zencore/include/zencore/uid.h index f8b1ccf98..08a335392 100644 --- a/src/zencore/include/zencore/uid.h +++ b/src/zencore/include/zencore/uid.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include @@ -67,7 +68,8 @@ struct Oid std::string ToString() const; [[nodiscard]] static Oid FromMemory(const void* Ptr); - auto operator<=>(const Oid& rhs) const = default; + auto operator<=>(const Oid& rhs) const = default; + inline bool operator<(const Oid& rhs) const { return MemCmpFixed(OidBits, rhs.OidBits) < 0; } [[nodiscard]] inline explicit operator bool() const { return *this != Zero; } static const Oid Zero; // Min (can be used to signify a "null" value, or for open range queries) diff --git a/src/zencore/iohash.cpp b/src/zencore/iohash.cpp index 1bf2c033d..8f3f8da26 100644 --- a/src/zencore/iohash.cpp +++ b/src/zencore/iohash.cpp @@ -12,7 +12,11 @@ namespace zen { -const IoHash IoHash::Zero{}; // Initialized to all zeros +static const uint8_t MaxData[20] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + +const IoHash IoHash::Max = IoHash::MakeFrom(MaxData); // Initialized to all 0xff +const IoHash IoHash::Zero{}; // Initialized to all zeros IoHash IoHash::HashBuffer(const void* data, size_t byteCount) -- cgit v1.2.3