degesch: save some memory on channel users

`struct str` was mostly unnecessary, we can save 16+ bytes,
while performance and code readability is mostly unchanged.
This commit is contained in:
Přemysl Eric Janouch 2020-10-04 08:22:20 +02:00
parent dc8b580574
commit dd8e543a20
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 21 additions and 26 deletions

View File

@ -1350,14 +1350,15 @@ struct channel_user
LIST_HEADER (struct channel_user)
struct user *user; ///< Reference to user
struct str prefixes; ///< Ordered @+... characters
char *prefixes; ///< Ordered @+... characters
};
static struct channel_user *
channel_user_new (void)
channel_user_new (struct user *user, const char *prefixes)
{
struct channel_user *self = xcalloc (1, sizeof *self);
self->prefixes = str_make ();
self->user = user;
self->prefixes = xstrdup (prefixes);
return self;
}
@ -1365,7 +1366,7 @@ static void
channel_user_destroy (struct channel_user *self)
{
user_unref (self->user);
str_free (&self->prefixes);
free (self->prefixes);
free (self);
}
@ -4417,9 +4418,9 @@ irc_get_channel_user_prefix (struct server *s,
struct channel_user *channel_user, struct str *output)
{
if (s->ctx->show_all_prefixes)
str_append (output, channel_user->prefixes.str);
else if (channel_user->prefixes.len)
str_append_c (output, channel_user->prefixes.str[0]);
str_append (output, channel_user->prefixes);
else if (channel_user->prefixes[0])
str_append_c (output, channel_user->prefixes[0]);
}
static bool
@ -4438,9 +4439,7 @@ irc_channel_link_user (struct channel *channel, struct user *user,
user_channel->channel = channel;
LIST_PREPEND (user->channels, user_channel);
struct channel_user *channel_user = channel_user_new ();
channel_user->user = user;
str_append (&channel_user->prefixes, prefixes);
struct channel_user *channel_user = channel_user_new (user, prefixes);
LIST_PREPEND (channel->users, channel_user);
channel->users_len++;
}
@ -6172,23 +6171,22 @@ mode_processor_do_user (struct mode_processor *self)
hard_assert (mode && (size_t) (mode - all_modes) < strlen (all_prefixes));
char prefix = all_prefixes[mode - all_modes];
struct str *prefixes = &channel_user->prefixes;
const char *pos = strchr (prefixes->str, prefix);
char **prefixes = &channel_user->prefixes;
char *pos = strchr (*prefixes, prefix);
if (self->adding == !!pos)
return;
if (self->adding)
{
// Add the new mode prefix while retaining the right order
char *old_prefixes = str_steal (prefixes);
*prefixes = str_make ();
struct str buf = str_make ();
for (const char *p = all_prefixes; *p; p++)
if (*p == prefix || strchr (old_prefixes, *p))
str_append_c (prefixes, *p);
free (old_prefixes);
if (*p == prefix || strchr (*prefixes, *p))
str_append_c (&buf, *p);
cstr_set (prefixes, str_steal (&buf));
}
else
str_remove_slice (prefixes, pos - prefixes->str, 1);
memmove (pos, pos + 1, strlen (pos));
}
static void
@ -7248,9 +7246,9 @@ channel_user_sort_entry_cmp (const void *entry_a, const void *entry_b)
// First order by the most significant channel user prefix
const char *prio_a = strchr (s->irc_chanuser_prefixes,
*a->channel_user->prefixes.str);
a->channel_user->prefixes[0]);
const char *prio_b = strchr (s->irc_chanuser_prefixes,
*b->channel_user->prefixes.str);
b->channel_user->prefixes[0]);
// Put unrecognized prefixes at the end of the list
if (prio_a || prio_b)
@ -7314,11 +7312,8 @@ irc_sync_channel_user (struct server *s, struct channel *channel,
// If our idea of the user's modes disagrees with what the server's
// sent us (the most powerful modes differ), use the latter one
if (channel_user->prefixes.str[0] != prefixes[0])
{
str_reset (&channel_user->prefixes);
str_append (&channel_user->prefixes, prefixes);
}
if (channel_user->prefixes[0] != prefixes[0])
cstr_set (&channel_user->prefixes, xstrdup (prefixes));
}
static void
@ -8595,7 +8590,7 @@ lua_channel_get_users (lua_State *L)
lua_createtable (L, 0, 2);
lua_weak_push (L, wrapper->plugin, iter->user, &lua_user_info);
lua_setfield (L, -2, "user");
lua_plugin_kv (L, "prefixes", iter->prefixes.str);
lua_plugin_kv (L, "prefixes", iter->prefixes);
lua_rawseti (L, -2, i++);
}