source-academy / js-slang

Implementations of sublanguages of JavaScript, TypeScript, Scheme and Python
https://source-academy.github.io/source/
Apache License 2.0
66 stars 104 forks source link

CSE Machine: `canAvoidInstr` is too slow #1698

Open martin-henz opened 5 months ago

martin-henz commented 5 months ago

The canAvoidInstr function is extremely inefficient and slows down the deployment each time by almost one hour!

@RichDom2185 and @sayomaki commented:

the code is not very efficient at all, it seems; we can just early return instead of iterating through the entire control every single step of the cse machine

I'm pretty sure this alone accounts for most of the 40minutes

same thing for the switch case logic, lots of unnecessary computation

@sayomaki : consider using javascript's Array.some() or Array.every() instead

@RichDom2185 : yes that also works, no need for a for loop, though I think the logic can be improved further

because the same computation is being repeated at every step

it should have been memoised

just create a new field in the control item and calculate it once only instead of calculating n times for each item where n is the number of control items