commit fec1c25: [Minor] Optimise boundaries processing by avoiding small strings allocation

Vsevolod Stakhov vsevolod at highsecure.ru
Sun Mar 20 13:00:04 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-03-20 12:53:03 +0000
URL: https://github.com/rspamd/rspamd/commit/fec1c25ef6fba71bc260c367c0e478b15ffac302

[Minor] Optimise boundaries processing by avoiding small strings allocation

---
 src/libmime/mime_parser.c | 18 ++++++++++++++----
 1 file 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);
 		}
 	}


More information about the Commits mailing list