Ignore:
Timestamp:
2018-07-03T05:58:47Z (6 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
c17d0af
Parents:
49ab3cb
git-author:
dequis <dx@…> (03-07-18 05:27:59)
git-committer:
dequis <dx@…> (03-07-18 05:58:47)
Message:

Add a hash table to speed up bee_user_by_handle()

This maintains a hash table next to the linked list, which results in
negligible additional memory usage (~300kb for 10k users) but allows
instant lookups.

This was a big problem with discord, which has huge user lists and joins
everyone to every channel. In my test, the GUILD_SYNC event for 10k-50k
user lists is now approximately 5 times faster.

This hash table based code is only used if handle_cmp is either
exact or case-insensitive string comparison (g_ascii_strcasecmp or
strcmp/g_strcmp0).

The old function that goes through the bee->users linked list is now
called bee_user_by_handle_slow() and used for protocols with unusual
handle_cmp functions - skimming through the code, just oscar.
May revisit this if it happens to more meaningful protocols.

The case-insensitive hashtable functions are copied from irssi, which is
also GPLv2. I renamed them from g_ to b_ (g_istr_equal to b_istr_equal)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/bee_user.c

    r49ab3cb r9e83b15  
    4242        bee->users = g_slist_prepend(bee->users, bu);
    4343
     44        if (ic->bee_users) {
     45                g_hash_table_insert(ic->bee_users, bu->handle, bu);
     46        }
    4447        if (bee->ui->user_new) {
    4548                bee->ui->user_new(bee, bu);
     
    6770                bu->ic->acc->prpl->buddy_data_free(bu);
    6871        }
    69 
     72        if (bu->ic->bee_users) {
     73                g_hash_table_remove(bu->ic->bee_users, bu->handle);
     74        }
    7075        bee->users = g_slist_remove(bee->users, bu);
    7176
     
    8085}
    8186
    82 bee_user_t *bee_user_by_handle(bee_t *bee, struct im_connection *ic, const char *handle)
     87bee_user_t *bee_user_by_handle_slow(bee_t *bee, struct im_connection *ic, const char *handle)
    8388{
    8489        GSList *l;
     
    9398
    9499        return NULL;
     100}
     101
     102bee_user_t *bee_user_by_handle(bee_t *bee, struct im_connection *ic, const char *handle)
     103{
     104        if (!ic->bee_users) {
     105                return bee_user_by_handle_slow(bee, ic, handle);
     106        }
     107
     108        return g_hash_table_lookup(ic->bee_users, handle);
    95109}
    96110
Note: See TracChangeset for help on using the changeset viewer.