From 9eacd3a8097d365bdb5815cb039578e946523da7 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Mon, 30 Sep 2024 10:45:40 +0200 Subject: use alternate IoHash comparision function (#177) * Use alternate IoHash comparision function - reduces KeepUnusedReferences execution time by ~20% --- src/zenstore/gc.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/zenstore/gc.cpp') diff --git a/src/zenstore/gc.cpp b/src/zenstore/gc.cpp index 159b13af0..3e0dc5e04 100644 --- a/src/zenstore/gc.cpp +++ b/src/zenstore/gc.cpp @@ -593,6 +593,24 @@ Sum(GcResult& Stat, bool Cancelled = false) return Stat; } +// For GC the has order only needs to be predictable, it does not have to be "true" sort order +static inline bool +LessForGC(const IoHash& Lhs, const IoHash& Rhs) +{ + const uint32_t* LhsData = reinterpret_cast(Lhs.Hash); + const uint32_t* RhsData = reinterpret_cast(Rhs.Hash); + + return LhsData[0] < RhsData[0] ? true + : LhsData[0] != RhsData[0] ? false + : LhsData[1] < RhsData[1] ? true + : LhsData[1] != RhsData[1] ? false + : LhsData[2] < RhsData[2] ? true + : LhsData[2] != RhsData[2] ? false + : LhsData[3] < RhsData[3] ? true + : LhsData[3] != RhsData[3] ? false + : LhsData[4] < RhsData[4]; +} + bool FilterReferences(GcCtx& Ctx, std::string_view Context, std::vector& InOutReferences) { @@ -631,7 +649,7 @@ FilterReferences(GcCtx& Ctx, std::string_view Context, std::vector& InOu { return false; } - std::sort(InOutReferences.begin(), InOutReferences.end()); + std::sort(InOutReferences.begin(), InOutReferences.end(), LessForGC); auto NewEnd = std::unique(InOutReferences.begin(), InOutReferences.end()); InOutReferences.erase(NewEnd, InOutReferences.end()); return true; @@ -666,7 +684,7 @@ KeepUnusedReferences(std::span SortedUsedReferences, std::span ReferencesWrite) @@ -3017,9 +3035,10 @@ TEST_CASE("scrub.basic") TEST_CASE("gc.keepunusedreferences") { - const IoHash Hashes[] = {IoHash::FromHexString("177030568fdd461bf4fe5ddbf4d463e514e8178e"), + // Order is important, this is the order the hashes would be sorted by FilterReferences + const IoHash Hashes[] = {IoHash::FromHexString("75ab3917854bfef7e72d795bb907a15cab1af2c3"), + IoHash::FromHexString("177030568fdd461bf4fe5ddbf4d463e514e8178e"), IoHash::FromHexString("372d795bb907a15cab15ab3917854bfef7e7af2c"), - IoHash::FromHexString("75ab3917854bfef7e72d795bb907a15cab1af2c3"), IoHash::FromHexString("ab3917854bfef7e7af2c372d795bb907a15cab15"), IoHash::FromHexString("d1df59fcab06793a5f2c372d795bb907a15cab15")}; { -- cgit v1.2.3