google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.26k stars 204 forks source link

Caching Syntax Parsing Results for Scripts Run Repeatedly #501

Closed housail closed 10 months ago

housail commented 10 months ago

Is your feature request related to a problem? Please describe. I've been using Google's Go Starlark extensively for running a few of my scripts. Our team runs a set of Starlark scripts quite frequently, several times an hour. We've noticed significant computational overhead related to the repeated parsing of scripts.

Describe the solution you'd like We're looking into optimizations and one glaring point is the constant parsing of unchanging scripts. To address this, I was hoping the Starlark interpreter might support some kind of syntax parsing caching system.

Ideally, I'd want the interpreter to parse the script once and cache the parsing result. Every time we run the same script afterward, the system should bypass the parsing step and directly use the cached parsed result unless the script changes.

Describe alternatives you've considered An alternative would be to create a separate caching mechanism in our codebase but I believe this could be a common use case that many enterprises or heavy users of Starlark might come across.

Additional context The optimisation of caching parsed scripts could significantly reduce the total execution time and computational overhead in scenarios where scripts are run repeatedly.

Looking forward to discussing this further, any thoughts or suggestions on implementation are welcome.

Thanks!

adonovan commented 10 months ago

This is already supported: use SourceProgram or FIleProgram to create a Program, then Write to save it in compiled form, then CompiledProgram to reload it. Compilation is a per-file operation. Make sure the same exact version of Starlark is used to read and write the compiled form; you can use the CompilerVersion constant if you need to form a sound cache key.