Let me start by saying that there are definitely parts that can be optimized. However, so far it seems it works for most (all?) vaults without further optimization.
Also: all the event handling and dynamic updates of the rendered markdown preview are only possible due to Obsidianās amazing plug-in API/infrastructure.
Obsidian has s.th. called a metadata cache. When you start obsidian, it scans all files and stores some metadata for each file. For example all tags or checklist items. The Tasks plugin waits for the metadata cache to finish. Then, or every file in the vault, Tasks asks the metadata cache for checklist items. If a file has checklist items, Tasks goes through all of them and parses them. So simplified: when you start Obsidian, Obsidian scans your whole vault for checklist items and then Tasks scans all checklist items (from the files in the vault).
During the inital scan, the Tasks plugin builds an in-memory cache of all tasks (with the additional data like due date, done date, and so on). After the initial scan, Obsidian will tell the Tasks plugin whenever a file has changed. Every time that happens, Tasks re-scans the checklist items of that file for tasks. In its in-memory cache, tasks replaces the old tasks from that file with the new tasks.
When you have a query block, it takes all tasks from the Tasks plugins in-memory cache and searches for all tasks that match the query. All matching tasks are sorted and then added to the markdown preview.
When a file changes and therefore the in-memory task cache changes, Tasks tells all rendered query results that there has been an update and each rendered query updates itself based on the existing query and the updated tasks. When a rendered query is removed, e.g. due to a closed file/panel, it wonāt update anymore.
Hope this helps! Feel free to ask for more details! (the source is open too, of course
)