Skip to content

vala, codegen: Add `o` modifier to regex literal to be able to make use of JIT optimization

Zhou Qiankang requested to merge wszqkzqk/vala:regex-o into main

Add an additional modifier of regex literal, o, to use the compile flag G_REGEX_OPTIMIZE to make use of JIT optimization.

Example:

var re = /EXPRESSION/o;

Why need this

GLib uses the pcre2/pcre library for regex and supports optimization for regex by passing G_REGEX_OPTIMIZE (RegexCompileFlags.OPTIMIZE in vala).

However, vala's regex literal doesn't support to create optimized regex instances. If I need to create an optimized regex instance, I must use new Regex, which does not support compile-time checking.

Moreover, if I want to create a regex instance in a static field of a class, I can simply write it like this:

public class Foo {
    static var bar = /EXPRESSION/;
    .......
}

But if I need to pass RegexCompileFlags.OPTIMIZE flag, the regex can only be initialized during the instantiation phase of the class, which is much more lengthy, like this:

public class Foo {
    static Regex? bar = null
    ......
    public Foo () {
        if (bar == null) {
            try {
                bar = new Regex ("""EXPRESSION""", RegexCompileFlags.OPTIMIZE);
            } catch {
                assert_not_reached ();
            }
        }
        ......
    }
}

o does not conflict with other modifiers (also not used in other languages so that it will not break the habit of programmers), and supporting this option simplifies the code for this case.

About optimization

According to GLib's doc of RegexCompileFlags and introduction of JIT compiler of PCRE2, if a regex instance is used for matching many times, optimized one will have significantly better performance.

Edited by Zhou Qiankang

Merge request reports