evas/cserve2: Add binary search in server side

This commit is contained in:
Jean-Philippe Andre 2013-08-02 15:27:20 +09:00
parent f5fee255de
commit e95bd7755e
1 changed files with 45 additions and 15 deletions

View File

@ -509,21 +509,51 @@ _shared_index_entry_new(Shared_Index *si)
} }
static Index_Entry * static Index_Entry *
_shared_index_entry_find(Shared_Index *si, int id) _shared_index_entry_get_by_id(Shared_Index *si, unsigned int id)
{ {
int k, count; Index_Entry *obj;
const char *base;
int low = 0, high, start_high, elemsize;
int cur;
if (!si) return NULL; if (!si || !si->sa || !id)
count = cserve2_shared_array_count_get(si->sa); return NULL;
// FIXME: Linear search O(n) // FIXME: HACK (consider all arrays always sorted by id)
for (k = 0; k < count; k++) high = si->sa->header->emptyidx; // Should be si->sa->header->sortedidx
if (high > si->sa->header->count)
high = si->sa->header->count;
base = si->sa->ds->data + sizeof(Shared_Array_Header);
elemsize = si->sa->header->elemsize;
// Binary search
start_high = high;
while(high != low)
{ {
Index_Entry *ie; cur = low + ((high - low) / 2);
ie = cserve2_shared_array_item_data_get(si->sa, k); obj = (Index_Entry *) (base + (elemsize * cur));
if (!ie) break; if (!obj)
if (ie->id == id) return NULL;
return ie; if (obj->id == id)
return obj;
if (obj->id < id)
low = cur + 1;
else
high = cur;
}
// Linear search
for (cur = start_high; cur < si->sa->header->count; cur++)
{
obj = (Index_Entry *) (base + (elemsize * cur));
if (!obj)
return NULL;
if (!obj->id)
return NULL;
if (obj->id == id)
return obj;
} }
return NULL; return NULL;
@ -709,7 +739,7 @@ cserve2_shared_mempool_buffer_ref(Shared_Mempool *sm, int bufferid)
Index_Entry *ie; Index_Entry *ie;
if (!sm) return -1; if (!sm) return -1;
ie = _shared_index_entry_find(sm->index, bufferid); ie = _shared_index_entry_get_by_id(sm->index, bufferid);
if (!ie) return -1; if (!ie) return -1;
if (!ie->refcount) if (!ie->refcount)
@ -742,7 +772,7 @@ _shared_mempool_buffer_del(Shared_Mempool *sm, int bufferid)
if (!sm || !bufferid) return EINA_FALSE; if (!sm || !bufferid) return EINA_FALSE;
ie = _shared_index_entry_find(sm->index, bufferid); ie = _shared_index_entry_get_by_id(sm->index, bufferid);
if (!ie || ie->refcount <= 0) if (!ie || ie->refcount <= 0)
{ {
CRIT("Tried to delete invalid buffer or with refcount 0"); CRIT("Tried to delete invalid buffer or with refcount 0");
@ -778,7 +808,7 @@ cserve2_shared_mempool_buffer_get(Shared_Mempool *sm, int bufferid)
char *data; char *data;
if (!sm) return NULL; if (!sm) return NULL;
ie = _shared_index_entry_find(sm->index, bufferid); ie = _shared_index_entry_get_by_id(sm->index, bufferid);
if (!ie || ie->refcount <= 0) if (!ie || ie->refcount <= 0)
{ {
CRIT("Tried to access invalid buffer or with refcount 0"); CRIT("Tried to access invalid buffer or with refcount 0");
@ -823,7 +853,7 @@ cserve2_shared_string_add(const char *str)
id = (int) (intptr_t) eina_hash_find(_string_entries, str); id = (int) (intptr_t) eina_hash_find(_string_entries, str);
if (id > 0) if (id > 0)
{ {
ie = _shared_index_entry_find(_string_mempool->index, id); ie = _shared_index_entry_get_by_id(_string_mempool->index, id);
if (!ie || ie->refcount <= 0) if (!ie || ie->refcount <= 0)
{ {
CRIT("String found in hash but not in mempool!"); CRIT("String found in hash but not in mempool!");