From 1f30d0502791cc91f287138a7bf4e9c35c1ab3ca Mon Sep 17 00:00:00 2001 From: mitaclaw <140017135+mitaclaw@users.noreply.github.com> Date: Tue, 7 May 2024 19:54:25 -0700 Subject: [PATCH] Externals: Update rangeset I added a new `get_stats` member function to the upstream for use in the JIT Widget Refresh. --- .../rangeset/include/rangeset/rangeset.h | 25 +++++++++++++++++++ .../rangeset/include/rangeset/rangesizeset.h | 11 ++++++++ 2 files changed, 36 insertions(+) diff --git a/Externals/rangeset/include/rangeset/rangeset.h b/Externals/rangeset/include/rangeset/rangeset.h index 60f665c9ee..22c7e81550 100644 --- a/Externals/rangeset/include/rangeset/rangeset.h +++ b/Externals/rangeset/include/rangeset/rangeset.h @@ -3,6 +3,7 @@ #include #include #include +#include namespace HyoutaUtilities { template class RangeSet { @@ -254,7 +255,31 @@ public: return !(*this == other); } + // Get free size and fragmentation ratio + std::pair get_stats() const { + std::size_t free_total = 0; + if (begin() == end()) + return {free_total, 1.0}; + std::size_t largest_size = 0; + for (auto iter = begin(); iter != end(); ++iter) { + const std::size_t size = calc_size(iter.from(), iter.to()); + if (size > largest_size) + largest_size = size; + free_total += size; + } + return {free_total, static_cast(free_total - largest_size) / free_total}; + } + private: + static std::size_t calc_size(T from, T to) { + if constexpr (std::is_pointer_v) { + // For pointers we don't want pointer arithmetic here, else void* breaks. + return reinterpret_cast(to) - reinterpret_cast(from); + } else { + return static_cast(to - from); + } + } + // Assumptions that can be made about the data: // - Range are stored in the form [from, to[ // That is, the starting value is inclusive, and the end value is exclusive. diff --git a/Externals/rangeset/include/rangeset/rangesizeset.h b/Externals/rangeset/include/rangeset/rangesizeset.h index e91c74f210..7128a90602 100644 --- a/Externals/rangeset/include/rangeset/rangesizeset.h +++ b/Externals/rangeset/include/rangeset/rangesizeset.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace HyoutaUtilities { // Like RangeSet, but additionally stores a map of the ranges sorted by their size, for quickly finding the largest or @@ -398,6 +399,16 @@ public: return !(*this == other); } + // Get free size and fragmentation ratio + std::pair get_stats() const { + std::size_t free_total = 0; + if (begin() == end()) + return {free_total, 1.0}; + for (auto iter = begin(); iter != end(); ++iter) + free_total += calc_size(iter.from(), iter.to()); + return {free_total, static_cast(free_total - Sizes.begin()->first) / free_total}; + } + private: static SizeT calc_size(T from, T to) { if constexpr (std::is_pointer_v) {