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

perf: handle callback case better to inline fast api call #99

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ build/
# C benchmark binary
bench/bench
deno.lock
bun.lockb
1 change: 0 additions & 1 deletion bench/bench_bun.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ function bench(query) {
}

const query = createQuery(sql);
console.log(query.get());
bench(() => query.get());
1 change: 0 additions & 1 deletion bench/bench_deno.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ function bench(query) {
}

const query = db.prepare(sql);
console.log(query.get());
bench(() => query.get());
10 changes: 5 additions & 5 deletions bench/bench_deno_ffi.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ unwrap(
pHandle,
SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_PRIVATECACHE |
SQLITE3_OPEN_CREATE | SQLITE3_OPEN_MEMORY,
0,
null,
),
);
const db = pHandle[0] + 2 ** 32 * pHandle[1];
const db = Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]);

function exec(sql) {
const _pErr = new Uint32Array(2);
unwrap(sqlite3_exec(db, toCString(sql), 0, 0, _pErr));
unwrap(sqlite3_exec(db, toCString(sql), null, null, _pErr));
}

exec("PRAGMA auto_vacuum = none");
Expand Down Expand Up @@ -60,10 +60,10 @@ function prepareStatement() {
toCString(sql),
sql.length,
pHandle,
0,
null,
),
);
return pHandle[0] + 2 ** 32 * pHandle[1];
return Deno.UnsafePointer.create(pHandle[0] + 2 ** 32 * pHandle[1]);
}

const prepared = prepareStatement();
Expand Down
164 changes: 115 additions & 49 deletions src/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,7 @@ function getColumn(handle: Deno.PointerValue, i: number, int64: boolean): any {
}

case SQLITE_INTEGER: {
const v = sqlite3_column_int64(handle, i);
const numv = Number(v);
if (Number.isSafeInteger(numv)) {
return numv;
} else {
return v;
}
return sqlite3_column_int64(handle, i);
}

case SQLITE_FLOAT: {
Expand Down Expand Up @@ -364,34 +358,46 @@ export class Statement {
}

#runNoArgs(): number {
const handle = this.#handle;
this.#begin();
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
const status = step(this.#handle);
let status;
if (this.callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
if (status !== SQLITE3_ROW && status !== SQLITE3_DONE) {
unwrap(status, this.db.unsafeHandle);
}
sqlite3_reset(this.#handle);
sqlite3_reset(handle);
return sqlite3_changes(this.db.unsafeHandle);
}

#runWithArgs(...params: RestBindParameters): number {
const handle = this.#handle;
this.#begin();
this.#bindAll(params);
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
const status = step(this.#handle);
let status;
if (this.callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
if (!this.#hasNoArgs && !this.#bound && params.length) {
this.#bindRefs.clear();
}
if (status !== SQLITE3_ROW && status !== SQLITE3_DONE) {
unwrap(status, this.db.unsafeHandle);
}
sqlite3_reset(this.#handle);
sqlite3_reset(handle);
return sqlite3_changes(this.db.unsafeHandle);
}

#valuesNoArgs<T extends Array<unknown>>(): T[] {
const handle = this.#handle;
const callback = this.callback;
this.#begin();
const columnCount = sqlite3_column_count(this.#handle);
const columnCount = sqlite3_column_count(handle);
const result: T[] = [];
const getRowArray = new Function(
"getColumn",
Expand All @@ -406,25 +412,35 @@ export class Statement {
};
`,
)(getColumn);
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
let status = step(this.#handle);
let status;
if (callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
while (status === SQLITE3_ROW) {
result.push(getRowArray(this.#handle));
status = step(this.#handle);
result.push(getRowArray(handle));
if (callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
}
if (status !== SQLITE3_DONE) {
unwrap(status, this.db.unsafeHandle);
}
sqlite3_reset(this.#handle);
sqlite3_reset(handle);
return result as T[];
}

#valuesWithArgs<T extends Array<unknown>>(
...params: RestBindParameters
): T[] {
const handle = this.#handle;
const callback = this.callback;
this.#begin();
this.#bindAll(params);
const columnCount = sqlite3_column_count(this.#handle);
const columnCount = sqlite3_column_count(handle);
const result: T[] = [];
const getRowArray = new Function(
"getColumn",
Expand All @@ -439,19 +455,27 @@ export class Statement {
};
`,
)(getColumn);
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
let status = step(this.#handle);
let status;
if (callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
while (status === SQLITE3_ROW) {
result.push(getRowArray(this.#handle));
status = step(this.#handle);
result.push(getRowArray(handle));
if (callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
}
if (!this.#hasNoArgs && !this.#bound && params.length) {
this.#bindRefs.clear();
}
if (status !== SQLITE3_DONE) {
unwrap(status, this.db.unsafeHandle);
}
sqlite3_reset(this.#handle);
sqlite3_reset(handle);
return result as T[];
}

Expand Down Expand Up @@ -480,43 +504,62 @@ export class Statement {
}

#allNoArgs<T extends Record<string, unknown>>(): T[] {
const handle = this.#handle;
const callback = this.callback;
this.#begin();
const getRowObject = this.getRowObject();

const result: T[] = [];
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
let status = step(this.#handle);
let status;
if (callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
while (status === SQLITE3_ROW) {
result.push(getRowObject(this.#handle));
status = step(this.#handle);
result.push(getRowObject(handle));
if (callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
}
if (status !== SQLITE3_DONE) {
unwrap(status, this.db.unsafeHandle);
}
sqlite3_reset(this.#handle);
sqlite3_reset(handle);
return result as T[];
}

#allWithArgs<T extends Record<string, unknown>>(
...params: RestBindParameters
): T[] {
const handle = this.#handle;
const callback = this.callback;
this.#begin();
this.#bindAll(params);
const getRowObject = this.getRowObject();
const result: T[] = [];
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
let status = step(this.#handle);
let status;
if (callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
while (status === SQLITE3_ROW) {
result.push(getRowObject(this.#handle));
status = step(this.#handle);
result.push(getRowObject(handle));
if (callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
}
if (!this.#hasNoArgs && !this.#bound && params.length) {
this.#bindRefs.clear();
}
if (status !== SQLITE3_DONE) {
unwrap(status, this.db.unsafeHandle);
}
sqlite3_reset(this.#handle);
sqlite3_reset(handle);
return result as T[];
}

Expand All @@ -536,8 +579,12 @@ export class Statement {
}
}

const step = this.callback ? sqlite3_step_cb : sqlite3_step;
const status = step(handle);
let status;
if (this.callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}

if (!this.#hasNoArgs && !this.#bound && params.length) {
this.#bindRefs.clear();
Expand All @@ -562,8 +609,12 @@ export class Statement {
const cc = sqlite3_column_count(handle);
const arr = new Array(cc);
sqlite3_reset(handle);
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
const status = step(handle);
let status;
if (this.callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
if (status === SQLITE3_ROW) {
for (let i = 0; i < cc; i++) {
arr[i] = getColumn(handle, i, int64);
Expand Down Expand Up @@ -615,8 +666,12 @@ export class Statement {
}
}

const step = this.callback ? sqlite3_step_cb : sqlite3_step;
const status = step(handle);
let status;
if (this.callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}

if (!this.#hasNoArgs && !this.#bound && params.length) {
this.#bindRefs.clear();
Expand All @@ -638,17 +693,20 @@ export class Statement {
#getNoArgs<T extends Record<string, unknown>>(): T | undefined {
const handle = this.#handle;
const int64 = this.db.int64;

const columnNames = this.columnNames();
const row: Record<string, unknown> = this.#rowObject;
sqlite3_reset(handle);
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
const status = step(handle);
let status;
if (this.callback) {
status = sqlite3_step_cb(handle);
} else {
status = sqlite3_step(handle);
}
if (status === SQLITE3_ROW) {
for (let i = 0; i < columnNames?.length; i++) {
row[columnNames[i]] = getColumn(handle, i, int64);
}
sqlite3_reset(this.#handle);
sqlite3_reset(handle);
return row as T;
} else if (status === SQLITE3_DONE) {
return;
Expand All @@ -675,11 +733,19 @@ export class Statement {
*[Symbol.iterator](): IterableIterator<any> {
this.#begin();
const getRowObject = this.getRowObject();
const step = this.callback ? sqlite3_step_cb : sqlite3_step;
let status = step(this.#handle);
let status;
if (this.callback) {
status = sqlite3_step_cb(this.#handle);
} else {
status = sqlite3_step(this.#handle);
}
while (status === SQLITE3_ROW) {
yield getRowObject(this.#handle);
status = step(this.#handle);
if (this.callback) {
status = sqlite3_step_cb(this.#handle);
} else {
status = sqlite3_step(this.#handle);
}
}
if (status !== SQLITE3_DONE) {
unwrap(status, this.db.unsafeHandle);
Expand Down