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?
I've got a large JS program which
qjstakes ~1.5 seconds just to parse/load. If I profile the evaluation usinggprof, then it shows that over a third of the time is spent infind_varalone:Given that every time
find_varis 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 whenfd->var_countis large (15951 in my case):Could this be replaced with a lookup/hashtable or similar in the
JSFunctionDefobject? Or is there a simpler alternative?