From: Mikko Rasa Date: Fri, 9 Dec 2022 23:47:45 +0000 (+0200) Subject: Rework protocol hash calculation X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=92e8064d9f43ca3e28ce9832cfe475034ab98704;p=libs%2Fnet.git Rework protocol hash calculation Instead of creating a string and hashing it, just build the hash directly. --- diff --git a/source/net/protocol.cpp b/source/net/protocol.cpp index 9cba638..81e9d14 100644 --- a/source/net/protocol.cpp +++ b/source/net/protocol.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -76,10 +75,13 @@ size_t Protocol::get_packet_size(const char *buf, size_t size) const uint64_t Protocol::get_hash() const { - string description; + uint64_t result = hash<64>(packet_id_defs.size()); for(auto &kvp: packet_id_defs) - description += format("%d:%s\n", kvp.first, kvp.second->describe()); - return hash<64>(description); + { + hash_update<64>(result, kvp.first); + hash_update<64>(result, kvp.second->get_hash()); + } + return result; } diff --git a/source/net/protocol.h b/source/net/protocol.h index 9f09fe5..e867c0a 100644 --- a/source/net/protocol.h +++ b/source/net/protocol.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "receiver.h" namespace Msp { @@ -38,7 +39,7 @@ private: { virtual ~CompoundTypeDef() = default; - virtual std::string describe() const = 0; + virtual std::uint64_t get_hash() const = 0; virtual char *serialize(const C &, char *, char *) const = 0; virtual const char *deserialize(C &, const char *, const char *) const = 0; }; @@ -50,7 +51,7 @@ private: CompoundDef(const S &); - std::string describe() const override; + std::uint64_t get_hash() const override; char *serialize(const C &, char *, char *) const override; const char *deserialize(C &, const char *, const char *) const override; }; @@ -63,7 +64,7 @@ private: BasicSerializer(const Protocol &) { } - std::string describe() const { return get_type_signature(); } + std::uint64_t get_hash() const { return Traits::signature; } char *serialize(const T &, char *, char *) const; const char *deserialize(T &, const char *, const char *) const; }; @@ -79,7 +80,7 @@ private: public: StringSerializer(const Protocol &); - std::string describe() const { return get_type_signature(); } + std::uint64_t get_hash() const; char *serialize(const std::string &, char *, char *) const; const char *deserialize(std::string &, const char *, const char *) const; }; @@ -97,7 +98,7 @@ private: public: ArraySerializer(const Protocol &); - std::string describe() const; + std::uint64_t get_hash() const; char *serialize(const A &, char *, char *) const; const char *deserialize(A &, const char *, const char *) const; }; @@ -114,7 +115,6 @@ private: public: CompoundSerializer(const Protocol &); - std::string describe() const { return def.describe(); } char *serialize(const C &, char *, char *) const; const char *deserialize(C &, const char *, const char *) const; }; @@ -138,7 +138,7 @@ private: public: Serializer(const Head &, Pointer, const Protocol &); - std::string describe() const; + std::uint64_t get_hash() const; char *serialize(const P &, char *, char *) const; const char *deserialize(P &, const char *, const char *) const; }; @@ -153,7 +153,7 @@ private: typedef Serializer::Serializer> Type; }; - std::string describe() const { return std::string(); } + std::uint64_t get_hash() const { return 0; } char *serialize(const P &, char *b, char *) const { return b; } const char *deserialize(P &, const char *b, const char *) const { return b; } }; @@ -169,7 +169,7 @@ private: virtual unsigned get_class_id() const = 0; unsigned get_id() const { return id; } - virtual std::string describe() const = 0; + virtual std::uint64_t get_hash() const = 0; virtual const char *dispatch(ReceiverBase &, const char *, const char *) const = 0; }; @@ -190,7 +190,7 @@ private: const CompoundTypeDef

&get_compound() const { return *compound; } - std::string describe() const override { return compound->describe(); } + std::uint64_t get_hash() const override { return compound->get_hash(); } char *serialize(const P &, char *, char *) const; const char *deserialize(P &, const char *, const char *) const; const char *dispatch(ReceiverBase &, const char *, const char *) const override; @@ -261,10 +261,6 @@ public: std::size_t dispatch(ReceiverBase &, const char *, std::size_t) const; std::uint64_t get_hash() const; - -private: - template - static std::string get_type_signature(); }; @@ -308,17 +304,6 @@ std::size_t Protocol::serialize(const P &pkt, char *buf, std::size_t size) const return size; } -template -std::string Protocol::get_type_signature() -{ - const std::uint16_t sig = Traits::signature; - std::string result; - result += sig&0xFF; - if(sig>=0x100) - result += '0'+(sig>>8); - return result; -} - template struct Protocol::BasicTraits @@ -367,9 +352,9 @@ Protocol::CompoundDef::CompoundDef(const S &s): { } template -std::string Protocol::CompoundDef::describe() const +std::uint64_t Protocol::CompoundDef::get_hash() const { - return "{"+serializer.describe()+"}"; + return hash_round<64>(serializer.get_hash(), 'C'); } template @@ -385,6 +370,12 @@ const char *Protocol::CompoundDef::deserialize(C &com, const char *buf, co } +inline std::uint64_t Protocol::StringSerializer::get_hash() const +{ + return Traits::signature; +} + + template Protocol::ArraySerializer::ArraySerializer(const Protocol &proto): length_serializer(proto), @@ -392,9 +383,9 @@ Protocol::ArraySerializer::ArraySerializer(const Protocol &proto): { } template -std::string Protocol::ArraySerializer::describe() const +std::uint64_t Protocol::ArraySerializer::get_hash() const { - return "["+element_serializer.describe()+"]"; + return hash_round<64>(element_serializer.get_hash(), 'A'); } template @@ -444,9 +435,9 @@ Protocol::Serializer::Serializer(const Head &h, Pointer p, const Pro { } template -std::string Protocol::Serializer::describe() const +std::uint64_t Protocol::Serializer::get_hash() const { - return Head::describe()+ser.describe(); + return hash_update<64>(Head::get_hash(), ser.get_hash()); } template