observablehq / framework

A static site generator for data apps, dashboards, reports, and more. Observable Framework combines JavaScript on the front-end for interactive graphics with any language on the back-end for data analysis.
https://observablehq.com/framework/
ISC License
2.13k stars 85 forks source link

Serialize asynchronous evaluation to improve performance #1469

Open Fil opened 1 week ago

Fil commented 1 week ago

sql queries —using the function in js code blocks, or within sql code blocks— can depend on variables that change often, such as the value of an interactive range slider.

The performance of these queries can suffer from the sheer quantity of queries sent during interaction, as they need to be executed in turn. Furthermore, it is a huge waste of compute, since all the queries but the last correspond to an invalidated state of the cell, and are discarded after execution.

This problem generalizes to all asynchronous functions.

Registering asynchronous functions per cell—so that each cell only runs one query at a time, and skips the queries launched by cells that have been invalidated—could lead to a dramatic performance boost.

mbostock commented 1 week ago

This is mostly a problem with DuckDB-Wasm or anything that’s using web workers to compute asynchronously; if the computation is running in the main thread, it tends to be serialized anyway because JavaScript is single-threaded; though there are equivalent problems if a cell is e.g. calling fetch: you’ll make many fetches that are discarded only to consider the response of the last one.

What we want here is an opt-in or opt-out mechanism that makes it easy to serialize computation, so that we don’t compute a variable too many times in parallel and waste resources.