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