[evolution-data-server] CamelMimeFilterToHTML: Speed up read of long lines



commit b76b47e4f9e6aef18ad05bc8b7e23cb905ccbb26
Author: Milan Crha <mcrha redhat com>
Date:   Fri Jul 16 11:28:00 2021 +0200

    CamelMimeFilterToHTML: Speed up read of long lines
    
    When using camel_mime_filter_backup(), the backed up text is re-read
    the next time, which is not necessary when searching for the end of
    the line, thus rather use own backing buffer.
    
    Related to https://gitlab.gnome.org/GNOME/evolution/-/issues/329

 src/camel/camel-mime-filter-tohtml.c | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)
---
diff --git a/src/camel/camel-mime-filter-tohtml.c b/src/camel/camel-mime-filter-tohtml.c
index fff6294b3..da39ed5dc 100644
--- a/src/camel/camel-mime-filter-tohtml.c
+++ b/src/camel/camel-mime-filter-tohtml.c
@@ -29,6 +29,7 @@
 struct _CamelMimeFilterToHTMLPrivate {
 
        CamelUrlScanner *scanner;
+       GString *backup;
 
        CamelMimeFilterToHTMLFlags flags;
        guint32 color;
@@ -284,7 +285,7 @@ html_convert (CamelMimeFilter *mime_filter,
 {
        CamelMimeFilterToHTMLPrivate *priv;
        const gchar *inptr;
-       gchar *outptr, *outend;
+       gchar *outptr, *outend, *backup_str = NULL;
        const gchar *start;
        const gchar *inend;
        gint depth;
@@ -350,6 +351,20 @@ html_convert (CamelMimeFilter *mime_filter,
                if (inptr >= inend && !flush)
                        break;
 
+               if (priv->backup) {
+                       gsize backup_len, backup_len_old;
+
+                       backup_len_old = priv->backup->len;
+                       g_string_append_len (priv->backup, start, (gsize) (inend - start));
+                       backup_len = priv->backup->len;
+                       backup_str = g_string_free (priv->backup, FALSE);
+                       priv->backup = NULL;
+
+                       inptr = backup_str + backup_len_old + (inptr - start);
+                       start = backup_str;
+                       inend = start + backup_len;
+               }
+
                priv->column = 0;
                depth = 0;
 
@@ -548,11 +563,15 @@ html_convert (CamelMimeFilter *mime_filter,
                        priv->pre_open = FALSE;
                }
        } else if (start < inend) {
-               /* backup */
-               camel_mime_filter_backup (
-                       mime_filter, start, (gsize) (inend - start));
+               /* backup, but do not use camel_mime_filter_backup() to avoid round trip for long lines */
+               if (priv->backup)
+                       g_string_append_len (priv->backup, start, (gsize) (inend - start));
+               else
+                       priv->backup = g_string_new_len (start, (gsize) (inend - start));
        }
 
+       g_free (backup_str);
+
        *out = mime_filter->outbuf;
        *outlen = outptr - mime_filter->outbuf;
        *outprespace = mime_filter->outpre;
@@ -567,6 +586,9 @@ mime_filter_tohtml_finalize (GObject *object)
 
        camel_url_scanner_free (priv->scanner);
 
+       if (priv->backup)
+               g_string_free (priv->backup, TRUE);
+
        /* Chain up to parent's finalize() method. */
        G_OBJECT_CLASS (camel_mime_filter_tohtml_parent_class)->finalize (object);
 }
@@ -608,6 +630,11 @@ mime_filter_tohtml_reset (CamelMimeFilter *mime_filter)
 
        priv->column = 0;
        priv->pre_open = FALSE;
+
+       if (priv->backup) {
+               g_string_free (priv->backup, TRUE);
+               priv->backup = NULL;
+       }
 }
 
 static void
@@ -630,6 +657,7 @@ camel_mime_filter_tohtml_init (CamelMimeFilterToHTML *filter)
 {
        filter->priv = camel_mime_filter_tohtml_get_instance_private (filter);
        filter->priv->scanner = camel_url_scanner_new ();
+       filter->priv->backup = NULL;
 }
 
 /**


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]