From 9d75ed14b4da0841cbf9a2a93107403ad6a4b5b8 Mon Sep 17 00:00:00 2001 From: Adrien Beraud Date: Mon, 10 Apr 2017 15:44:06 +0200 Subject: [PATCH] infohash: add to_c_str(), improve toString() performance --- include/opendht/infohash.h | 2 ++ src/infohash.cpp | 34 +++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/include/opendht/infohash.h b/include/opendht/infohash.h index 5e07ba1c4..74186ed2e 100644 --- a/include/opendht/infohash.h +++ b/include/opendht/infohash.h @@ -278,6 +278,8 @@ class OPENDHT_PUBLIC InfoHash final : public std::array { OPENDHT_PUBLIC friend std::ostream& operator<< (std::ostream& s, const InfoHash& h); OPENDHT_PUBLIC friend std::istream& operator>> (std::istream& s, InfoHash& h); + const char* to_c_str() const; + std::string toString() const; template diff --git a/src/infohash.cpp b/src/infohash.cpp index 5070abe43..7530ca1d4 100644 --- a/src/infohash.cpp +++ b/src/infohash.cpp @@ -71,20 +71,40 @@ InfoHash::getRandom() return h; } +struct HexMap : public std::array, 256> { + HexMap() { + for (size_t i=0; i> 4) & 0x0F]; + e[1] = hex_digits[i & 0x0F]; + } + } +private: + static constexpr const char* hex_digits = "0123456789abcdef"; +}; + +const char* +InfoHash::to_c_str() const +{ + static const HexMap map; + thread_local std::array buf; + for (size_t i=0; i