uniq_skiplist_clear impl. more optimization

fstore_storage_engine
YuQing 2023-05-09 07:45:11 +08:00
parent 2c5734ab22
commit 5247caa71a
3 changed files with 42 additions and 28 deletions

View File

@ -312,13 +312,17 @@ int main(int argc, char *argv[])
test_delete(); test_delete();
printf("\n"); printf("\n");
test_clear();
printf("\n");
test_insert(); test_insert();
printf("\n"); printf("\n");
test_clear(); test_clear();
printf("\n"); printf("\n");
printf("skiplist level_count: %d\n", sl->top_level_index + 1); test_insert();
printf("\n");
uniq_skiplist_free(sl); uniq_skiplist_free(sl);
fast_mblock_manager_stat_print(false); fast_mblock_manager_stat_print(false);

View File

@ -161,12 +161,24 @@ void uniq_skiplist_destroy(UniqSkiplistFactory *factory)
factory->node_allocators = NULL; factory->node_allocators = NULL;
} }
static inline void init_chain(UniqSkiplist *sl)
{
int i;
if (sl->factory->bidirection) {
LEVEL0_DOUBLE_CHAIN_TAIL(sl) = sl->top;
}
for (i=0; i<=sl->top_level_index; i++) {
sl->top->links[i] = sl->factory->tail;
}
}
UniqSkiplist *uniq_skiplist_new(UniqSkiplistFactory *factory, UniqSkiplist *uniq_skiplist_new(UniqSkiplistFactory *factory,
const int level_count) const int level_count)
{ {
UniqSkiplist *sl; UniqSkiplist *sl;
struct fast_mblock_man *top_mblock; struct fast_mblock_man *top_mblock;
int i;
if (level_count <= 0) { if (level_count <= 0) {
logError("file: "__FILE__", line: %d, " logError("file: "__FILE__", line: %d, "
@ -199,13 +211,7 @@ UniqSkiplist *uniq_skiplist_new(UniqSkiplistFactory *factory,
} }
memset(sl->top, 0, top_mblock->info.element_size); memset(sl->top, 0, top_mblock->info.element_size);
sl->top->level_index = sl->top_level_index; sl->top->level_index = sl->top_level_index;
if (sl->factory->bidirection) { init_chain(sl);
LEVEL0_DOUBLE_CHAIN_TAIL(sl) = sl->top;
}
for (i=0; i<level_count; i++) {
sl->top->links[i] = sl->factory->tail;
}
return sl; return sl;
} }
@ -257,15 +263,11 @@ static int uniq_skiplist_grow(UniqSkiplist *sl)
return 0; return 0;
} }
void uniq_skiplist_free(UniqSkiplist *sl) static void do_clear(UniqSkiplist *sl)
{ {
volatile UniqSkiplistNode *node; volatile UniqSkiplistNode *node;
volatile UniqSkiplistNode *deleted; volatile UniqSkiplistNode *deleted;
if (sl->top == NULL) {
return;
}
node = sl->top->links[0]; node = sl->top->links[0];
if (sl->factory->free_func != NULL) { if (sl->factory->free_func != NULL) {
while (node != sl->factory->tail) { while (node != sl->factory->tail) {
@ -286,11 +288,30 @@ void uniq_skiplist_free(UniqSkiplist *sl)
} }
} }
sl->element_count = 0;
}
void uniq_skiplist_clear(UniqSkiplist *sl)
{
if (!uniq_skiplist_empty(sl)) {
do_clear(sl);
init_chain(sl);
}
}
void uniq_skiplist_free(UniqSkiplist *sl)
{
if (sl->top == NULL) {
return;
}
if (!uniq_skiplist_empty(sl)) {
do_clear(sl);
}
fast_mblock_free_object(sl->factory->node_allocators + fast_mblock_free_object(sl->factory->node_allocators +
sl->top_level_index, sl->top); sl->top_level_index, sl->top);
sl->top = NULL; sl->top = NULL;
sl->element_count = 0;
sl->top_level_index = 0; sl->top_level_index = 0;
fast_mblock_free_object(&sl->factory->skiplist_allocator, sl); fast_mblock_free_object(&sl->factory->skiplist_allocator, sl);
} }

View File

@ -253,18 +253,7 @@ static inline bool uniq_skiplist_empty(UniqSkiplist *sl)
return sl->top->links[0] == sl->factory->tail; return sl->top->links[0] == sl->factory->tail;
} }
static inline void uniq_skiplist_clear(UniqSkiplist *sl) void uniq_skiplist_clear(UniqSkiplist *sl);
{
volatile UniqSkiplistNode *current;
volatile UniqSkiplistNode *deleted;
current = sl->top->links[0];
while (current != sl->factory->tail) {
deleted = current;
current = current->links[0];
uniq_skiplist_delete(sl, deleted->data);
}
}
#define LEVEL0_DOUBLE_CHAIN_NEXT_LINK(node) node->links[0] #define LEVEL0_DOUBLE_CHAIN_NEXT_LINK(node) node->links[0]
#define LEVEL0_DOUBLE_CHAIN_PREV_LINK(node) node->links[node->level_index + 1] #define LEVEL0_DOUBLE_CHAIN_PREV_LINK(node) node->links[node->level_index + 1]