node:sqlite / bun:sqlite: built without SQLITE_DEFAULT_MEMSTATUS=0, so reads from several workers contend on a global mutex
What version of Bun is running?
1.4.2, and 1.4.3-canary (the #44103 build, 47eeafc2c)
What platform is your computer?
Linux 7.2 x86_64 (WSL2), 16 vCPU
What steps can reproduce the bug?
PRAGMA compile_options shows the only threading-relevant difference between the SQLite in Node and the one in Bun (both THREADSAFE=1, MUTEX_PTHREADS):
node 24.21.0: DEFAULT_MEMSTATUS=0
bun 1.4.2 / 1.4.3-canary: (not set, so memory statistics are on)With memory statistics on, every SQLite malloc and free takes one process-wide mutex and updates shared counters. Connections are private to their threads, yet they all contend on that one lock. The SQLite docs recommend SQLITE_DEFAULT_MEMSTATUS=0 for this reason.
This C program shows the cost on its own, with no Bun involved. 8 threads each open their own read-only connection and do point lookups that read all 21 columns, calling column_type/column_text/column_bytes/column_int64 the way a JS binding does. The only change between runs is sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0|1):
// N threads, each with its own read-only connection, doing point lookups that
// read all 21 columns. argv: db nthreads memstatus(0|1)
// Build: cc -O2 -o memstat memstat.c -lsqlite3 -lpthread
#include <sqlite3.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static const char *path;
static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
static void *work(void *arg) {
long seed = (long)arg;
sqlite3 *db; sqlite3_stmt *st;
sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL);
sqlite3_prepare_v2(db, "SELECT * FROM nodes WHERE id = ?", -1, &st, NULL);
char key[32]; long acc = 0;
double t = now();
for (long i = 0; i < 120000; i++) {
snprintf(key, sizeof key, "n%lu", ((unsigned long)(i * 2654435761UL + seed * 7919) % 20000) % 20000);
sqlite3_bind_text(st, 1, key, -1, SQLITE_TRANSIENT);
if (sqlite3_step(st) == SQLITE_ROW) {
int n = sqlite3_column_count(st);
for (int c = 0; c < n; c++) {
// What a JS binding does per column: type, then the value and its length.
if (sqlite3_column_type(st, c) == SQLITE_INTEGER) acc += sqlite3_column_int64(st, c);
else { const unsigned char *s = sqlite3_column_text(st, c); acc += sqlite3_column_bytes(st, c) + (s ? s[0] : 0); }
}
}
sqlite3_reset(st);
}
double ms = now() - t;
sqlite3_finalize(st); sqlite3_close(db);
double *out = malloc(sizeof *out); *out = ms + (acc == 42 ? 1 : 0);
return out;
}
static void create_db(void) {
sqlite3 *db; sqlite3_stmt *st; char a[32], b[32], c[32], v[8], doc[256];
if (sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK) { sqlite3_close(db); return; }
sqlite3_close(db);
sqlite3_open(path, &db);
sqlite3_exec(db, "CREATE TABLE nodes (id TEXT PRIMARY KEY, name TEXT, kind TEXT, file TEXT, line INTEGER, sig TEXT, doc TEXT,"
" c0 TEXT, c1 INTEGER, c2 TEXT, c3 INTEGER, c4 TEXT, c5 INTEGER, c6 TEXT, c7 INTEGER, c8 TEXT, c9 INTEGER, c10 TEXT, c11 INTEGER,"
" c12 TEXT, c13 INTEGER); BEGIN", NULL, NULL, NULL);
sqlite3_prepare_v2(db, "INSERT INTO nodes VALUES (?1, ?2, 'function', ?3, ?4, '(a: string, b: number): void', ?5,"
" ?6, ?7, ?6, ?7, ?6, ?7, ?6, ?7, ?6, ?7, ?6, ?7, ?6, ?7)", -1, &st, NULL);
for (int i = 0; i < 20000; i++) {
snprintf(a, sizeof a, "n%d", i); snprintf(b, sizeof b, "symbol%d", i); snprintf(c, sizeof c, "src/file%d.ts", i % 500);
snprintf(v, sizeof v, "v%d", i % 97); memset(doc, 'x', i % 200); doc[i % 200] = 0;
sqlite3_bind_text(st, 1, a, -1, SQLITE_TRANSIENT); sqlite3_bind_text(st, 2, b, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(st, 3, c, -1, SQLITE_TRANSIENT); sqlite3_bind_int(st, 4, i % 900);
sqlite3_bind_text(st, 5, doc, -1, SQLITE_TRANSIENT); sqlite3_bind_text(st, 6, v, -1, SQLITE_TRANSIENT);
sqlite3_bind_int(st, 7, i); sqlite3_step(st); sqlite3_reset(st);
}
sqlite3_finalize(st); sqlite3_exec(db, "COMMIT", NULL, NULL, NULL); sqlite3_close(db);
}
int main(int argc, char **argv) {
path = argv[1]; int n = atoi(argv[2]);
sqlite3_config(SQLITE_CONFIG_MEMSTATUS, atoi(argv[3]));
sqlite3_initialize();
create_db();
pthread_t th[64]; double sum = 0;
for (long i = 0; i < n; i++) pthread_create(&th[i], NULL, work, (void *)i);
for (int i = 0; i < n; i++) { double *ms; pthread_join(th[i], (void **)&ms); sum += *ms; free(ms); }
printf("sqlite %s memstatus=%s threads=%d avg per thread %.0f ms\n", sqlite3_libversion(), argv[3], n, sum / n);
return 0;
}cc -O2 -o memstat memstat.c -lsqlite3 -lpthread
./memstat t.db 1 1; ./memstat t.db 8 1; ./memstat t.db 1 0; ./memstat t.db 8 0Average per-thread time, system SQLite 3.46.1, 3 alternating rounds:
| memstatus | 1 thread | 8 threads | 8/1 |
|---|---|---|---|
| 1 (Bun's build) | 908–1,015 ms | 817–1,044 ms | 0.89–1.03 |
| 0 (Node's build) | 924–978 ms | 660–759 ms | 0.71–0.82 |
With one thread the setting makes no difference. With 8 threads it costs each thread about 25%.
What is the expected behavior?
node:sqlite and bun:sqlite reads from several worker threads, each with its own connection, should scale the way they do on Node.
What do you see instead?
Read all 21 columns with SELECT * ... WHERE id = ? (120k get() calls per worker, each worker with its own connection to one WAL database). The per-worker time ratio of 8 workers to 1 worker is:
| runtime | node:sqlite |
bun:sqlite |
|---|---|---|
| node 24.21.0 | 0.81–0.90 | — |
| bun 1.4.2 | 1.24–1.33 | 1.75–1.88 |
| bun 1.4.3-canary | 1.67–2.08 | 1.76–2.17 |
Selecting only the 8 integer columns, or only the 13 text columns, mostly scales like Node on Bun (0.84–1.13, with one 2.63 outlier during a load spike), so the effect grows with the number of values read per row.
In a perf profile of the canary bun-profile build, the mutex samples inside get() grow per worker from 1 to 8 workers. Under sqlite3Malloc they go from 42 to 95, under sqlite3_free from 21 to 59, and under releaseMemArray from 10 to 41. Most of the rest is the per-connection db->mutex taken by every sqlite3_column_* call.
This is separate from the worker GC wait I filed as #44156: BUN_JSC_numberOfGCMarkers=1 doesn't change these numbers (1.76–2.13 with it, 1.72–2.08 without), and the extra time is on-CPU. It's also separate from #44084, where #44103 fixes the single-thread cost.
Additional information
The likely fix is adding SQLITE_DEFAULT_MEMSTATUS=0 to the SQLite build flags, matching Node (or calling sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0) before SQLite initializes). I can't rebuild Bun to confirm the full effect, so treat the 25% as the C program's number, not Bun's.
Source: oven-sh/bun