fast path for sorted_array_insert

pull/37/merge
YuQing 2021-12-14 21:18:30 +08:00
parent 0c437d3799
commit f6c5256264
1 changed files with 26 additions and 19 deletions

View File

@ -59,9 +59,15 @@ static char *sorted_array_bsearch(SortedArrayContext *ctx, char *base,
int sorted_array_insert(SortedArrayContext *ctx,
void *base, int *count, const void *elt)
{
char *current;
if (*count == 0 || ctx->compare_func((char *)base +
ctx->element_size * (*count - 1), elt) < 0)
{ //fast path
current = (char *)base + ctx->element_size * (*count);
} else {
int insert_pos;
int move_count;
char *current;
char *found;
char *end;
@ -85,6 +91,7 @@ int sorted_array_insert(SortedArrayContext *ctx,
memmove((char *)base + ctx->element_size * (insert_pos + 1),
current, ctx->element_size * move_count);
}
}
switch (ctx->element_size) {
case 1: