|
Boost-Commit : |
From: xushiweizh_at_[hidden]
Date: 2008-05-07 11:19:25
Author: xushiwei
Date: 2008-05-07 11:19:24 EDT (Wed, 07 May 2008)
New Revision: 45199
URL: http://svn.boost.org/trac/boost/changeset/45199
Log:
performance enhancement.
Text files modified:
sandbox/memory/boost/memory/auto_alloc.hpp | 100 +++++++++++++++++++----------------
sandbox/memory/boost/memory/basic.hpp | 109 +++++++++++----------------------------
sandbox/memory/boost/memory/gc_alloc.hpp | 85 +++++++++++++++++-------------
sandbox/memory/libs/memory/test/test_basic/memory/performance.cpp | 12 ++--
sandbox/memory/libs/memory/test/test_basic/test.dsp | 2
5 files changed, 140 insertions(+), 168 deletions(-)
Modified: sandbox/memory/boost/memory/auto_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/auto_alloc.hpp (original)
+++ sandbox/memory/boost/memory/auto_alloc.hpp 2008-05-07 11:19:24 EDT (Wed, 07 May 2008)
@@ -125,62 +125,51 @@
m_begin = m_end = (char*)HeaderSize;
}
- template <class Type>
- void BOOST_MEMORY_CALL destroy(Type* obj)
- {
- // no action
- }
-
- template <class Type>
- Type* BOOST_MEMORY_CALL newArray(size_t count, Type* zero)
+private:
+ void* BOOST_MEMORY_CALL _do_allocate(size_t cb)
{
- Type* array = (Type*)destructor_traits<Type>::allocArray(*this, count);
- return constructor_traits<Type>::constructArray(array, count);
- }
-
- template <class Type>
- void BOOST_MEMORY_CALL destroyArray(Type* array, size_t count)
- {
- // no action
- }
-
- void* BOOST_MEMORY_CALL allocate(size_t cb)
- {
- if ((size_t)(m_end - m_begin) < cb)
+ if (cb >= BlockSize)
{
- if (cb >= BlockSize)
+ MemBlock* pHeader = _chainHeader();
+ MemBlock* pNew = (MemBlock*)m_alloc.allocate(HeaderSize + cb);
+ if (pHeader)
{
- MemBlock* pHeader = _chainHeader();
- MemBlock* pNew = (MemBlock*)m_alloc.allocate(HeaderSize + cb);
- if (pHeader)
- {
- pNew->pPrev = pHeader->pPrev;
- pHeader->pPrev = pNew;
- }
- else
- {
- m_end = m_begin = pNew->buffer;
- pNew->pPrev = NULL;
- }
- return pNew->buffer;
+ pNew->pPrev = pHeader->pPrev;
+ pHeader->pPrev = pNew;
}
else
{
- MemBlock* pNew = (MemBlock*)m_alloc.allocate(sizeof(MemBlock));
- pNew->pPrev = _chainHeader();
- m_begin = pNew->buffer;
- m_end = (char*)pNew + m_alloc.alloc_size(pNew);
+ m_end = m_begin = pNew->buffer;
+ pNew->pPrev = NULL;
}
+ return pNew->buffer;
+ }
+ else
+ {
+ MemBlock* pNew = (MemBlock*)m_alloc.allocate(sizeof(MemBlock));
+ pNew->pPrev = _chainHeader();
+ m_begin = pNew->buffer;
+ m_end = (char*)pNew + m_alloc.alloc_size(pNew);
+ return m_end -= cb;
}
- return m_end -= cb;
}
- void* BOOST_MEMORY_CALL allocate(size_t cb, int fnZero)
+public:
+ __forceinline void* BOOST_MEMORY_CALL allocate(size_t cb)
+ {
+ if ((size_t)(m_end - m_begin) >= cb)
+ {
+ return m_end -= cb;
+ }
+ return _do_allocate(cb);
+ }
+
+ __forceinline void* BOOST_MEMORY_CALL allocate(size_t cb, int fnZero)
{
return allocate(cb);
}
- void* BOOST_MEMORY_CALL allocate(size_t cb, destructor_t fn)
+ __forceinline void* BOOST_MEMORY_CALL allocate(size_t cb, destructor_t fn)
{
DestroyNode* pNode = (DestroyNode*)allocate(sizeof(DestroyNode) + cb);
pNode->fnDestroy = fn;
@@ -189,14 +178,14 @@
return pNode + 1;
}
- void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, destructor_t fn)
+ __forceinline void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, destructor_t fn)
{
DestroyNode* pNode = (DestroyNode*)allocate(sizeof(DestroyNode) + cb);
pNode->fnDestroy = fn;
return pNode + 1;
}
- void* BOOST_MEMORY_CALL manage(void* p, destructor_t fn)
+ __forceinline void* BOOST_MEMORY_CALL manage(void* p, destructor_t fn)
{
DestroyNode* pNode = (DestroyNode*)p - 1;
BOOST_MEMORY_ASSERT(pNode->fnDestroy == fn);
@@ -206,12 +195,12 @@
return p;
}
- void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, int fnZero)
+ __forceinline void* BOOST_MEMORY_CALL unmanaged_alloc(size_t cb, int fnZero)
{
return allocate(cb);
}
- void* BOOST_MEMORY_CALL manage(void* p, int fnZero)
+ __forceinline void* BOOST_MEMORY_CALL manage(void* p, int fnZero)
{
return p;
}
@@ -230,6 +219,25 @@
// no action
}
+ template <class Type>
+ void BOOST_MEMORY_CALL destroy(Type* obj)
+ {
+ // no action
+ }
+
+ template <class Type>
+ Type* BOOST_MEMORY_CALL newArray(size_t count, Type* zero)
+ {
+ Type* array = (Type*)destructor_traits<Type>::allocArray(*this, count);
+ return constructor_traits<Type>::constructArray(array, count);
+ }
+
+ template <class Type>
+ void BOOST_MEMORY_CALL destroyArray(Type* array, size_t count)
+ {
+ // no action
+ }
+
BOOST_MEMORY_FAKE_DBG_ALLOCATE_();
};
Modified: sandbox/memory/boost/memory/basic.hpp
==============================================================================
--- sandbox/memory/boost/memory/basic.hpp (original)
+++ sandbox/memory/boost/memory/basic.hpp 2008-05-07 11:19:24 EDT (Wed, 07 May 2008)
@@ -255,69 +255,19 @@
BOOST_MEMORY_DECL_CTYPE(unsigned long);
// =========================================================================
-// operator new of GC Allocator
+// NEW, NEW_ARRAY, ALLOC, ALLOC_ARRAY
NS_BOOST_MEMORY_BEGIN
-enum _alloc_flag { _theAllocFlag = 0 };
-
-NS_BOOST_MEMORY_END
-
-template <class AllocT, class Type>
-__forceinline
-void* BOOST_MEMORY_CALL operator new(
- size_t cb, AllocT& alloc, Type* zero, NS_BOOST_MEMORY::_alloc_flag)
-{
- BOOST_MEMORY_ASSERT(cb == sizeof(Type));
- typedef NS_BOOST_MEMORY::destructor_traits<Type> Traits;
- return alloc.unmanaged_alloc(sizeof(Type), Traits::destruct);
-}
-
-template <class AllocT, class Type>
-__forceinline
-void BOOST_MEMORY_CALL operator delete(
- void* p, AllocT& alloc, Type* zero, NS_BOOST_MEMORY::_alloc_flag)
-{
- // no action
-}
-
-#if defined(_DEBUG)
-
-template <class AllocT, class Type>
-__forceinline
-void* BOOST_MEMORY_CALL operator new(
- size_t cb, AllocT& alloc, Type* zero, NS_BOOST_MEMORY::_alloc_flag,
- const char* file, const int line)
-{
- BOOST_MEMORY_ASSERT(cb == sizeof(Type));
- typedef NS_BOOST_MEMORY::destructor_traits<Type> Traits;
- return alloc.unmanaged_alloc(sizeof(Type), Traits::destruct);
-}
-
-template <class AllocT, class Type>
-__forceinline
-void BOOST_MEMORY_CALL operator delete(
- void* p, AllocT& alloc, Type* zero, NS_BOOST_MEMORY::_alloc_flag,
- const char* file, const int line)
+class _unmanaged
{
- // no action
-}
-
-#endif
-
-#if defined(_DEBUG)
-#define BOOST_MEMORY_FILE_LINE_ARG , __FILE__, __LINE__
-#else
-#define BOOST_MEMORY_FILE_LINE_ARG
-#endif
-
-#define BOOST_MEMORY_UNMANAGED_NEW(alloc, Type) \
- ::new((alloc), (Type*)0, NS_BOOST_MEMORY::_theAllocFlag BOOST_MEMORY_FILE_LINE_ARG) Type
-
-// =========================================================================
-// NEW, NEW_ARRAY, ALLOC, ALLOC_ARRAY
-
-NS_BOOST_MEMORY_BEGIN
+public:
+ template <class Type>
+ __forceinline Type* BOOST_MEMORY_CALL operator->*(Type* p) const
+ {
+ return p;
+ }
+};
template <class AllocT>
class _managed
@@ -335,30 +285,27 @@
}
};
-class _unmanaged
-{
-public:
- template <class Type>
- __forceinline Type* BOOST_MEMORY_CALL operator->*(Type* p) const
- {
- return p;
- }
-};
-
template <class AllocT>
-__forceinline _managed<AllocT> BOOST_MEMORY_CALL _get_managed(AllocT& alloc, destructor_t fn)
+__forceinline _unmanaged BOOST_MEMORY_CALL _get_managed(AllocT& alloc, int fnZero)
{
- return _managed<AllocT>(alloc);
+ return _unmanaged();
}
template <class AllocT>
-__forceinline _unmanaged BOOST_MEMORY_CALL _get_managed(AllocT& alloc, int fnZero)
+__forceinline
+_managed<AllocT> BOOST_MEMORY_CALL _get_managed(AllocT& alloc, destructor_t fn)
{
- return _unmanaged();
+ return _managed<AllocT>(alloc);
}
NS_BOOST_MEMORY_END
+#if defined(_DEBUG)
+#define BOOST_MEMORY_FILE_LINE_ARG , __FILE__, __LINE__
+#else
+#define BOOST_MEMORY_FILE_LINE_ARG
+#endif
+
#define BOOST_MEMORY_NEW_ARG(Type) sizeof(Type), NS_BOOST_MEMORY::destructor_traits<Type>::destruct
#define BOOST_MEMORY_DBG_NEW_ARG(Type) BOOST_MEMORY_NEW_ARG(Type) BOOST_MEMORY_FILE_LINE_ARG
@@ -366,6 +313,10 @@
#define BOOST_MEMORY_DBG_ALLOC_ARG(Type) sizeof(Type) BOOST_MEMORY_FILE_LINE_ARG
#define BOOST_MEMORY_DBG_ALLOC_ARRAY_ARG(Type, count) sizeof(Type)*(count) BOOST_MEMORY_FILE_LINE_ARG
+#define BOOST_MEMORY_ALLOC(alloc, Type) ((Type*)(alloc).allocate(BOOST_MEMORY_DBG_ALLOC_ARG(Type)))
+#define BOOST_MEMORY_ALLOC_ARRAY(alloc, Type, count) ((Type*)(alloc).allocate(BOOST_MEMORY_DBG_ALLOC_ARRAY_ARG(Type, count)))
+#define BOOST_MEMORY_NEW_ARRAY(alloc, Type, count) (alloc).newArray(BOOST_MEMORY_DBG_NEW_ARRAY_ARG(Type, count))
+
#if defined(BOOST_MEMORY_NO_STRICT_EXCEPTION_SEMANTICS)
//
// Backard options:
@@ -373,17 +324,19 @@
//
#define BOOST_MEMORY_NEW(alloc, Type) \
::new((alloc).allocate(BOOST_MEMORY_DBG_NEW_ARG(Type))) Type
+
#else
+
+#define BOOST_MEMORY_UNMANAGED_NEW(alloc, Type) \
+ ::new((alloc).unmanaged_alloc(BOOST_MEMORY_NEW_ARG(Type))) Type
+
#define BOOST_MEMORY_GET_MANAGED_(alloc, Type) \
NS_BOOST_MEMORY::_get_managed(alloc, NS_BOOST_MEMORY::destructor_traits<Type>::destruct)
+
#define BOOST_MEMORY_NEW(alloc, Type) \
BOOST_MEMORY_GET_MANAGED_(alloc, Type) ->* BOOST_MEMORY_UNMANAGED_NEW(alloc, Type)
-#endif
-#define BOOST_MEMORY_NEW_ARRAY(alloc, Type, count) (alloc).newArray(BOOST_MEMORY_DBG_NEW_ARRAY_ARG(Type, count))
-
-#define BOOST_MEMORY_ALLOC(alloc, Type) ((Type*)(alloc).allocate(BOOST_MEMORY_DBG_ALLOC_ARG(Type)))
-#define BOOST_MEMORY_ALLOC_ARRAY(alloc, Type, count) ((Type*)(alloc).allocate(BOOST_MEMORY_DBG_ALLOC_ARRAY_ARG(Type, count)))
+#endif
// =========================================================================
Modified: sandbox/memory/boost/memory/gc_alloc.hpp
==============================================================================
--- sandbox/memory/boost/memory/gc_alloc.hpp (original)
+++ sandbox/memory/boost/memory/gc_alloc.hpp 2008-05-07 11:19:24 EDT (Wed, 07 May 2008)
@@ -414,46 +414,44 @@
m_destroyCount = 0;
}
- void* BOOST_MEMORY_CALL allocate(const size_t cbData)
+private:
+ void* BOOST_MEMORY_CALL _do_allocate(const size_t cb, const size_t cbData)
{
- const size_t cb = BOOST_MEMORY_ALIGN(cbData, AlignSize) + sizeof(MemHeader);
- if ((size_t)(m_end - m_begin) < cb)
+ if ((size_t)(m_end - m_begin) >= cbData)
{
- if ((size_t)(m_end - m_begin) >= cbData)
+ MemHeader* pAlloc = (MemHeader*)m_begin - 1;
+ pAlloc->dataMemHeader = (nodeAlloced | (m_end - (char*)pAlloc));
+ m_begin = m_end = _null.begin();
+ return pAlloc + 1;
+ }
+
+ FreeMemHeader* pNew;
+ if (cb >= AllocSizeBig)
+ {
+ if (cb >= BlockSize)
{
- MemHeader* pAlloc = (MemHeader*)m_begin - 1;
- pAlloc->dataMemHeader = (nodeAlloced | (m_end - (char*)pAlloc));
- m_begin = m_end = _null.begin();
+ if (cb >= AllocSizeHuge)
+ return m_hugeAlloc.allocate(cbData);
+
+ MemHeader* pAlloc = (MemHeader*)_newBlock(cb + HeaderSize);
+ pAlloc->dataMemHeader |= nodeAlloced;
return pAlloc + 1;
}
- FreeMemHeader* pNew;
- if (cb >= AllocSizeBig)
- {
- if (cb >= BlockSize)
- {
- if (cb >= AllocSizeHuge)
- return m_hugeAlloc.allocate(cbData);
-
- MemHeader* pAlloc = (MemHeader*)_newBlock(cb + HeaderSize);
- pAlloc->dataMemHeader |= nodeAlloced;
- return pAlloc + 1;
- }
+ pNew = _newBlock(MemBlockSize);
+ }
+ else
+ {
+ try_gc();
+ if (m_freeList.empty() || (pNew = m_freeList.top())->getNodeSize() < cb) {
pNew = _newBlock(MemBlockSize);
}
- else
- {
- try_gc();
- if (m_freeList.empty() || (pNew = m_freeList.top())->getNodeSize() < cb) {
- pNew = _newBlock(MemBlockSize);
- }
- else {
- m_freeList.pop();
- }
+ else {
+ m_freeList.pop();
}
- _commitCurrentNode();
- m_begin = pNew->begin();
- m_end = pNew->end();
}
+ _commitCurrentNode();
+ m_begin = pNew->begin();
+ m_end = pNew->end();
BOOST_MEMORY_ASSERT((size_t)(m_end - m_begin) >= cb);
@@ -462,6 +460,19 @@
return pAlloc + 1;
}
+public:
+ __forceinline void* BOOST_MEMORY_CALL allocate(const size_t cbData)
+ {
+ const size_t cb = BOOST_MEMORY_ALIGN(cbData, AlignSize) + sizeof(MemHeader);
+ if ((size_t)(m_end - m_begin) >= cb)
+ {
+ MemHeader* pAlloc = (MemHeader*)(m_end -= cb);
+ pAlloc->dataMemHeader = (nodeAlloced | cb);
+ return pAlloc + 1;
+ }
+ return _do_allocate(cb, cbData);
+ }
+
void* BOOST_MEMORY_CALL allocate(const size_t cbData0, destructor_t fn)
{
const size_t cbData = sizeof(DestroyInfo) + cbData0;
@@ -516,15 +527,11 @@
return p;
}
- void BOOST_MEMORY_CALL deallocate(void* pData, const size_t cbData)
+ __forceinline void BOOST_MEMORY_CALL deallocate(void* pData, const size_t cbData)
{
const size_t cb = BOOST_MEMORY_ALIGN(cbData, AlignSize) + sizeof(MemHeader);
- if (cb >= AllocSizeHuge)
- {
- m_hugeAlloc.deallocate(pData, cbData);
- }
- else
+ if (cb < AllocSizeHuge)
{
MemHeader* p = (MemHeader*)pData - 1;
BOOST_MEMORY_ASSERT(p->getNodeSize() >= cb);
@@ -533,6 +540,10 @@
p->freeNode();
m_freeSize += cb;
}
+ else
+ {
+ m_hugeAlloc.deallocate(pData, cbData);
+ }
}
void* BOOST_MEMORY_CALL reallocate(void* p, size_t oldSize, size_t newSize)
Modified: sandbox/memory/libs/memory/test/test_basic/memory/performance.cpp
==============================================================================
--- sandbox/memory/libs/memory/test/test_basic/memory/performance.cpp (original)
+++ sandbox/memory/libs/memory/test/test_basic/memory/performance.cpp 2008-05-07 11:19:24 EDT (Wed, 07 May 2008)
@@ -29,8 +29,6 @@
{
private:
NS_BOOST_DETAIL::accumulator m_acc;
- NS_BOOST_MEMORY::block_pool m_recycle;
-
static Type* p[Total];
public:
@@ -153,11 +151,12 @@
template <class LogT2>
void doScopedAlloc(LogT2& log, int NAlloc, int PerAlloc)
{
+ NS_BOOST_MEMORY::block_pool& recycle = boost::tls_block_pool::instance();
NS_BOOST_DETAIL::performance_counter counter;
{
for (int j = 0; j < NAlloc; ++j)
{
- boost::scoped_alloc alloc(m_recycle);
+ boost::scoped_alloc alloc(recycle);
for (int i = 0; i < PerAlloc; ++i)
{
Type* p = BOOST_NEW(alloc, Type);
@@ -170,11 +169,12 @@
template <class LogT2>
void doGcAlloc(LogT2& log, int NAlloc, int PerAlloc)
{
+ NS_BOOST_MEMORY::block_pool& recycle = boost::tls_block_pool::instance();
NS_BOOST_DETAIL::performance_counter counter;
{
for (int j = 0; j < NAlloc; ++j)
{
- boost::gc_alloc alloc(m_recycle);
+ boost::gc_alloc alloc(recycle);
for (int i = 0; i < PerAlloc; ++i)
{
Type* p = BOOST_NEW(alloc, Type);
@@ -189,11 +189,12 @@
const int PerAlloc1 = PerAlloc/2;
const int PerAlloc2 = PerAlloc - PerAlloc1;
int i;
+ NS_BOOST_MEMORY::block_pool& recycle = boost::tls_block_pool::instance();
NS_BOOST_DETAIL::performance_counter counter;
{
for (int j = 0; j < NAlloc; ++j)
{
- boost::gc_alloc alloc(m_recycle);
+ boost::gc_alloc alloc(recycle);
for (i = 0; i < PerAlloc1; ++i)
p[i] = BOOST_NEW(alloc, Type);
for (i = 0; i < PerAlloc1; ++i)
@@ -276,7 +277,6 @@
doAutoAlloc(nullLog, 1, Total);
doTlsScopedAlloc(nullLog, 1, Total);
- doScopedAlloc(nullLog, 1, Total);
doComparison(log, Total);
doComparison(log, 1000);
Modified: sandbox/memory/libs/memory/test/test_basic/test.dsp
==============================================================================
--- sandbox/memory/libs/memory/test/test_basic/test.dsp (original)
+++ sandbox/memory/libs/memory/test/test_basic/test.dsp 2008-05-07 11:19:24 EDT (Wed, 07 May 2008)
@@ -42,7 +42,7 @@
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c
-# ADD CPP /nologo /MD /W3 /GX /Zi /O2 /I "../../../../" /I "../../../../../boost/" /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FD /c
+# ADD CPP /nologo /MD /W3 /GX /Zi /O2 /I "../../../../" /I "../../../../../boost/" /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FAs /FD /c
# SUBTRACT CPP /YX /Yc /Yu
# ADD BASE RSC /l 0x804 /d "NDEBUG"
# ADD RSC /l 0x804 /d "NDEBUG"
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk