Skip to content

Optimising find_var / find_arg performance #456

@andrjohns

Description

@andrjohns

I've got a large JS program which qjs takes ~1.5 seconds just to parse/load. If I profile the evaluation using gprof, then it shows that over a third of the time is spent in find_var alone:

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
 36.65      1.84     1.84   275818     0.00     0.00  find_var
 12.75      2.48     0.64   212052     0.00     0.00  JS_CallInternal
 11.55      3.06     0.58   100362     0.00     0.00  get_var_ref
...

Given that every timefind_var is called it iterates through all variables in the scope until finding the target variable, it looks like this is causing a lot of repeated iteration over the same variables when fd->var_count is large (15951 in my case):

static int find_var(JSContext *ctx, JSFunctionDef *fd, JSAtom name)
{
    int i;
    for(i = fd->var_count; i-- > 0;) {
        if (fd->vars[i].var_name == name && fd->vars[i].scope_level == 0)
            return i;
    }
    return find_arg(ctx, fd, name);
}

Could this be replaced with a lookup/hashtable or similar in the JSFunctionDef object? Or is there a simpler alternative?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions