Skip to content

Commit

Permalink
[Minor] Optimise boundaries processing by avoiding small strings allo…
Browse files Browse the repository at this point in the history
…cation
  • Loading branch information
vstakhov committed Mar 20, 2022
1 parent 5a7a082 commit fec1c25
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/libmime/mime_parser.c
Expand Up @@ -1153,7 +1153,6 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp,
void *context)
{
const gchar *end = text + len, *p = text + match_pos, *bend;
gchar *lc_copy;
gsize blen;
gboolean closing = FALSE;
struct rspamd_mime_boundary b;
Expand Down Expand Up @@ -1225,13 +1224,21 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp,
b.boundary = p - st->start - 2;
b.start = bend - st->start;

if (closing) {
/* Small optimisation as boundaries are usually short strings */
gchar *lc_copy, lc_copy_buf[128];

if (blen + 2 < sizeof(lc_copy_buf)) {
lc_copy = lc_copy_buf;
}
else {
lc_copy = g_malloc (blen + 2);
}

if (closing) {
memcpy (lc_copy, p, blen + 2);
rspamd_str_lc (lc_copy, blen + 2);
}
else {
lc_copy = g_malloc (blen);
memcpy (lc_copy, p, blen);
rspamd_str_lc (lc_copy, blen);
}
Expand All @@ -1256,7 +1263,10 @@ rspamd_mime_preprocess_cb (struct rspamd_multipattern *mp,
b.closed_hash = 0;
}

g_free (lc_copy);
/* Check if a string has been allocated on the heap */
if (blen + 2 >= sizeof(lc_copy_buf)) {
g_free(lc_copy);
}
g_array_append_val (st->boundaries, b);
}
}
Expand Down

0 comments on commit fec1c25

Please sign in to comment.