Commit 6af0a664 authored by Mohammed Sadiq's avatar Mohammed Sadiq Committed by Mohammed Sadiq
Browse files

matrix: Speed up buddy search

hash the matrix id and compare the number instead of comparing
the matrix id string everytime
parent 9de38e1c
Pipeline #66709 passed with stages
in 8 minutes and 25 seconds
......@@ -29,6 +29,8 @@ struct _ChattyMaBuddy
MatrixApi *matrix_api;
MatrixEnc *matrix_enc;
/* generated using g_str_hash for faster comparison */
guint id_hash;
gboolean is_self;
};
......@@ -209,6 +211,17 @@ chatty_ma_buddy_get_id (ChattyMaBuddy *self)
return "";
}
guint
chatty_ma_buddy_get_id_hash (ChattyMaBuddy *self)
{
g_return_val_if_fail (CHATTY_IS_MA_BUDDY (self), 0);
if (!self->id_hash && self->matrix_id)
self->id_hash = g_str_hash (self->matrix_id);
return self->id_hash;
}
void
chatty_ma_buddy_add_devices (ChattyMaBuddy *self,
JsonObject *root)
......
......@@ -30,6 +30,7 @@ ChattyMaBuddy *chatty_ma_buddy_new (const char *matrix_id,
MatrixApi *api,
MatrixEnc *enc);
const char *chatty_ma_buddy_get_id (ChattyMaBuddy *self);
guint chatty_ma_buddy_get_id_hash (ChattyMaBuddy *self);
void chatty_ma_buddy_add_devices (ChattyMaBuddy *self,
JsonObject *root);
GList *chatty_ma_buddy_get_devices (ChattyMaBuddy *self);
......
......@@ -175,17 +175,21 @@ ma_chat_find_buddy (ChattyMaChat *self,
guint *index)
{
guint n_items;
guint id_hash;
g_assert (CHATTY_IS_MA_CHAT (self));
g_assert (G_IS_LIST_MODEL (model));
g_return_val_if_fail (matrix_id && *matrix_id, NULL);
n_items = g_list_model_get_n_items (model);
id_hash = g_str_hash (matrix_id);
for (guint i = 0; i < n_items; i++) {
g_autoptr(ChattyMaBuddy) buddy = NULL;
buddy = g_list_model_get_item (model, i);
if (g_str_equal (chatty_ma_buddy_get_id (buddy), matrix_id)) {
if (id_hash == chatty_ma_buddy_get_id_hash (buddy) &&
g_str_equal (chatty_ma_buddy_get_id (buddy), matrix_id)) {
if (index)
*index = i;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment