[gtksourceview/wip/chergert/pcre2: 9/9] pcre2: use JIT for PCRE2 matching




commit f8f409f49fa157cf7bf331247581fae47f2c1757
Author: Christian Hergert <chergert redhat com>
Date:   Wed Sep 30 12:41:49 2020 -0700

    pcre2: use JIT for PCRE2 matching
    
    If the JIT is supported, lets use it. This has some rather good performance
    benefits with a bit of memory overhead for the JITd code pages. In
    particular, the cost of regex creation goes up by about 4x (using C as an
    example language syntax). The average runtime of those regex drops by about
    the same magnitude (4x). However, the worst cases can drop significantly
    with local tests showing an order of magnitude less (10x).
    
    All our regex are cached by the source language, so the amount we run
    the regex compared to creation of them is significant.
    
    The goal here is that we can save a lot of time during updates for other
    main loop work if we save how much we spend in Regex. It also means we can
    support larger files without timing out (currently 2msec timeout per-line
    before hitting protections).

 gtksourceview/implregex.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
---
diff --git a/gtksourceview/implregex.c b/gtksourceview/implregex.c
index fd84e2e2..a6788604 100644
--- a/gtksourceview/implregex.c
+++ b/gtksourceview/implregex.c
@@ -40,6 +40,7 @@ struct _ImplRegex
        PCRE2_SPTR             name_table;
        int                    name_count;
        int                    name_entry_size;
+       guint                  has_jit : 1;
 };
 
 struct _ImplMatchInfo
@@ -174,6 +175,9 @@ impl_regex_new (const char          *pattern,
                                    &regex->name_table);
        }
 
+       /* Now try to JIT the pattern for faster execution time */
+       regex->has_jit = pcre2_jit_compile (regex->code, PCRE2_JIT_COMPLETE) == 0;
+
 #ifdef GTK_SOURCE_PROFILER_ENABLED
        if (GTK_SOURCE_PROFILER_ACTIVE)
                message = g_strdup_printf ("compile=%lx match=%lx pattern=%s",
@@ -537,13 +541,22 @@ again:
        prev_begin = match_info->offsets[0];
        prev_end = match_info->offsets[1];
 
-       rc = pcre2_match (match_info->regex->code,
-                         (PCRE2_SPTR)match_info->string,
-                         match_info->string_len,
-                         match_info->start_pos,
-                         match_info->match_flags,
-                         match_info->match_data,
-                         NULL);
+       if (match_info->regex->has_jit)
+               rc = pcre2_jit_match (match_info->regex->code,
+                                     (PCRE2_SPTR)match_info->string,
+                                     match_info->string_len,
+                                     match_info->start_pos,
+                                     match_info->match_flags,
+                                     match_info->match_data,
+                                     NULL);
+       else
+               rc = pcre2_match (match_info->regex->code,
+                                 (PCRE2_SPTR)match_info->string,
+                                 match_info->string_len,
+                                 match_info->start_pos,
+                                 match_info->match_flags,
+                                 match_info->match_data,
+                                 NULL);
 
        if (set_regex_error (error, rc))
        {


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