improvement ring's performance

This commit is contained in:
zhengshuxin 2019-07-31 22:00:41 +08:00
parent a0b258eb28
commit 85812e6f1c
4 changed files with 120 additions and 43 deletions

View File

@ -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

View File

@ -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 */

View File

@ -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
/*-------------------- 一些方便快捷的宏操作 --------------------------------*/

View File

@ -1,4 +1,8 @@
109) 2019.7.31
109.1) performance: 因为 ring.h 中的方法被调用次数比较频繁,所以将其中代码实现
放在头文件中,成为内联函数或宏定义
108) 2019.7.30
108.1) performance: 从 DPDK 摘取代码用以实现 getimeofday() 从而获得更高的性能