source: lib/misc.h @ 50d0a72

Last change on this file since 50d0a72 was 9e83b15, checked in by dequis <dx@…>, at 2018-07-03T05:58:47Z

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)

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[5ebff60]1/********************************************************************\
[e27661d]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[0e788f5]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[e27661d]5  \********************************************************************/
6
7/* Misc. functions                                                      */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[e27661d]24*/
25
[1719464]26#ifndef _MISC_H
27#define _MISC_H
28
29#include <gmodule.h>
30#include <time.h>
[60141cf]31#include <sys/socket.h>
[e27661d]32
[5ebff60]33struct ns_srv_reply {
[36e9f62]34        int prio;
35        int weight;
36        int port;
37        char name[];
38};
39
[632627e]40#ifndef NAMESER_HAS_NS_TYPES
41
42#define NS_MAXDNAME 1025
43#define NS_INT16SZ  2
44#define NS_INT32SZ  4
45
46#define NS_GET16(s, cp) do { \
[5ebff60]47                register const unsigned char *t_cp = (const unsigned char *) (cp); \
48                (s) = ((guint16) t_cp[0] << 8) \
49                      | ((guint16) t_cp[1]) \
50                ; \
51                (cp) += NS_INT16SZ; \
52} while (0)
[632627e]53
54#define NS_GET32(s, cp) do { \
[5ebff60]55                register const unsigned char *t_cp = (const unsigned char *) (cp); \
56                (s) = ((guint16) t_cp[0] << 24) \
57                      | ((guint16) t_cp[1] << 16) \
58                      | ((guint16) t_cp[2] << 8) \
59                      | ((guint16) t_cp[3]) \
60                ; \
61                (cp) += NS_INT32SZ; \
62} while (0)
[632627e]63
64#define ns_rr_rdlen(rr) ((rr).rdlength + 0)
65#define ns_rr_rdata(rr) ((rr).rdata + 0)
66
67struct _ns_flagdata { int mask, shift; };
68
69typedef struct __ns_rr {
70        char name[NS_MAXDNAME];
71        guint16 type;
72        guint16 rr_class;
73        guint32 ttl;
74        guint16 rdlength;
75        const unsigned char* rdata;
76} ns_rr;
77
78typedef enum __ns_sect {
79        ns_s_qd = 0,
80        ns_s_zn = 0,
81        ns_s_an = 1,
82        ns_s_pr = 1,
83        ns_s_ns = 2,
84        ns_s_ud = 2,
85        ns_s_ar = 3,
[5ebff60]86        ns_s_max = 4
[632627e]87} ns_sect;
88
[5ebff60]89typedef struct __ns_msg {
[632627e]90        const unsigned char* _msg;
91        const unsigned char* _eom;
92        guint16 _id;
93        guint16 _flags;
94        guint16 _counts[ns_s_max];
95        const unsigned char* _sections[ns_s_max];
96        ns_sect _sect;
97        int _rrnum;
98        const unsigned char* _msg_ptr;
99} ns_msg;
100
101typedef enum __ns_class {
102        ns_c_invalid = 0,
103        ns_c_in = 1,
104        ns_c_2 = 2,
105        ns_c_chaos = 3,
106        ns_c_hs = 4,
107        ns_c_none = 254,
108        ns_c_any = 255,
109        ns_c_max = 65536
110} ns_class;
111
112
113/* TODO : fill out the rest */
114typedef enum __ns_type {
115        ns_t_srv = 33
116} ns_type;
117
118#endif /* NAMESER_HAS_NS_INITPARSE */
119
[5ebff60]120G_MODULE_EXPORT void strip_linefeed(gchar *text);
121G_MODULE_EXPORT char *add_cr(char *text);
[e27661d]122G_MODULE_EXPORT char *strip_newlines(char *source);
123
[5ebff60]124G_MODULE_EXPORT time_t get_time(int year, int month, int day, int hour, int min, int sec);
125G_MODULE_EXPORT time_t mktime_utc(struct tm *tp);
126double gettime(void);
[e27661d]127
[5ebff60]128G_MODULE_EXPORT void strip_html(char *msg);
129G_MODULE_EXPORT char *escape_html(const char *html);
130G_MODULE_EXPORT void http_decode(char *s);
131G_MODULE_EXPORT void http_encode(char *s);
[e27661d]132
[5ebff60]133G_MODULE_EXPORT signed int do_iconv(char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf);
[1719464]134
[5ebff60]135G_MODULE_EXPORT void random_bytes(unsigned char *buf, int count);
[e27661d]136
[5ebff60]137G_MODULE_EXPORT int is_bool(char *value);
138G_MODULE_EXPORT int bool2int(char *value);
[5100caa]139
[5ebff60]140G_MODULE_EXPORT struct ns_srv_reply **srv_lookup(char *service, char *protocol, char *domain);
141G_MODULE_EXPORT void srv_free(struct ns_srv_reply **srv);
[36e9f62]142
[5ebff60]143G_MODULE_EXPORT char *word_wrap(const char *msg, int line_len);
144G_MODULE_EXPORT gboolean ssl_sockerr_again(void *ssl);
145G_MODULE_EXPORT int md5_verify_password(char *password, char *hash);
146G_MODULE_EXPORT char **split_command_parts(char *command, int limit);
147G_MODULE_EXPORT char *get_rfc822_header(const char *text, const char *header, int len);
148G_MODULE_EXPORT int truncate_utf8(char *string, int maxlen);
[73f0a01]149G_MODULE_EXPORT gboolean parse_int64(char *string, int base, guint64 *number);
[47ab9a9]150G_MODULE_EXPORT char *str_reject_chars(char *string, const char *reject, char replacement);
[4466e3e]151G_MODULE_EXPORT char *str_pad_and_truncate(const char *string, long char_len, const char *ellipsis);
[24b8bbb]152
[9e83b15]153G_MODULE_EXPORT int b_istr_equal(gconstpointer v, gconstpointer v2);
154G_MODULE_EXPORT guint b_istr_hash(gconstpointer v);
155
[e27661d]156#endif
Note: See TracBrowser for help on using the repository browser.