Repository navigation
Open
Milestone
Description
An async batch of statements blocks the browser page until the batch is complete. This issue proposes an option that gives the event loop a turn between two statements.
Problem
alasql.promise runs the statements of a batch as a chain of promises. All statements run as microtasks. Input, rendering and timers cannot run until the last statement is complete.
setTimeout(() => console.log('timer'), 0);
await alasql.promise(['CREATE TABLE t (a INT)', 'INSERT INTO t VALUES (1)', 'SELECT * FROM t']);
await alasql.promise('CREATE TABLE u (a INT); INSERT INTO u VALUES (1); SELECT * FROM u');
// AlaSQL writes 'timer' here, after the two batchesProposal
Add two options. The default is off.
alasql.options.yieldIntervalMs = 16; // default false
alasql.options.yieldFn = null; // default nullyieldIntervalMs
- A number turns the yield on.
- After each statement except the last, AlaSQL yields if this number of milliseconds has gone by since the start of the run or the last yield.
0yields after each statement except the last.- A batch of many small statements yields only some times. This keeps the cost of the yield low.
- AlaSQL uses
scheduler.yield()if the browser has it. If not, AlaSQL uses aMessageChannelin the browser,setImmediatein Node, andsetTimeout(0)as the last choice. Browsers make nested timers wait 4 ms, sosetTimeoutis last. - Each run keeps its start time and the time of its last yield. AlaSQL keeps no global yield state.
yieldFn
yieldFnis a function that the user sets. AlaSQL calls it in place of the built-in yield, and waits for the promise it returns.yieldIntervalMsstill decides when AlaSQL calls it.yieldFndoes not get progress data.alasql.options.progressgives the progress (The progress option gets different arguments from the array and string forms of alasql.promise #2978).
Example 1. Wait for the next frame, so that the browser shows a progress bar between two statements.
alasql.options.yieldIntervalMs = 16;
alasql.options.yieldFn = () => new Promise(resolve => requestAnimationFrame(() => resolve()));Example 2. Count the yields in a test.
let yields = 0;
alasql.options.yieldIntervalMs = 0;
alasql.options.yieldFn = async () => {
yields++;
};
await alasql.promise(['SELECT 1', 'SELECT 2', 'SELECT 3']);
// yields is 2Scope
- The options apply to each async run of more than one statement. This includes
alasql.promise([...]),alasql.promise('A; B; C')andalasql(sql, params, cb). - A sync call does not yield.
- With the options off, the behaviour does not change. This is not a breaking change.
Limits
- The yield does not split one statement. A SELECT that runs for 2 s blocks for 2 s.
- A yield inside a scan needs an async copy of the compiled query code and the join and group loops. This makes the hot loops slower and gives two code paths to keep. A sort cannot stop in the middle. Thus this issue does not include a yield inside a statement.
- For a long single query, use the worker build (
dist/alasql-worker.js), or report the query as a performance issue. Comma join is slow when a table is filtered by IN or OR #2916 went from more than 60 s to 14 ms.
Risks
- Other code can run during a yield. It can be an event handler or a different AlaSQL call that changes a table. This is already possible between two
await alasql.promise(...)calls. adruncompiles each statement with the globalalasql.useid. AUSEfrom other code during a yield moves the remaining statements to a different database. The run must keep its database id and set it again after each yield.- Two runs of the same cached SQL share per-run state (Cached statements hold per-run state, so two runs of the same SQL at once overwrite each other #2596). A yield makes it more likely that two runs overlap.
- AlaSQL calls
alasql.options.progressat the same point. Do the yield after the progress call, so that the page can show the new progress. This issue does not changeprogress(see The progress option gets different arguments from the array and string forms of alasql.promise #2978).
Where
src/engine/promise.js, for the array form.adruninsrc/engine/core.js, for more than one statement in one string and for the callback form.
Tests
- Options off. A timer set before a batch runs after the batch.
yieldIntervalMs = 0. A timer set before a batch runs between its statements.yieldIntervalMs = 0withyieldFn. AlaSQL callsyieldFnone time between each two statements. AlaSQL does not call it for one statement.- A
USEfrom other code during a yield does not change the database of the remaining statements. - The results and errors are the same with the options on and off.
Documentation
- Add a wiki page for the two options, with the two examples, the limits above and a link to the worker build.
- Add a short mention to the README, near
alasql.promise.
Possible next step, a YIELD statement
A YIELD statement marks a point where AlaSQL can yield. Do this in a different issue after the options.
BEGIN
INSERT INTO t SELECT * FROM big1;
YIELD;
INSERT INTO t SELECT * FROM big2;
ENDGrammar
- Add
YIELDto the non-reserved words, asCONTINUEis. A column with the nameyieldthen continues to work.continueworks as a column name today (checked). - Thus the statement is not a breaking change.
Behaviour
YIELDfollows the options. WithyieldIntervalMsoff,YIELDdoes nothing.- With
yieldIntervalMsset,YIELDyields if that number of milliseconds has gone by since the start of the run or the last yield. It uses the same run times as the yield between statements. - In a sync call,
YIELDdoes nothing. YIELDgives1in the results array, asDECLAREandSETdo.
Where it is useful
- At the top level of a batch, the options already yield between statements. There,
YIELDadds nothing. - Inside
BEGIN ENDandIF, there is no yield between statements. There,YIELDadds a yield point. - Inside
WHILE, the loop yields by itself after An async WHILE loop waits for a timer before each iteration #2979.YIELDis not necessary there.
Code
- The async code for the procedural statements already exists.
BeginEndruns its statements one by one with a callback,Ifsends the callback on, andWhilecalls the callback after each iteration. - Thus
yy.Yield.prototype.execute(databaseid, params, cb)can do the yield and then callcb. The procedural statements need no change. executemust get the run times. Today it gets onlydatabaseid,paramsandcb, so the run context must go throughBeginEnd,IfandWhile.
Metadata
Metadata
Assignees
Labels
No labels
Activity