Separate custom string processing from string storage.

This commit is contained in:
Bartosz Taudul 2017-11-10 17:45:19 +01:00
parent f6af913fd3
commit 2887753b5d
2 changed files with 36 additions and 17 deletions

View File

@ -243,8 +243,8 @@ View::View( FileRead& f )
auto dst = m_slab.Alloc<char>( ssz+1 ); auto dst = m_slab.Alloc<char>( ssz+1 );
f.Read( dst, ssz ); f.Read( dst, ssz );
dst[ssz] = '\0'; dst[ssz] = '\0';
m_customStringMap.emplace( dst, m_customStringData.size() ); m_stringMap.emplace( dst, m_stringData.size() );
m_customStringData.push_back( dst ); m_stringData.push_back( dst );
stringMap.emplace( ptr, dst ); stringMap.emplace( ptr, dst );
} }
@ -916,28 +916,39 @@ void View::AddThreadString( uint64_t id, std::string&& str )
m_threadNames.emplace( id, std::move( str ) ); m_threadNames.emplace( id, std::move( str ) );
} }
void View::AddCustomString( uint64_t ptr, std::string&& str ) void View::AddCustomString( uint64_t ptr, const std::string& str )
{ {
auto pit = m_pendingCustomStrings.find( ptr ); auto pit = m_pendingCustomStrings.find( ptr );
assert( pit != m_pendingCustomStrings.end() ); assert( pit != m_pendingCustomStrings.end() );
std::unique_lock<std::mutex> lock( m_lock ); const auto sl = StoreString( str );
auto sit = m_customStringMap.find( str.c_str() ); m_lock.lock();
if( sit == m_customStringMap.end() ) GetTextData( *pit->second )->userText = sl.ptr;
m_lock.unlock();
m_pendingCustomStrings.erase( pit );
}
View::StringLocation View::StoreString( const std::string& str )
{
StringLocation ret;
auto sit = m_stringMap.find( str.c_str() );
if( sit == m_stringMap.end() )
{ {
const auto sz = str.size(); const auto sz = str.size();
auto ptr = m_slab.Alloc<char>( sz+1 ); auto ptr = m_slab.Alloc<char>( sz+1 );
memcpy( ptr, str.c_str(), sz ); memcpy( ptr, str.c_str(), sz );
ptr[sz] = '\0'; ptr[sz] = '\0';
GetTextData( *pit->second )->userText = ptr; ret.ptr = ptr;
m_customStringMap.emplace( ptr, m_customStringData.size() ); ret.idx = m_stringData.size();
m_customStringData.push_back( ptr ); std::lock_guard<std::mutex> lock( m_lock );
m_stringMap.emplace( ptr, m_stringData.size() );
m_stringData.push_back( ptr );
} }
else else
{ {
GetTextData( *pit->second )->userText = sit->first; ret.ptr = sit->first;
ret.idx = sit->second;
} }
lock.unlock(); return ret;
m_pendingCustomStrings.erase( pit );
} }
void View::AddSourceLocation( const QueueSourceLocation& srcloc ) void View::AddSourceLocation( const QueueSourceLocation& srcloc )
@ -3245,9 +3256,9 @@ void View::Write( FileWrite& f )
f.Write( v.second.c_str(), v.second.size() ); f.Write( v.second.c_str(), v.second.size() );
} }
sz = m_customStringData.size(); sz = m_stringData.size();
f.Write( &sz, sizeof( sz ) ); f.Write( &sz, sizeof( sz ) );
for( auto& v : m_customStringData ) for( auto& v : m_stringData )
{ {
uint64_t ptr = (uint64_t)v; uint64_t ptr = (uint64_t)v;
f.Write( &ptr, sizeof( ptr ) ); f.Write( &ptr, sizeof( ptr ) );

View File

@ -115,6 +115,12 @@ private:
uint64_t postponeTime; uint64_t postponeTime;
}; };
struct StringLocation
{
const char* ptr;
uint32_t idx;
};
void Worker(); void Worker();
void DispatchProcess( const QueueItem& ev, const char*& ptr ); void DispatchProcess( const QueueItem& ev, const char*& ptr );
@ -144,11 +150,13 @@ private:
void AddString( uint64_t ptr, std::string&& str ); void AddString( uint64_t ptr, std::string&& str );
void AddThreadString( uint64_t id, std::string&& str ); void AddThreadString( uint64_t id, std::string&& str );
void AddCustomString( uint64_t ptr, std::string&& str ); void AddCustomString( uint64_t ptr, const std::string& str );
void AddSourceLocation( const QueueSourceLocation& srcloc ); void AddSourceLocation( const QueueSourceLocation& srcloc );
void AddSourceLocationPayload( uint64_t ptr, const char* data, size_t sz ); void AddSourceLocationPayload( uint64_t ptr, const char* data, size_t sz );
void AddMessageData( uint64_t ptr, const char* str, size_t sz ); void AddMessageData( uint64_t ptr, const char* str, size_t sz );
StringLocation StoreString( const std::string& str );
uint32_t ShrinkSourceLocation( uint64_t srcloc ); uint32_t ShrinkSourceLocation( uint64_t srcloc );
void InsertMessageData( MessageData* msg, uint64_t thread ); void InsertMessageData( MessageData* msg, uint64_t thread );
@ -237,8 +245,8 @@ private:
std::map<uint32_t, LockMap> m_lockMap; std::map<uint32_t, LockMap> m_lockMap;
uint64_t m_zonesCnt; uint64_t m_zonesCnt;
Vector<const char*> m_customStringData; Vector<const char*> m_stringData;
std::unordered_map<const char*, uint32_t, charutil::Hasher, charutil::Comparator> m_customStringMap; std::unordered_map<const char*, uint32_t, charutil::Hasher, charutil::Comparator> m_stringMap;
std::mutex m_mbpslock; std::mutex m_mbpslock;
std::vector<float> m_mbps; std::vector<float> m_mbps;