[svn-r9550] Purpose:

Code optimization

Description:
    Rework & move around some of the macros for querying balanced properties
of nodes to speed up tree balancing code.

Platforms tested:
    FreeBSD 4.10 (sleipnir) w/parallel
    Solaris 2.7 (arabica)
    Too minor to require h5committest
This commit is contained in:
Quincey Koziol 2004-11-18 15:11:50 -05:00
parent 8c97a51f70
commit 6ec311b1d3
2 changed files with 13 additions and 5 deletions

View File

@ -1575,15 +1575,17 @@ H5TB_swapkid(H5TB_NODE ** root, H5TB_NODE * ptr, int side)
static herr_t static herr_t
H5TB_balance(H5TB_NODE ** root, H5TB_NODE * ptr, int side, int added) H5TB_balance(H5TB_NODE ** root, H5TB_NODE * ptr, int side, int added)
{ {
H5TB_leaf olcnt, orcnt; /* Old left & right counts for node */
H5TB_flag odouble; /* Old 'double' status */
int deeper = added; /* 1 if sub-tree got longer; -1 if got shorter */ int deeper = added; /* 1 if sub-tree got longer; -1 if got shorter */
int odelta; int odelta;
int obal;
FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5TB_balance); FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5TB_balance);
while (NULL != ptr) { while (NULL != ptr) {
odelta = Delta(ptr, side); /* delta before the node was added */ olcnt = LeftCnt(ptr);
obal = UnBal(ptr); orcnt = RightCnt(ptr);
odouble = Double(ptr);
if (LEFT == side) /* One more/fewer left child: */ if (LEFT == side) /* One more/fewer left child: */
if (0 < added) if (0 < added)
ptr->lcnt++; /* LeftCnt(ptr)++ */ ptr->lcnt++; /* LeftCnt(ptr)++ */
@ -1595,6 +1597,7 @@ H5TB_balance(H5TB_NODE ** root, H5TB_NODE * ptr, int side, int added)
ptr->rcnt--; /* RightCnt(ptr)-- */ ptr->rcnt--; /* RightCnt(ptr)-- */
if (0 != deeper) if (0 != deeper)
{ /* One leg got longer or shorter: */ { /* One leg got longer or shorter: */
odelta = DeltaCnt(olcnt, orcnt, odouble, side); /* compute delta before the node was added */
if ((deeper < 0 && odelta < 0) || (deeper > 0 && odelta > 0)) if ((deeper < 0 && odelta < 0) || (deeper > 0 && odelta > 0))
{ /* Became too unbalanced: */ { /* Became too unbalanced: */
H5TB_NODE *kid; H5TB_NODE *kid;
@ -1623,7 +1626,7 @@ H5TB_balance(H5TB_NODE ** root, H5TB_NODE * ptr, int side, int added)
ptr = H5TB_swapkid(root, ptr, side); ptr = H5TB_swapkid(root, ptr, side);
} }
} }
else if (obal) else if (olcnt!=orcnt)
{ /* Just became balanced: */ { /* Just became balanced: */
ptr->flags &= ~H5TB_UNBAL; ptr->flags &= ~H5TB_UNBAL;
if (0 < deeper) if (0 < deeper)

View File

@ -59,14 +59,19 @@ typedef int (*H5TB_cmp_t)(const void *k1, const void *k2, int cmparg);
# define HasChild(n,s) ( Cnt(n,s)>0 ) # define HasChild(n,s) ( Cnt(n,s)>0 )
# define Heavy(n,s) ( (s) & (LeftCnt(n)>RightCnt(n) ? LEFT : \ # define Heavy(n,s) ( (s) & (LeftCnt(n)>RightCnt(n) ? LEFT : \
LeftCnt(n)==RightCnt(n) ? 0 : RIGHT)) LeftCnt(n)==RightCnt(n) ? 0 : RIGHT))
# define HeavyCnt(l,r,s) ( (s) & ((l)>(r) ? LEFT : (l)==(r) ? 0 : RIGHT))
# define Intern(n) ( LeftCnt(n) && RightCnt(n) ) # define Intern(n) ( LeftCnt(n) && RightCnt(n) )
# define UnBal(n) ( LeftCnt(n)>RightCnt(n) ? LEFT : \ # define UnBal(n) ( LeftCnt(n)>RightCnt(n) ? LEFT : \
LeftCnt(n)==RightCnt(n) ? 0 : RIGHT) LeftCnt(n)==RightCnt(n) ? 0 : RIGHT)
# define UnBalanced(n) ( LeftCnt(n)!=RightCnt(n) ? 1 : 0) # define UnBalanced(n) ( LeftCnt(n)!=RightCnt(n) )
# define UnBalancedCnt(l,r) ( (l)!=(r) )
# define Double(n) ( H5TB_DOUBLE & (n)->flags ) # define Double(n) ( H5TB_DOUBLE & (n)->flags )
# define Other(side) ( LEFT + RIGHT - (side) ) # define Other(side) ( LEFT + RIGHT - (side) )
# define Weight(n) ( Double(n) ? 2 : UnBalanced(n) ) # define Weight(n) ( Double(n) ? 2 : UnBalanced(n) )
# define WeightCnt(l,r,d) ( (d) ? 2 : UnBalancedCnt(l,r) )
# define Delta(n,s) ( Heavy(n,s) ? Weight(n) : -Weight(n) ) # define Delta(n,s) ( Heavy(n,s) ? Weight(n) : -Weight(n) )
# define DeltaCnt(l,r,d,s) ( HeavyCnt(l,r,s) ? WeightCnt(l,r,d) : \
-WeightCnt(l,r,d) )
# define SetFlags(n,s,b,i) ( ( -2<(b) && (b)<2 ? 0 : H5TB_DOUBLE ) \ # define SetFlags(n,s,b,i) ( ( -2<(b) && (b)<2 ? 0 : H5TB_DOUBLE ) \
| ( 0>(b) ? H5TB_HEAVY(s) : (b)>0 ? H5TB_HEAVY(Other(s)) : 0 ) \ | ( 0>(b) ? H5TB_HEAVY(s) : (b)>0 ? H5TB_HEAVY(Other(s)) : 0 ) \
| ( (i) ? H5TB_INTERN : 0 ) ) | ( (i) ? H5TB_INTERN : 0 ) )