Open andrjohns opened 3 months ago
@andrjohns: Interesting use case. Do you have 15951 local variables in scope? How many nested scopes? Any closures involved too? Are these var
or let
/ const
definitions?
We never envisioned this simple loop to be an issue, but indeed some sort of hashing would help... The javascript scoping rules make this cumbersome, but I can try and investigate some ideas.
Could you possibly share this file for me to benchmark lookup alternatives?
Thanks! It's a bit of a behemoth file, but you can get it from here: https://github.com/stan-dev/stanc3/releases/download/nightly/stanc.js
As context, it's a transpiler for the Stan Language, converting the Stan language to C++. The transpiler itself is written in OCaml, but R doesn't support compiling OCaml in packages, so we instead have to compile the OCaml to JavaScript (using js_of_ocaml) for use in R
That's the minified file, let me know if the original/verbose would be more useful instead!
That's the minified file, let me know if the original/verbose would be more useful instead!
@andrjohns Indeed a more verbose version would be useful :)
That's the minified file, let me know if the original/verbose would be more useful instead!
@andrjohns Indeed a more verbose version would be useful :)
Of course, here you go: stanc-debug.js.txt
(Using .txt
for github upload restrictions)
This is really just me thinking out aloud but...
We never envisioned this simple loop to be an issue, but indeed some sort of hashing would help
...what could maybe also work is a small 4-8 entries LRU1 cache that's searched first. find_var does a lot of repeated work for the source file @andrjohns provided.
1 Or a LFSR that randomly discards entries. Probably similar asymptotic performance, possibly better mechanical performance.
I've got a large JS program which
qjs
takes ~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_var
alone:Given that every time
find_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 whenfd->var_count
is large (15951 in my case):Could this be replaced with a lookup/hashtable or similar in the
JSFunctionDef
object? Or is there a simpler alternative?