Skip to content

Commit

Permalink
Optimize btree/find_closest a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoebi committed Aug 1, 2015
1 parent f7c2128 commit 351b4e8
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions sapi/phpdbg/phpdbg_btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,49 +66,52 @@ phpdbg_btree_result *phpdbg_btree_find(phpdbg_btree *tree, zend_ulong idx) {
phpdbg_btree_result *phpdbg_btree_find_closest(phpdbg_btree *tree, zend_ulong idx) {
phpdbg_btree_branch *branch = tree->branch;
int i = tree->depth - 1, last_superior_i = -1;
zend_bool had_alternative_branch = 0;

if (branch == NULL) {
return NULL;
}

/* find nearest watchpoint */
do {
/* an impossible branch was found if: */
if (!had_alternative_branch && (idx >> i) % 2 == 0 && !branch->branches[0]) {
/* there's no lower branch than idx */
if (last_superior_i == -1) {
/* failure */
return NULL;
}
/* reset state */
branch = tree->branch;
i = tree->depth - 1;
/* follow branch according to bits in idx until the last lower branch before the impossible branch */
do {
CHOOSE_BRANCH((idx >> i) % 2 == 1 && branch->branches[1]);
} while (--i > last_superior_i);
/* use now the lower branch of which we can be sure that it contains only branches lower than idx */
CHOOSE_BRANCH(0);
/* and choose the highest possible branch in the branch containing only branches lower than idx */
while (i--) {
CHOOSE_BRANCH(branch->branches[1]);
if ((idx >> i) % 2 == 0) {
if (branch->branches[0]) {
CHOOSE_BRANCH(0);
/* an impossible branch was found if: */
} else {
/* there's no lower branch than idx */
if (last_superior_i == -1) {
/* failure */
return NULL;
}
/* reset state */
branch = tree->branch;
i = tree->depth - 1;
/* follow branch according to bits in idx until the last lower branch before the impossible branch */
do {
CHOOSE_BRANCH((idx >> i) % 2 == 1 && branch->branches[1]);
} while (--i > last_superior_i);
/* use now the lower branch of which we can be sure that it contains only branches lower than idx */
CHOOSE_BRANCH(0);
/* and choose the highest possible branch in the branch containing only branches lower than idx */
while (i--) {
CHOOSE_BRANCH(branch->branches[1]);
}
break;
}
break;
}
/* follow branch according to bits in idx until having found an impossible branch */
if (had_alternative_branch || (idx >> i) % 2 == 1) {
} else {
if (branch->branches[1]) {
if (branch->branches[0]) {
last_superior_i = i;
}
CHOOSE_BRANCH(1);
} else {
CHOOSE_BRANCH(0);
had_alternative_branch = 1;
while (i--) {
CHOOSE_BRANCH(branch->branches[1]);
}
break;
}
} else {
CHOOSE_BRANCH(0);
}
} while (i--);

Expand Down

0 comments on commit 351b4e8

Please sign in to comment.