Skip to content

Commit 72d48e6

Browse files
committed
[NeoMathEngine] MemoryPool less memory consumption
Signed-off-by: Kirill Golikov <[email protected]>
1 parent 9d6e6f8 commit 72d48e6

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

NeoMathEngine/src/MemoryPool.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ void CMemoryPool::Free( const CMemoryHandle& handle )
156156
TUsedAddressMap::const_iterator it = usedMap.find( GetRaw( handle ) );
157157
const CUsedInfo& info = it->second;
158158

159-
if( info.buffer != nullptr ) {
160-
CMemoryBufferPool* const pool = info.buffer->OwnerPool;
161-
pool->Free( info.buffer );
159+
if( info.HasPoolBuffer() ) {
160+
CMemoryBufferPool* const pool = info.Buffer()->OwnerPool;
161+
pool->Free( info.Buffer() );
162162
freeMemorySize += pool->BufferSize;
163163
} else {
164164
// Large buffer, don't use the pool
165-
freeMemory( info.size, handle );
166-
freeMemorySize += info.size;
165+
freeMemory( info.Size(), handle );
166+
freeMemorySize += info.Size();
167167
}
168168
usedMap.erase( it );
169169
}
@@ -189,9 +189,9 @@ void CMemoryPool::TransferHandleToThisThread( const CMemoryHandle& handle, size_
189189
TUsedAddressMap::iterator usedMapIt = usedMap.find( GetRaw( handle ) );
190190
CUsedInfo& info = usedMapIt->second;
191191

192-
if( info.buffer != nullptr ) {
192+
if( info.HasPoolBuffer() ) {
193193
// Find the buffer pool to steal from
194-
CMemoryBufferPool* otherThreadBufferPool = info.buffer->OwnerPool;
194+
CMemoryBufferPool* otherThreadBufferPool = info.Buffer()->OwnerPool;
195195
ASSERT_EXPR( size <= otherThreadBufferPool->BufferSize );
196196
size = otherThreadBufferPool->BufferSize; // set actual allocated size
197197

@@ -200,7 +200,7 @@ void CMemoryPool::TransferHandleToThisThread( const CMemoryHandle& handle, size_
200200
if( !thisThreadData.Enabled ) {
201201
// Transfer the handle from that thread's pool just to heap, so
202202
// it wouldn't be cleaned-up for that thread after mathEngine.CleanUp().
203-
delete info.buffer;
203+
delete info.Buffer();
204204
info = CUsedInfo( size );
205205
} else {
206206
// Find the buffer in this thread's pool to append it to
@@ -212,11 +212,11 @@ void CMemoryPool::TransferHandleToThisThread( const CMemoryHandle& handle, size_
212212
}
213213
}
214214
// Transfer the handle from other thread-owner to this thread-owner
215-
info.buffer->OwnerPool = thisThreadBufferPool;
215+
info.Buffer()->OwnerPool = thisThreadBufferPool;
216216
}
217217
} else { // Large buffers don't use the pools
218218
const size_t validSize = *std::lower_bound( std::begin( BufferSizes ), std::end( BufferSizes ), size );
219-
ASSERT_EXPR( size == info.size || validSize == info.size );
219+
ASSERT_EXPR( size == info.Size() || validSize == info.Size() );
220220
// No need to transfer, because
221221
// it wouldn't be cleaned-up for that thread after mathEngine.CleanUp().
222222
}

NeoMathEngine/src/MemoryPool.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,22 @@ class CMemoryPool : public CCrtAllocatedObject {
8181
>;
8282
// The information about a memory block address
8383
struct CUsedInfo final {
84-
size_t size = 0;
85-
CMemoryBuffer* buffer = nullptr;
86-
87-
CUsedInfo( size_t _size = 0 ) : size( _size ) {}
84+
// Either the size of heap memory block
85+
CUsedInfo( size_t _size = 0 ) : size( _size | flag ) { ASSERT_EXPR( _size < flag ); }
86+
// Or the address to memory block buffer
8887
CUsedInfo( CMemoryBuffer* _buffer ) : buffer( _buffer ) {}
88+
89+
CMemoryBuffer* Buffer() const { return buffer; }
90+
size_t Size() const { return size & ~flag; }
91+
bool HasPoolBuffer() const { return !( size & flag ); }
92+
93+
private:
94+
// Flag to signal that in unipn is a size, not a buffer
95+
static constexpr size_t flag = size_t( 1 ) << ( sizeof( size_t ) * 8 - 1 );
96+
union { // Either the size, or a buffer (8 bytes size structure)
97+
size_t size;
98+
CMemoryBuffer* buffer;
99+
};
89100
};
90101
// The memory blocks addresses map
91102
using TUsedAddressMap = std::unordered_map<

0 commit comments

Comments
 (0)