-
Notifications
You must be signed in to change notification settings - Fork 428
Speed up SelectQueryBuilder.if type checking #1965
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
Open
koskimas
wants to merge
1
commit into
remove-recursive-type-checks-from-kysely-class
Choose a base branch
from
speed-up-if-function-type-check
base: remove-recursive-type-checks-from-kysely-class
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { bench } from '@ark/attest' | ||
| import type { DB } from '../typings/test-d/huge-db.test-d.js' | ||
| import type { Kysely } from '../../dist/index.js' | ||
|
|
||
| // `$if` is how conditional filters, optional selections and optional joins get | ||
| // built in application code, so it tends to appear in every non-trivial query. | ||
| // | ||
| // Each of these numbers includes the one-time cost of relating two | ||
| // instantiations of the builder under test, because that is what a `$if` call | ||
| // site makes the compiler do: it has to match the callback's return type | ||
| // against the builder type in the signature. Keep the callback return types | ||
| // pointed at the smallest interface that still identifies the builder - see the | ||
| // comment on `SelectQueryBuilder.$if`. | ||
|
|
||
| declare const kysely: Kysely<DB> | ||
| declare const condition: boolean | ||
|
|
||
| console.log('if.bench.ts:\n') | ||
|
|
||
| bench.baseline(() => {}) | ||
|
|
||
| bench('kysely..select..$if(qb => qb.select(column))', () => { | ||
| return kysely | ||
| .selectFrom('my_table') | ||
| .select('my_table.id') | ||
| .$if(condition, (qb) => | ||
| qb.select('my_table.col_2e66a5c7e24d1d066230f368ce8b094e'), | ||
| ) | ||
| }).types([7027, 'instantiations']) | ||
|
|
||
| bench('kysely..select..$if(qb => qb.where(...))', () => { | ||
| return kysely | ||
| .selectFrom('my_table') | ||
| .select('my_table.id') | ||
| .$if(condition, (qb) => | ||
| qb.where('my_table.col_1d726898491fbca9a8dac855d2be1be8', '=', 1), | ||
| ) | ||
| }).types([3509, 'instantiations']) | ||
|
|
||
| bench('kysely..select..$if(qb => qb.innerJoin(...))', () => { | ||
| return kysely | ||
| .selectFrom('my_table') | ||
| .select('my_table.id') | ||
| .$if(condition, (qb) => | ||
| qb.innerJoin( | ||
| 'table_0b5ac72e03509e06683edcba4b3887ab', | ||
| 'table_0b5ac72e03509e06683edcba4b3887ab.id', | ||
| 'my_table.id', | ||
| ), | ||
| ) | ||
| }).types([8742, 'instantiations']) | ||
|
|
||
| bench('kysely..select..$if x3', () => { | ||
| return kysely | ||
| .selectFrom('my_table') | ||
| .select('my_table.id') | ||
| .$if(condition, (qb) => | ||
| qb.where('my_table.col_1d726898491fbca9a8dac855d2be1be8', '=', 1), | ||
| ) | ||
| .$if(condition, (qb) => | ||
| qb.select('my_table.col_2e66a5c7e24d1d066230f368ce8b094e'), | ||
| ) | ||
| .$if(condition, (qb) => | ||
| qb.select('my_table.col_4f84013a8b5e4c2b7529058c8fafcaa8'), | ||
| ) | ||
| }).types([10196, 'instantiations']) | ||
|
|
||
| bench('kysely..update..$if(qb => qb.returning(column))', () => { | ||
| return kysely | ||
| .updateTable('my_table') | ||
| .set('my_table.col_2e66a5c7e24d1d066230f368ce8b094e', 'x') | ||
| .returning('my_table.id') | ||
| .$if(condition, (qb) => | ||
| qb.returning('my_table.col_2e66a5c7e24d1d066230f368ce8b094e'), | ||
| ) | ||
| }).types([27600, 'instantiations']) | ||
|
|
||
| bench('kysely..delete..$if(qb => qb.returning(column))', () => { | ||
| return kysely | ||
| .deleteFrom('my_table') | ||
| .returning('my_table.id') | ||
| .$if(condition, (qb) => | ||
| qb.returning('my_table.col_2e66a5c7e24d1d066230f368ce8b094e'), | ||
| ) | ||
| }).types([12710, 'instantiations']) | ||
|
|
||
| bench('kysely..insert..$if(qb => qb.returning(column))', () => { | ||
| return kysely | ||
| .insertInto('my_table') | ||
| .defaultValues() | ||
| .returning('my_table.id') | ||
| .$if(condition, (qb) => | ||
| qb.returning('my_table.col_2e66a5c7e24d1d066230f368ce8b094e'), | ||
| ) | ||
| }).types([4083, 'instantiations']) | ||
|
|
||
| bench('kysely..$call(qb => qb.select(column))', () => { | ||
| return kysely | ||
| .selectFrom('my_table') | ||
| .select('my_table.id') | ||
| .$call((qb) => qb.select('my_table.col_2e66a5c7e24d1d066230f368ce8b094e')) | ||
| }).types([968, 'instantiations']) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { bench } from '@ark/attest' | ||
| import type { DB } from '../typings/test-d/huge-db.test-d.js' | ||
| import type { Kysely } from '../../dist/index.js' | ||
|
|
||
| // `insert-into.bench.ts` only covers picking the table. These cover the parts | ||
| // that carry data - the values themselves, upserts and `set` - which is where a | ||
| // row type gets matched against the table's insertable columns. Bulk inserts | ||
| // are worth watching in particular: the cost should stay flat in the number of | ||
| // rows, since every row is checked against the same type. | ||
|
|
||
| declare const kysely: Kysely<DB> | ||
| declare const kyselyAny: Kysely<any> | ||
|
|
||
| const row = { | ||
| col_1d726898491fbca9a8dac855d2be1be8: 1, | ||
| col_2e66a5c7e24d1d066230f368ce8b094e: 'a', | ||
| col_2f76db193eac6ad0f152563313673ac9: new Date(), | ||
| col_4f84013a8b5e4c2b7529058c8fafcaa8: 'b', | ||
| col_d2508118d0d39e198d1129d87d692d59: new Date(), | ||
| col_454ff479a3b5a9ef082d9be9ac02a6f4: 'c', | ||
| col_3917508388f24a50271f7088b657123c: 'd', | ||
| } | ||
|
|
||
| declare const rows: (typeof row)[] | ||
|
|
||
| console.log('insert-values.bench.ts:\n') | ||
|
|
||
| bench.baseline(() => { | ||
| return kysely.insertInto('my_table') | ||
| }) | ||
|
|
||
| bench('kysely.insertInto(table).values(row)', () => { | ||
| return kysely.insertInto('my_table').values(row) | ||
| }).types([1038, 'instantiations']) | ||
|
|
||
| bench('kysely.insertInto(table).values(~row)', () => { | ||
| // @ts-expect-error | ||
| return kysely.insertInto('my_table').values({ ...row, no_such_column: 1 }) | ||
| }).types([1171, 'instantiations']) | ||
|
|
||
| bench('kysely.insertInto(table).values([row])', () => { | ||
| return kysely.insertInto('my_table').values([row]) | ||
| }).types([1047, 'instantiations']) | ||
|
|
||
| bench('kysely.insertInto(table).values([row x5])', () => { | ||
| return kysely.insertInto('my_table').values([row, row, row, row, row]) | ||
| }).types([1047, 'instantiations']) | ||
|
|
||
| bench('kysely.insertInto(table).values([row x10])', () => { | ||
| return kysely | ||
| .insertInto('my_table') | ||
| .values([row, row, row, row, row, row, row, row, row, row]) | ||
| }).types([1047, 'instantiations']) | ||
|
|
||
| bench('kysely.insertInto(table).values(row[])', () => { | ||
| return kysely.insertInto('my_table').values(rows) | ||
| }).types([1045, 'instantiations']) | ||
|
|
||
| bench('kysely.insertInto(table).values(eb => row)', () => { | ||
| return kysely.insertInto('my_table').values((eb) => ({ | ||
| ...row, | ||
| col_1d726898491fbca9a8dac855d2be1be8: eb | ||
| .selectFrom('my_table as t2') | ||
| .select('t2.col_1d726898491fbca9a8dac855d2be1be8') | ||
| .limit(1), | ||
| })) | ||
| }).types([2522, 'instantiations']) | ||
|
|
||
| bench('kysely..onConflict(oc => oc.column(column).doNothing())', () => { | ||
| return kysely | ||
| .insertInto('my_table') | ||
| .values(row) | ||
| .onConflict((oc) => | ||
| oc.column('col_1d726898491fbca9a8dac855d2be1be8').doNothing(), | ||
| ) | ||
| }).types([1102, 'instantiations']) | ||
|
|
||
| bench('kysely..onConflict(oc => oc.column(column).doUpdateSet(row))', () => { | ||
| return kysely | ||
| .insertInto('my_table') | ||
| .values(row) | ||
| .onConflict((oc) => | ||
| oc.column('col_1d726898491fbca9a8dac855d2be1be8').doUpdateSet(row), | ||
| ) | ||
| }).types([2131, 'instantiations']) | ||
|
|
||
| bench('kysely..onConflict(oc => oc..doUpdateSet(eb => excluded ref))', () => { | ||
| return kysely | ||
| .insertInto('my_table') | ||
| .values(row) | ||
| .onConflict((oc) => | ||
| oc.column('col_1d726898491fbca9a8dac855d2be1be8').doUpdateSet({ | ||
| col_2e66a5c7e24d1d066230f368ce8b094e: (eb) => | ||
| eb.ref('excluded.col_2e66a5c7e24d1d066230f368ce8b094e'), | ||
| }), | ||
| ) | ||
| }).types([4458, 'instantiations']) | ||
|
|
||
| bench('kysely.updateTable(table).set(row)', () => { | ||
| return kysely.updateTable('my_table').set(row) | ||
| }).types([848, 'instantiations']) | ||
|
|
||
| bench('kysely.updateTable(table).set(column, value)', () => { | ||
| return kysely | ||
| .updateTable('my_table') | ||
| .set('my_table.col_2e66a5c7e24d1d066230f368ce8b094e', 'x') | ||
| }).types([3078, 'instantiations']) | ||
|
|
||
| bench('kysely.updateTable(table).set(eb => row)', () => { | ||
| return kysely.updateTable('my_table').set((eb) => ({ | ||
| col_1d726898491fbca9a8dac855d2be1be8: eb( | ||
| 'col_1d726898491fbca9a8dac855d2be1be8', | ||
| '+', | ||
| 1, | ||
| ), | ||
| })) | ||
| }).types([3520, 'instantiations']) | ||
|
Comment on lines
+99
to
+117
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these don't belong in this bench. |
||
|
|
||
| bench('kyselyAny.insertInto(table).values(row)', () => { | ||
| return kyselyAny.insertInto('my_table').values(row) | ||
| }).types([421, 'instantiations']) | ||
|
|
||
| bench('kyselyAny.updateTable(table).set(row)', () => { | ||
| return kyselyAny.updateTable('my_table').set(row) | ||
| }).types([333, 'instantiations']) | ||
|
Comment on lines
+123
to
+125
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't belong in this bench. |
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's probably better to: