aboutsummaryrefslogtreecommitdiff
path: root/src/zencore
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2024-09-23 19:19:40 +0200
committerGitHub Enterprise <[email protected]>2024-09-23 19:19:40 +0200
commitbc9e590727211d803cce7be84c1cbc026179b841 (patch)
tree96d89b59cdced94ce1d795cd941d35d26f6c5e88 /src/zencore
parentmade fmt formatter format function const (#162) (diff)
downloadzen-bc9e590727211d803cce7be84c1cbc026179b841.tar.xz
zen-bc9e590727211d803cce7be84c1cbc026179b841.zip
gc unused refactor (#165)
* optimize IoHash and OId comparisions * refactor filtering of unused references * add attachment filtering to gc
Diffstat (limited to 'src/zencore')
-rw-r--r--src/zencore/include/zencore/iohash.h8
-rw-r--r--src/zencore/include/zencore/memory.h35
-rw-r--r--src/zencore/include/zencore/uid.h4
-rw-r--r--src/zencore/iohash.cpp6
4 files changed, 47 insertions, 6 deletions
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<sizeof Hash, std::uint32_t>(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<std::size_t SIZE>
+inline int
+MemCmpFixed(const void* a1, const void* a2)
+{
+ auto const s1 = reinterpret_cast<const unsigned char*>(a1);
+ auto const s2 = reinterpret_cast<const unsigned char*>(a2);
+ auto const diff = *s1 - *s2;
+ return diff ? diff : MemCmpFixed<SIZE - 1>(s1 + 1, s2 + 1);
+}
+
+template<>
+inline int
+MemCmpFixed<0>(const void*, const void*)
+{
+ return 0;
+}
+
+template<std::size_t SIZE, typename EQTYPE>
+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<const EQTYPE*>(a1);
+ auto const s2 = reinterpret_cast<const EQTYPE*>(a2);
+ return (*s1 != *s2) ? MemCmpFixed<sizeof(EQTYPE)>(s1, s2) : MemCmpFixed<SIZE - sizeof(EQTYPE), EQTYPE>(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 <zencore/memory.h>
#include <zencore/zencore.h>
#include <compare>
@@ -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<sizeof OidBits, std::uint32_t>(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)