mirror of
https://github.com/boostorg/regex.git
synced 2025-07-17 14:22:08 +02:00
Made the mem_cache_block lockfree
This *significantly* improves parallel performance of regex. Currently if I have a large number of threads all using regexes; even if they are using idependent regex objects, performance is still extremely poor due to the lock inside of the mem_block_cache.
This commit is contained in:
@ -19,9 +19,7 @@
|
|||||||
#define BOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
|
#define BOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
|
||||||
|
|
||||||
#include <new>
|
#include <new>
|
||||||
#ifdef BOOST_HAS_THREADS
|
#include <boost/atomic/atomic.hpp>
|
||||||
#include <boost/regex/pending/static_mutex.hpp>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef BOOST_HAS_ABI_HEADERS
|
#ifdef BOOST_HAS_ABI_HEADERS
|
||||||
# include BOOST_ABI_PREFIX
|
# include BOOST_ABI_PREFIX
|
||||||
@ -30,59 +28,41 @@
|
|||||||
namespace boost{
|
namespace boost{
|
||||||
namespace BOOST_REGEX_DETAIL_NS{
|
namespace BOOST_REGEX_DETAIL_NS{
|
||||||
|
|
||||||
struct mem_block_node
|
|
||||||
{
|
|
||||||
mem_block_node* next;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct mem_block_cache
|
struct mem_block_cache
|
||||||
{
|
{
|
||||||
// this member has to be statically initialsed:
|
boost::atomic<void*> cache[BOOST_REGEX_MAX_CACHE_BLOCKS];
|
||||||
mem_block_node* next;
|
|
||||||
unsigned cached_blocks;
|
|
||||||
#ifdef BOOST_HAS_THREADS
|
|
||||||
boost::static_mutex mut;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
mem_block_cache() {
|
||||||
|
for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
|
||||||
|
cache[i].store(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
~mem_block_cache()
|
~mem_block_cache()
|
||||||
{
|
{
|
||||||
while(next)
|
for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
|
||||||
{
|
if (cache[i].load()) ::operator delete(cache[i].load());
|
||||||
mem_block_node* old = next;
|
}
|
||||||
next = next->next;
|
|
||||||
::operator delete(old);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void* get()
|
void* get()
|
||||||
{
|
{
|
||||||
#ifdef BOOST_HAS_THREADS
|
for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
|
||||||
boost::static_mutex::scoped_lock g(mut);
|
void* p = cache[i].load();
|
||||||
#endif
|
if (p != NULL) {
|
||||||
if(next)
|
if (cache[i].compare_exchange_strong(p, NULL)) return p;
|
||||||
{
|
}
|
||||||
mem_block_node* result = next;
|
}
|
||||||
next = next->next;
|
return ::operator new(BOOST_REGEX_BLOCKSIZE);
|
||||||
--cached_blocks;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
return ::operator new(BOOST_REGEX_BLOCKSIZE);
|
|
||||||
}
|
}
|
||||||
void put(void* p)
|
void put(void* ptr)
|
||||||
{
|
{
|
||||||
#ifdef BOOST_HAS_THREADS
|
for (size_t i = 0;i < BOOST_REGEX_MAX_CACHE_BLOCKS; ++i) {
|
||||||
boost::static_mutex::scoped_lock g(mut);
|
void* p = cache[i].load();
|
||||||
#endif
|
if (p == NULL) {
|
||||||
if(cached_blocks >= BOOST_REGEX_MAX_CACHE_BLOCKS)
|
if (cache[i].compare_exchange_strong(p, ptr)) return;
|
||||||
{
|
}
|
||||||
::operator delete(p);
|
}
|
||||||
}
|
::operator delete(ptr);
|
||||||
else
|
|
||||||
{
|
|
||||||
mem_block_node* old = static_cast<mem_block_node*>(p);
|
|
||||||
old->next = next;
|
|
||||||
next = old;
|
|
||||||
++cached_blocks;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -192,9 +192,9 @@ BOOST_REGEX_DECL void BOOST_REGEX_CALL put_mem_block(void* p)
|
|||||||
#else
|
#else
|
||||||
|
|
||||||
#ifdef BOOST_HAS_THREADS
|
#ifdef BOOST_HAS_THREADS
|
||||||
mem_block_cache block_cache = { 0, 0, BOOST_STATIC_MUTEX_INIT, };
|
mem_block_cache block_cache;
|
||||||
#else
|
#else
|
||||||
mem_block_cache block_cache = { 0, 0, };
|
mem_block_cache block_cache;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
BOOST_REGEX_DECL void* BOOST_REGEX_CALL get_mem_block()
|
BOOST_REGEX_DECL void* BOOST_REGEX_CALL get_mem_block()
|
||||||
|
Reference in New Issue
Block a user