1
0
mirror of https://github.com/wolfpld/tracy.git synced 2025-03-20 07:40:02 +08:00

Reduce custom vector size 16 -> 13 bytes.

This commit is contained in:
Bartosz Taudul
2017-10-28 23:12:11 +02:00
parent 1a117b330f
commit 34123de977
5 changed files with 53 additions and 36 deletions
+29 -21
View File
@@ -5,6 +5,7 @@
#include <stdint.h>
#include "TracyMemory.hpp"
#include "TracyPopcnt.hpp"
namespace tracy
{
@@ -13,6 +14,7 @@ namespace tracy
template<typename T>
using Vector = std::vector<T>;
#else
#pragma pack( 1 )
template<typename T>
class Vector
{
@@ -20,10 +22,11 @@ public:
using iterator = T*;
Vector()
: m_ptr( nullptr )
: m_ptr( new T[1] )
, m_size( 0 )
, m_capacity( 0 )
{
memUsage.fetch_add( sizeof( T ), std::memory_order_relaxed );
}
Vector( const Vector& ) = delete;
@@ -37,7 +40,7 @@ public:
~Vector()
{
memUsage.fetch_sub( m_capacity * sizeof( T ), std::memory_order_relaxed );
memUsage.fetch_sub( Capacity() * sizeof( T ), std::memory_order_relaxed );
delete[] m_ptr;
}
@@ -73,13 +76,13 @@ public:
void push_back( const T& v )
{
if( m_size == m_capacity ) AllocMore();
if( m_size == Capacity() ) AllocMore();
m_ptr[m_size++] = v;
}
void push_back( T&& v )
{
if( m_size == m_capacity ) AllocMore();
if( m_size == Capacity() ) AllocMore();
m_ptr[m_size++] = std::move( v );
}
@@ -87,7 +90,7 @@ public:
{
assert( it >= m_ptr && it <= m_ptr + m_size );
const auto dist = it - m_ptr;
if( m_size == m_capacity ) AllocMore();
if( m_size == Capacity() ) AllocMore();
if( dist != m_size ) memmove( m_ptr + dist + 1, m_ptr + dist, ( m_size - dist ) * sizeof( T ) );
m_size++;
m_ptr[dist] = v;
@@ -98,7 +101,7 @@ public:
{
assert( it >= m_ptr && it <= m_ptr + m_size );
const auto dist = it - m_ptr;
if( m_size == m_capacity ) AllocMore();
if( m_size == Capacity() ) AllocMore();
if( dist != m_size ) memmove( m_ptr + dist + 1, m_ptr + dist, ( m_size - dist ) * sizeof( T ) );
m_size++;
m_ptr[dist] = std::move( v );
@@ -110,7 +113,7 @@ public:
assert( it >= m_ptr && it <= m_ptr + m_size );
const auto sz = end - begin;
const auto dist = it - m_ptr;
while( m_size + sz > m_capacity ) AllocMore();
while( m_size + sz > Capacity() ) AllocMore();
if( dist != m_size ) memmove( m_ptr + dist + sz, m_ptr + dist, ( m_size - dist ) * sizeof( T ) );
m_size += sz;
memcpy( m_ptr + dist, begin, sz * sizeof( T ) );
@@ -133,8 +136,15 @@ public:
void reserve( size_t cap )
{
if( cap <= m_capacity ) return;
memUsage.fetch_add( ( cap - m_capacity ) * sizeof( T ), std::memory_order_relaxed );
if( cap <= Capacity() ) return;
cap--;
cap |= cap >> 1;
cap |= cap >> 2;
cap |= cap >> 4;
cap |= cap >> 8;
cap |= cap >> 16;
cap = TracyCountBits( cap );
memUsage.fetch_add( ( ( 1 << cap ) - Capacity() ) * sizeof( T ), std::memory_order_relaxed );
m_capacity = cap;
Realloc();
}
@@ -147,22 +157,14 @@ public:
private:
void AllocMore()
{
if( m_capacity == 0 )
{
m_capacity = 1;
memUsage.fetch_add( m_capacity * sizeof( T ), std::memory_order_relaxed );
}
else
{
memUsage.fetch_add( m_capacity * sizeof( T ), std::memory_order_relaxed );
m_capacity *= 2;
}
memUsage.fetch_add( Capacity() * sizeof( T ), std::memory_order_relaxed );
m_capacity++;
Realloc();
}
void Realloc()
{
T* ptr = new T[m_capacity];
T* ptr = new T[Capacity()];
if( m_size != 0 )
{
memcpy( ptr, m_ptr, m_size * sizeof( T ) );
@@ -171,10 +173,16 @@ private:
m_ptr = ptr;
}
uint32_t Capacity() const
{
return 1 << m_capacity;
}
T* m_ptr;
uint32_t m_size;
uint32_t m_capacity;
uint8_t m_capacity;
};
#pragma pack()
#endif
}