Skip to content

Instantly share code, notes, and snippets.

@billywhizz
Created February 8, 2026 01:18
Show Gist options
  • Select an option

  • Save billywhizz/faff376c1f4a0d49075b0fc746b04ec1 to your computer and use it in GitHub Desktop.

Select an option

Save billywhizz/faff376c1f4a0d49075b0fc746b04ec1 to your computer and use it in GitHub Desktop.
import { api } from 'lib/sqlite/api.js'
import { bindall, bind_custom } from 'lib/ffi.js'
import { Bench } from 'lib/bench.js'
const { assert, core, ptr, utf8_length } = lo
const { dlopen, RTLD_NOW, RTLD_LOCAL } = core
const sqlite_handle = assert(dlopen(lo.getenv('SQLITE_SO') || 'libsqlite3.so', RTLD_NOW | RTLD_LOCAL))
const sqlite = bindall(api, sqlite_handle)
const u32 = ptr(new Uint32Array(2))
const {
step, column_int, column_double, reset, finalize,
open2, exec2, exec, close2, prepare2, column_count,
column_type, column_bytes, bind_int, bind_int64,
bind_double, bind_text, bind_blob,
errmsg, column_name, column_text, column_blob
} = sqlite
const BLOB_WRITABLE = 1
const BLOB_READONLY = 0
const OK = 0
const ROW = 100
const DONE = 101
const OPEN_CREATE = 0x00000004
const OPEN_READWRITE = 0x00000002
const OPEN_NOMUTEX = 0x00008000
const flags = OPEN_CREATE | OPEN_READWRITE | OPEN_NOMUTEX
assert(open2(':memory:', u32.ptr, flags, 0) === OK)
const db = u32[0] + ((2 ** 32) * u32[1])
assert(exec2(db, 'PRAGMA auto_vacuum = none', 0, 0, u32.ptr) === OK)
assert(exec2(db, 'PRAGMA temp_store = memory', 0, 0, u32.ptr) === OK)
assert(exec2(db, 'PRAGMA locking_mode = exclusive', 0, 0, u32.ptr) === OK)
assert(exec2(db, 'pragma user_version = 1000', 0, 0, u32.ptr) === OK)
const sql = 'pragma user_version'
assert(prepare2(db, sql, utf8_length(sql), u32.ptr, 0) === OK)
const stmt = u32[0] + ((2 ** 32) * u32[1])
function get_version (stmt) {
if (reset(stmt) === -1) return -1
if(step(stmt) === ROW) return column_int(stmt, 0)
return finalize(stmt)
}
assert(get_version(stmt) === 1000)
const runs = parseInt(lo.args[2] || 20000000, 10)
const iter = 5
const bench = new Bench()
const name = 'user_version'
bench.name_width = name.length
for (let i = 0; i < iter; i++) {
bench.start(name)
for (let j = 0; j < runs; j++) {
assert(get_version(stmt) === 1000)
}
bench.end(runs)
}
finalize(stmt)
close2(db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment