sub: jsre filter: abort init early on empty filter list

TL;DR: previously a JavaScript VM was created + destroyed whenever
       a sub track was initialized, even if no jsre filter was set.
       Now a JS VM is created only if jsre filters were set.

Sub filters are initialized once when a subtitle track is chosen, and
then whenever the sub track changes or when some sub options change.

Sub filters init is synchronous - playback is suspended till it ends.

A filter can abort init early (get disabled) depending on conditions
specific to each filter. The regex and jsre filters aborted early
if the filter is disabled (default is enabled) or if the track is not
ass (relativey rare, e.g. bitmap subs).

The init then iterates over the filter strings, and if the result is
empty (common - no filter was added, but also if all strings failed
regex init) then it's also aborted during init.

While this iteration step is cheap with filter regex, with jsre it
requires instanciating the JS VM (mujs) in advance in order to parse
the filter strings at the list, and the VM is then destroyed if the
list ends up empty.

This VM create+destroy is fast but measurable (0.2 - 0.7 ms, slowest
measured on 2010 MacBook Air), but can be avoided altogether if we
check that the filter list is not empty before we create the VM.

So now we do just that.
This commit is contained in:
Avi Halachmi (:avih) 2022-06-09 13:46:42 +03:00
parent 3a52159286
commit 7ff4a27eb6
1 changed files with 4 additions and 1 deletions

View File

@ -74,6 +74,9 @@ static bool jsre_init(struct sd_filter *ft)
if (!ft->opts->rf_enable)
return false;
if (!(ft->opts->jsre_items && ft->opts->jsre_items[0]))
return false;
struct priv *p = talloc_zero(ft, struct priv);
ft->priv = p;
@ -84,7 +87,7 @@ static bool jsre_init(struct sd_filter *ft)
}
talloc_set_destructor(p, destruct_priv);
for (int n = 0; ft->opts->jsre_items && ft->opts->jsre_items[n]; n++) {
for (int n = 0; ft->opts->jsre_items[n]; n++) {
char *item = ft->opts->jsre_items[n];
int err = p_regcomp(p->J, p->num_regexes, item, JS_REGEXP_I | JS_REGEXP_M);