diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2022-03-20 12:53:03 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2022-03-20 12:53:03 +0000 |
commit | fec1c25ef6fba71bc260c367c0e478b15ffac302 (patch) | |
tree | bde70db89a22e1b951528e0f1219ae1de3d223de /src/libmime | |
parent | 5a7a082211a952f8ce170d177db848881d994d99 (diff) | |
download | rspamd-fec1c25ef6fba71bc260c367c0e478b15ffac302.tar.gz rspamd-fec1c25ef6fba71bc260c367c0e478b15ffac302.zip |
[Minor] Optimise boundaries processing by avoiding small strings allocation
Diffstat (limited to 'src/libmime')
-rw-r--r-- | src/libmime/mime_parser.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/libmime/mime_parser.c b/src/libmime/mime_parser.c index b2f396139..964b5b30e 100644 --- a/src/libmime/mime_parser.c +++ b/src/libmime/mime_parser.c @@ -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; @@ -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); } @@ -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); } } |