mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-02 03:47:53 +08:00
improvement ring's performance
This commit is contained in:
parent
a0b258eb28
commit
85812e6f1c
@ -22,6 +22,7 @@ CFLAGS = -c -g -W \
|
||||
-Wmissing-prototypes \
|
||||
-Wcast-qual \
|
||||
-DUSE_FAST_TIME \
|
||||
-DUSE_FAST_RING \
|
||||
#-DUSE_VALGRIND \
|
||||
#-I/usr/local/include
|
||||
#-DUSE_PRINTF_MACRO
|
||||
|
@ -2,11 +2,11 @@
|
||||
|
||||
#include "ring.h"
|
||||
|
||||
#ifndef USE_FAST_RING
|
||||
|
||||
/* ring_init - initialize ring head */
|
||||
void ring_init(RING *ring)
|
||||
{
|
||||
if (ring == NULL)
|
||||
return;
|
||||
ring->pred = ring->succ = ring;
|
||||
ring->parent = ring;
|
||||
ring->len = 0;
|
||||
@ -16,9 +16,6 @@ void ring_init(RING *ring)
|
||||
|
||||
int ring_size(const RING *ring)
|
||||
{
|
||||
if (ring == NULL)
|
||||
return -1;
|
||||
|
||||
return ring->len;
|
||||
}
|
||||
|
||||
@ -26,8 +23,6 @@ int ring_size(const RING *ring)
|
||||
|
||||
void ring_append(RING *ring, RING *entry)
|
||||
{
|
||||
if (ring == NULL || entry == NULL)
|
||||
return;
|
||||
entry->succ = ring->succ;
|
||||
entry->pred = ring;
|
||||
entry->parent = ring->parent;
|
||||
@ -40,8 +35,6 @@ void ring_append(RING *ring, RING *entry)
|
||||
|
||||
void ring_prepend(RING *ring, RING *entry)
|
||||
{
|
||||
if (ring == NULL || entry == NULL)
|
||||
return;
|
||||
entry->pred = ring->pred;
|
||||
entry->succ = ring;
|
||||
entry->parent = ring->parent;
|
||||
@ -54,40 +47,35 @@ void ring_prepend(RING *ring, RING *entry)
|
||||
|
||||
void ring_detach(RING *entry)
|
||||
{
|
||||
RING *succ;
|
||||
RING *pred;
|
||||
RING *succ, *pred;
|
||||
|
||||
if (entry == NULL || entry->parent == entry)
|
||||
return;
|
||||
succ = entry->succ;
|
||||
pred = entry->pred;
|
||||
if (succ == NULL || pred == NULL)
|
||||
return;
|
||||
pred->succ = succ;
|
||||
succ->pred = pred;
|
||||
if (entry->parent != entry) {
|
||||
succ = entry->succ;
|
||||
pred = entry->pred;
|
||||
if (succ && pred) {
|
||||
pred->succ = succ;
|
||||
succ->pred = pred;
|
||||
|
||||
entry->parent->len--;
|
||||
|
||||
entry->succ = entry->pred = entry;
|
||||
entry->parent = entry;
|
||||
entry->len = 0;
|
||||
entry->parent->len--;
|
||||
entry->succ = entry->pred = entry;
|
||||
entry->parent = entry;
|
||||
entry->len = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ring_pop_head - pop ring's head entry out from ring */
|
||||
|
||||
RING *ring_pop_head(RING *ring)
|
||||
{
|
||||
RING *succ;
|
||||
|
||||
if (ring == NULL)
|
||||
return NULL;
|
||||
RING *succ;
|
||||
|
||||
succ = ring->succ;
|
||||
if (succ == ring)
|
||||
if (succ == ring) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ring_detach(succ);
|
||||
|
||||
return succ;
|
||||
}
|
||||
|
||||
@ -95,16 +83,15 @@ RING *ring_pop_head(RING *ring)
|
||||
|
||||
RING *ring_pop_tail(RING *ring)
|
||||
{
|
||||
RING *pred;
|
||||
|
||||
if (ring == NULL)
|
||||
return NULL;
|
||||
RING *pred;
|
||||
|
||||
pred = ring->pred;
|
||||
if (pred == ring)
|
||||
if (pred == ring) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ring_detach(pred);
|
||||
|
||||
return pred;
|
||||
}
|
||||
|
||||
#endif /* USE_FAST_RING */
|
||||
|
@ -28,48 +28,133 @@ typedef struct RING_ITER {
|
||||
* 初始化数据环
|
||||
* @param ring {RING*} 数据环
|
||||
*/
|
||||
#ifdef USE_FAST_RING
|
||||
#define ring_init(__ring) do { \
|
||||
RING *_ring = __ring; \
|
||||
_ring->pred = _ring->succ = _ring; \
|
||||
_ring->parent = _ring; \
|
||||
_ring->len = 0; \
|
||||
} while (0)
|
||||
#else
|
||||
void ring_init(RING *ring);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* 获得当前数据环内元素个数
|
||||
* @param ring {RING*} 数据环
|
||||
* @return {int} 数据环内元素个数
|
||||
*/
|
||||
#ifdef USE_FAST_RING
|
||||
#define ring_size(r) (((RING*)(r))->len)
|
||||
#else
|
||||
int ring_size(const RING *ring);
|
||||
|
||||
/**
|
||||
* 将一个新元素添加进环的头部
|
||||
* @param ring {RING*} 数据环
|
||||
* @param entry {RING*} 新的元素
|
||||
*/
|
||||
void ring_prepend(RING *ring, RING *entry);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* 将一个新元素添加进环的尾部
|
||||
* @param ring {RING*} 数据环
|
||||
* @param entry {RING*} 新的元素
|
||||
*/
|
||||
#ifdef USE_FAST_RING
|
||||
#define ring_append(r, e) do { \
|
||||
((RING*)(e))->succ = ((RING*)(r))->succ; \
|
||||
((RING*)(e))->pred = (RING*)(r); \
|
||||
((RING*)(e))->parent = ((RING*)(r))->parent; \
|
||||
((RING*)(r))->succ->pred = (RING*)(e); \
|
||||
((RING*)(r))->succ = (RING*)(e); \
|
||||
((RING*)(r))->parent->len++; \
|
||||
} while (0)
|
||||
|
||||
#else
|
||||
void ring_append(RING *ring, RING *entry);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* 将一个新元素添加进环的头部
|
||||
* @param ring {RING*} 数据环
|
||||
* @param entry {RING*} 新的元素
|
||||
*/
|
||||
#ifdef USE_FAST_RING
|
||||
#define ring_prepend(r, e) do { \
|
||||
((RING*)(e))->pred = ((RING*)(r))->pred; \
|
||||
((RING*)(e))->succ = (RING*)(r); \
|
||||
((RING*)(e))->parent = ((RING*)(r))->parent; \
|
||||
((RING*)(r))->pred->succ = (RING*)(e); \
|
||||
((RING*)(r))->pred = (RING*)(e); \
|
||||
((RING*)(r))->parent->len++; \
|
||||
} while (0)
|
||||
#else
|
||||
void ring_prepend(RING *ring, RING *entry);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* 将一个环元素从数据环中删除
|
||||
* @param entry {RING*} 环元素
|
||||
*/
|
||||
#ifdef USE_FAST_RING
|
||||
#define ring_detach(e) do { \
|
||||
RING *_succ, *_pred; \
|
||||
if (((RING*)(e))->parent != (RING*)(e)) { \
|
||||
_succ = ((RING*)(e))->succ; \
|
||||
_pred = ((RING*)(e))->pred; \
|
||||
if (_succ && _pred) { \
|
||||
_pred->succ = _succ; \
|
||||
_succ->pred = _pred; \
|
||||
((RING*)(e))->parent->len--; \
|
||||
((RING*)(e))->succ = (RING*)(e); \
|
||||
((RING*)(e))->pred = (RING*)(e); \
|
||||
((RING*)(e))->parent = (RING*)(e); \
|
||||
((RING*)(e))->len = 0; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
#else
|
||||
void ring_detach(RING *entry);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* 从环中弹出头部环元素
|
||||
* @param ring {RING*} 数据环
|
||||
* @return {RING*} 头部环元素,如果返回空则表示该数据环为空
|
||||
*/
|
||||
#ifdef USE_FAST_RING
|
||||
static inline RING *ring_pop_head(RING *ring)
|
||||
{
|
||||
RING *succ;
|
||||
|
||||
succ = ring->succ;
|
||||
if (succ == ring) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ring_detach(succ);
|
||||
return succ;
|
||||
}
|
||||
#else
|
||||
RING *ring_pop_head(RING *ring);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* 从环中弹出尾部环元素
|
||||
* @param ring {RING*} 数据环
|
||||
* @return {RING*} 尾部环元素,如果返回空则表示该数据环为空
|
||||
*/
|
||||
#ifdef USE_FAST_RING
|
||||
static inline RING *ring_pop_tail(RING *ring)
|
||||
{
|
||||
RING *pred;
|
||||
|
||||
pred = ring->pred;
|
||||
if (pred == ring) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ring_detach(pred);
|
||||
return pred;
|
||||
}
|
||||
#else
|
||||
RING *ring_pop_tail(RING *ring);
|
||||
#endif
|
||||
|
||||
/*-------------------- 一些方便快捷的宏操作 --------------------------------*/
|
||||
|
||||
|
@ -1,4 +1,8 @@
|
||||
|
||||
109) 2019.7.31
|
||||
109.1) performance: 因为 ring.h 中的方法被调用次数比较频繁,所以将其中代码实现
|
||||
放在头文件中,成为内联函数或宏定义
|
||||
|
||||
108) 2019.7.30
|
||||
108.1) performance: 从 DPDK 摘取代码用以实现 getimeofday() 从而获得更高的性能
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user