From 0a21224c989bbbc293e0597610d798cf82f189f3 Mon Sep 17 00:00:00 2001 From: JCW Date: Fri, 30 May 2025 11:27:03 +0100 Subject: [PATCH] optimise --- include/xrpl/beast/core/FunctionProfiler.h | 16 +++++++-- include/xrpl/beast/hash/xxhasher.h | 39 +++++++++------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/include/xrpl/beast/core/FunctionProfiler.h b/include/xrpl/beast/core/FunctionProfiler.h index eb28a65bc1..a2ed6d299f 100644 --- a/include/xrpl/beast/core/FunctionProfiler.h +++ b/include/xrpl/beast/core/FunctionProfiler.h @@ -9,13 +9,17 @@ #include #include #include -#include #include #include #include // std::accumulate -// #define __rdtsc() 0 +#define PROFILING 1 +#if PROFILING +#include +#else +#define __rdtsc() 0 +#endif namespace beast { template @@ -56,24 +60,29 @@ public: FunctionProfiler( std::string const& tag, std::source_location location = std::source_location::current()) +#if PROFILING : functionName(location.function_name() + tag) , start(std::chrono::steady_clock::now()) , cpuCycleStart(__rdtsc()) +#endif { } ~FunctionProfiler() noexcept { +#if PROFILING auto duration = std::chrono::steady_clock::now() - start; std::lock_guard lock{mutex_}; funcionDurations[functionName].time.emplace_back(duration); funcionDurations[functionName].cpuCycles.emplace_back((__rdtsc() - cpuCycleStart)); +#endif } }; inline std::string getProfilingResults() { +#if PROFILING std::lock_guard lock{FunctionProfiler::mutex_}; std::stringstream ss; ss << "Function profiling results:" << std::endl; @@ -101,6 +110,9 @@ getProfilingResults() } return ss.str(); +#else + return ""; +#endif } } // namespace beast diff --git a/include/xrpl/beast/hash/xxhasher.h b/include/xrpl/beast/hash/xxhasher.h index 2dfd27a499..9d492ba458 100644 --- a/include/xrpl/beast/hash/xxhasher.h +++ b/include/xrpl/beast/hash/xxhasher.h @@ -32,7 +32,6 @@ #include #include -#define PROFILING 0 namespace beast { @@ -59,10 +58,12 @@ private: }; // XXH3_state_t* state_; +#if PROFILING inline static thread_local state_wrapper wrapper{}; std::size_t totalSize_ = 0; std::chrono::nanoseconds duration_{}; std::uint64_t cpuCycles = 0; +#endif std::uint8_t seed_ = 0; std::array buffer_; @@ -166,8 +167,8 @@ public: void operator()(void const* key, std::size_t len) noexcept { - totalSize_ += len; #if PROFILING + totalSize_ += len; auto start = std::chrono::steady_clock::now(); auto cpuCyclesStart = __rdtsc(); #endif @@ -192,32 +193,24 @@ public: auto start = std::chrono::steady_clock::now(); auto cpuCyclesStart = __rdtsc(); #endif - std::array hash{}; - const size_t bit_width = readBuffer_.size() * 8; - const size_t shift = seed_ % bit_width; - for (size_t i = 0; i < 8; ++i) { - size_t out_bit_index = i * 8; - size_t in_bit_index = (out_bit_index + shift) % bit_width; + if (readBuffer_.size() == 0) return 0; - size_t byte_index = in_bit_index / 8; - size_t bit_offset = in_bit_index % 8; + const size_t bit_width = readBuffer_.size() * 8; + const size_t shift = seed_ % bit_width; - // Grab two adjacent bytes to ensure we can extract 8 bits safely - uint8_t first = readBuffer_[byte_index % readBuffer_.size()]; - uint8_t second = readBuffer_[(byte_index + 1) % readBuffer_.size()]; + // Copy input into a buffer long enough to safely extract 64 bits with wraparound + std::uint64_t buffer = 0; - // Combine into 16-bit buffer - uint16_t combined = (first << 8) | second; + // Load the first 8 bytes (or wrap if input < 8 bytes) + for (size_t i = 0; i < 8; ++i) { + size_t index = readBuffer_.size() - 1 - (i % readBuffer_.size()); + buffer <<= 8; + buffer |= readBuffer_[index]; + } - // Extract 8 bits starting at bit_offset - uint8_t extracted = (combined >> (8 - bit_offset)) & 0xFF; - - hash[i] = std::byte(extracted); - } - - std::uint64_t result; - std::memcpy(&result, hash.data(), hash.size()); + // Rotate and return + auto result = (buffer << shift) | (buffer >> (64 - shift)); #if PROFILING duration_ += std::chrono::steady_clock::now() - start;