bindings/javascript: Reduce VM/native crossing overhead

Before:

```
penberg@vonneumann perf % node perf-turso.js
cpu: Apple M1
runtime: node v22.16.0 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'525 ns/iter   (1'482 ns … 1'720 ns)  1'534 ns  1'662 ns  1'720 ns

summary for Statement
  Statement.get() bind parameters
penberg@vonneumann perf % bun perf-turso.js
cpu: Apple M1
runtime: bun 1.2.15 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'198 ns/iter   (1'157 ns … 1'495 ns)  1'189 ns  1'456 ns  1'495 ns

summary for Statement
  Statement.get() bind parameters
```

After:

```

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'206 ns/iter   (1'180 ns … 1'402 ns)  1'208 ns  1'365 ns  1'402 ns

summary for Statement
  Statement.get() bind parameters
penberg@vonneumann perf % bun perf-turso.js
cpu: Apple M1
runtime: bun 1.2.15 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p999
----------------------------------------------------------------------- -----------------------------
• Statement
----------------------------------------------------------------------- -----------------------------
Statement.get() bind parameters   1'019 ns/iter     (980 ns … 1'360 ns)  1'005 ns  1'270 ns  1'360 ns

summary for Statement
  Statement.get() bind parameters
```
This commit is contained in:
Pekka Enberg
2025-08-01 16:34:53 +03:00
parent a51c35c979
commit 94efe9dd46
4 changed files with 127 additions and 89 deletions

View File

@@ -5,6 +5,11 @@ const { bindParams } = require("./bind.js");
const SqliteError = require("./sqlite-error.js");
// Step result constants
const STEP_ROW = 1;
const STEP_DONE = 2;
const STEP_IO = 3;
const convertibleErrorTypes = { TypeError };
const CONVERTIBLE_ERROR_PREFIX = "[TURSO_CONVERT_TYPE]";
@@ -257,14 +262,18 @@ class Statement {
this.stmt.reset();
bindParams(this.stmt, bindParameters);
for (;;) {
const result = this.stmt.step();
if (result.io) {
const stepResult = this.stmt.step();
if (stepResult === STEP_IO) {
this.db.db.ioLoopSync();
continue;
}
if (result.done) {
if (stepResult === STEP_DONE) {
break;
}
if (stepResult === STEP_ROW) {
// For run(), we don't need the row data, just continue
continue;
}
}
const lastInsertRowid = this.db.db.lastInsertRowid();
@@ -282,15 +291,17 @@ class Statement {
this.stmt.reset();
bindParams(this.stmt, bindParameters);
for (;;) {
const result = this.stmt.step();
if (result.io) {
const stepResult = this.stmt.step();
if (stepResult === STEP_IO) {
this.db.db.ioLoopSync();
continue;
}
if (result.done) {
if (stepResult === STEP_DONE) {
return undefined;
}
return result.value;
if (stepResult === STEP_ROW) {
return this.stmt.row();
}
}
}
@@ -313,15 +324,17 @@ class Statement {
bindParams(this.stmt, bindParameters);
const rows = [];
for (;;) {
const result = this.stmt.step();
if (result.io) {
const stepResult = this.stmt.step();
if (stepResult === STEP_IO) {
this.db.db.ioLoopSync();
continue;
}
if (result.done) {
if (stepResult === STEP_DONE) {
break;
}
rows.push(result.value);
if (stepResult === STEP_ROW) {
rows.push(this.stmt.row());
}
}
return rows;
}