Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite: disable memstatus APIs at build time #56541

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Jan 10, 2025

This commit defines SQLITE_DEFAULT_MEMSTATUS=0 for the SQLite build. This setting disables several currently unused C APIs in SQLite, which can yield noticeable performance improvements. This setting is also used by better-sqlite, and is one of the recommended compile-time options in the SQLite docs.

The disabled APIs are used to report statistics about SQLite's memory usage. The drawback to this change is that those APIs could possibly be useful one day. I think the perf tradeoff and prior art make this change worth it.

Refs: https://sqlite.org/compile.html

I tested this code against main, this PR, and better-sqlite by changing the `require()` calls. On my machine, this PR was the fastest (6.4 seconds), followed by better-sqlite (6.7 seconds), followed by main (7.6 seconds).
'use strict';
const { strictEqual } = require('node:assert');
const { DatabaseSync: Database } = require('node:sqlite');
// const Database = require('better-sqlite3');
const N = 1_000_000;
const db = new Database(':memory:');

db.exec('PRAGMA journal_mode = WAL');
db.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, created_at TEXT DEFAULT CURRENT_TIMESTAMP) STRICT');

const start = performance.now();

for (let i = 0; i < N; ++i) {
  const insert = db.prepare('INSERT INTO data (key) values (?)');
  strictEqual(insert.run(i).lastInsertRowid, i);
  const query = db.prepare('SELECT created_at FROM data WHERE key = ?');
  strictEqual(typeof query.get(i).created_at, 'string');
}

const end = performance.now();
db.close();
console.log('elapsed:', end - start);

This commit defines SQLITE_DEFAULT_MEMSTATUS=0 for the SQLite
build. This setting disables several currently unused C APIs in
SQLite, which can yield noticeable performance improvements.
This setting is also used by better-sqlite, and is one of the
recommended compile-time options in the SQLite docs.

The disabled APIs are used to report statistics about SQLite's
memory usage. The drawback to this change is that those APIs
could possibly be useful one day.

Refs: https://sqlite.org/compile.html
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/gyp
  • @nodejs/security-wg

@nodejs-github-bot nodejs-github-bot added dependencies Pull requests that update a dependency file. needs-ci PRs that need a full CI run. sqlite Issues and PRs related to the SQLite subsystem. labels Jan 10, 2025
@daeyeon daeyeon added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 10, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 10, 2025
@nodejs-github-bot
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file. needs-ci PRs that need a full CI run. sqlite Issues and PRs related to the SQLite subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants