From f528f24036698e2014f370f174abab286ef397b6 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 18 Jan 2024 13:03:38 +0100 Subject: codec: Changed hashing to crc32 and made the calculation constexpr --- modules/codec/c++/schema_hash.h | 60 +++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 23 deletions(-) (limited to 'modules/codec/c++/schema_hash.h') diff --git a/modules/codec/c++/schema_hash.h b/modules/codec/c++/schema_hash.h index d4312c4..48e8d6c 100644 --- a/modules/codec/c++/schema_hash.h +++ b/modules/codec/c++/schema_hash.h @@ -2,22 +2,14 @@ #include #include "schema.h" +#include "crc32.h" namespace saw { -struct schema_hash_combine { - static constexpr uint64_t apply(uint64_t seed, uint64_t v){ - return seed ^( std::hash{}(v) + 0x9e3779b9 + (seed<<6) + (seed >> 2)); - } -}; - template struct hash_literal { - static constexpr uint64_t apply(uint64_t seed){ + static constexpr uint32_t apply(uint32_t seed){ constexpr std::string_view view = lit.view(); - for(uint64_t i = 0; i < view.size(); ++i){ - seed = schema_hash_combine::apply(seed, static_cast(view[i])); - } - return seed; + return hash::update(seed, view); } }; @@ -30,7 +22,7 @@ template<> struct schema_hash_seed { using Schema = schema::SignedInteger; - static constexpr uint64_t apply(uint64_t seed){ + static constexpr uint32_t apply(uint32_t seed){ return hash_literal::apply(seed); } }; @@ -39,7 +31,7 @@ template<> struct schema_hash_seed { using Schema = schema::UnsignedInteger; - static constexpr uint64_t apply(uint64_t seed){ + static constexpr uint32_t apply(uint32_t seed){ return hash_literal::apply(seed); } }; @@ -48,7 +40,7 @@ template<> struct schema_hash_seed { using Schema = schema::FloatingPoint; - static constexpr uint64_t apply(uint64_t seed){ + static constexpr uint32_t apply(uint32_t seed){ return hash_literal::apply(seed); } }; @@ -57,7 +49,7 @@ template<> struct schema_hash_seed { using Schema = schema::String; - static constexpr uint64_t apply(uint64_t seed){ + static constexpr uint32_t apply(uint32_t seed){ return hash_literal::apply(seed); } }; @@ -66,10 +58,21 @@ template struct schema_hash_seed> { using Schema = schema::Primitive; - static constexpr uint64_t apply(uint64_t seed){ + static constexpr uint32_t apply(uint32_t seed){ seed = hash_literal::apply(seed); seed = schema_hash_seed

::apply(seed); - seed = schema_hash_combine::apply(seed, N); + uint64_t val = N; + std::array dat{ + static_cast(val >> 0), + static_cast(val >> 8), + static_cast(val >> 16), + static_cast(val >> 24), + static_cast(val >> 32), + static_cast(val >> 40), + static_cast(val >> 48), + static_cast(val >> 56) + }; + seed = hash::update(seed, &dat[0], dat.size()); return seed; } }; @@ -78,10 +81,21 @@ template struct schema_hash_seed> { using Schema = schema::Array; - static constexpr uint64_t apply(uint64_t seed){ + static constexpr uint32_t apply(uint32_t seed){ seed = hash_literal::apply(seed); seed = schema_hash_seed::apply(seed); - seed = schema_hash_combine::apply(seed, N); + uint64_t val = N; + std::array dat{ + static_cast(val >> 0), + static_cast(val >> 8), + static_cast(val >> 16), + static_cast(val >> 24), + static_cast(val >> 32), + static_cast(val >> 40), + static_cast(val >> 48), + static_cast(val >> 56) + }; + seed = hash::update(seed, &dat[0], dat.size()); return seed; } }; @@ -91,7 +105,7 @@ struct schema_hash_seed...>> { using Schema = schema::Struct...>; template - static constexpr uint64_t apply_ele(uint64_t seed){ + static constexpr uint32_t apply_ele(uint32_t seed){ using Type = typename parameter_pack_type::type; constexpr string_literal Lit = parameter_key_pack_type::literal; using MemberT = typename parameter_pack_type...>::type; @@ -106,7 +120,7 @@ struct schema_hash_seed...>> { return seed; } - static constexpr uint64_t apply(uint64_t seed){ + static constexpr uint32_t apply(uint32_t seed){ seed = hash_literal::apply(seed); if constexpr (sizeof...(V) > 0){ seed = apply_ele<0>(seed); @@ -117,8 +131,8 @@ struct schema_hash_seed...>> { template struct schema_hash { - static constexpr uint64_t apply() { - constexpr uint64_t seed = 0; + static constexpr uint32_t apply() { + constexpr uint32_t seed = 0; return schema_hash_seed::apply(seed); } }; -- cgit v1.2.3