From ca4483ecf5385c82b2aa6d67bf826c82e1ab86eb Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 11 Nov 2017 02:31:51 +0100 Subject: [PATCH] Prevent source location payload duplication. --- server/TracyCharUtil.hpp | 15 +++++++++++++++ server/TracyEvent.hpp | 4 ++-- server/TracyView.cpp | 26 +++++++++++++++++--------- server/TracyView.hpp | 20 +++++++++++++++++++- 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/server/TracyCharUtil.hpp b/server/TracyCharUtil.hpp index ece9de1b..8e1be47d 100644 --- a/server/TracyCharUtil.hpp +++ b/server/TracyCharUtil.hpp @@ -23,6 +23,21 @@ static inline uint32_t hash( const char* str ) return hash; } +static inline uint32_t hash( const char* str, size_t sz ) +{ + uint32_t hash = 5381; + int c; + + while( sz > 0 ) + { + c = *str++; + hash = ( ( hash << 5 ) + hash ) ^ c; + sz--; + } + + return hash; +} + struct Hasher { size_t operator()( const char* key ) const diff --git a/server/TracyEvent.hpp b/server/TracyEvent.hpp index bd9349b5..0f6ba6d5 100644 --- a/server/TracyEvent.hpp +++ b/server/TracyEvent.hpp @@ -20,7 +20,7 @@ struct StringRef { if( isidx ) { - stridx = (uint32_t)data; + stridx = data; } else { @@ -31,7 +31,7 @@ struct StringRef union { uint64_t strptr; - uint32_t stridx; + uint64_t stridx; }; bool isidx; }; diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 0dcd631f..3b86434b 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -952,16 +952,24 @@ void View::AddSourceLocationPayload( uint64_t ptr, char* data, size_t sz ) const auto ssz = sz - ( end - start ); const auto source = StoreString( end, ssz ); - auto srcloc = m_slab.Alloc(); - srcloc->function = StringRef( StringRef::Idx, func.idx ); - srcloc->file = StringRef( StringRef::Idx, source.idx ); - srcloc->line = line; - srcloc->color = color; + SourceLocation srcloc { StringRef( StringRef::Idx, func.idx ), StringRef( StringRef::Idx, source.idx ), line, color }; + auto it = m_sourceLocationPayloadMap.find( &srcloc ); + if( it == m_sourceLocationPayloadMap.end() ) + { + auto slptr = m_slab.Alloc(); + memcpy( slptr, &srcloc, sizeof( srcloc ) ); + uint32_t idx = m_sourceLocationPayload.size(); + m_sourceLocationPayloadMap.emplace( slptr, idx ); + std::unique_lock lock( m_lock ); + m_sourceLocationPayload.push_back( slptr ); + pit->second->srcloc = -int32_t( idx + 1 ); + } + else + { + std::unique_lock lock( m_lock ); + pit->second->srcloc = -int32_t( it->second + 1 ); + } - std::unique_lock lock( m_lock ); - pit->second->srcloc = -int32_t( m_sourceLocationPayload.size() + 1 ); - m_sourceLocationPayload.push_back( srcloc ); - lock.unlock(); m_pendingSourceLocationPayload.erase( pit ); } diff --git a/server/TracyView.hpp b/server/TracyView.hpp index 5b2d4d94..e488c628 100644 --- a/server/TracyView.hpp +++ b/server/TracyView.hpp @@ -109,6 +109,22 @@ private: uint32_t idx; }; + struct SourceLocationHasher + { + size_t operator()( const SourceLocation* ptr ) const + { + return charutil::hash( (const char*)ptr, sizeof( SourceLocation ) ); + } + }; + + struct SourceLocationComparator + { + bool operator()( const SourceLocation* lhs, const SourceLocation* rhs ) const + { + return memcmp( lhs, rhs, sizeof( SourceLocation ) ) == 0; + } + }; + void Worker(); void DispatchProcess( const QueueItem& ev, char*& ptr ); @@ -223,7 +239,6 @@ private: Vector m_plots; Vector m_messages; Vector m_textData; - Vector m_sourceLocationPayload; std::unordered_map m_strings; std::unordered_map m_threadNames; std::unordered_map m_sourceLocation; @@ -234,6 +249,9 @@ private: Vector m_stringData; std::unordered_map m_stringMap; + Vector m_sourceLocationPayload; + std::unordered_map m_sourceLocationPayloadMap; + std::mutex m_mbpslock; std::vector m_mbps;