kysely: Case of extremely slow type checking
I have a query pattern that degrades tsc type checking performance to an outsized degree. It involves a nested query in selectFrom, and a simple where clause.
This is my most minimal reproduction:
const demoQuery = (db: Kysely<DB>) =>
db
.selectFrom((eb) =>
eb
.selectFrom("my_table")
// Removing this `where` makes a dramatic difference in compilation time.
.where((eb2) => eb2("my_table.id", "in", [1, 2, 3]))
.selectAll()
.as("test")
)
.selectAll();
With the where line indicated by the comment, running tsc --diagnostics --noEmit takes about 30 seconds, whereas after removing it it takes about a second.
TypeScript diagnostics with where:
Files: 435
Lines: 119235
Identifiers: 112580
Symbols: 258239
Types: 185820
Instantiations: 4743126
Memory used: 581215K
I/O read: 0.19s
I/O write: 0.00s
Parse time: 0.87s
Bind time: 0.27s
Check time: 30.30s
Emit time: 0.00s
Total time: 31.44s
TypeScript diagnostics WITHOUT where:
Files: 435
Lines: 119235
Identifiers: 112577
Symbols: 76912
Types: 2539
Instantiations: 10897
Memory used: 133318K
I/O read: 0.04s
I/O write: 0.00s
Parse time: 0.78s
Bind time: 0.23s
Check time: 0.25s
Emit time: 0.00s
Total time: 1.26s
My DB has a large number of tables, so I’m sure that’s part of the issue, but I never had this problem until I added this particular pathological query.
I’m using:
kyselyv0.27.2typescriptv5.3.3- Node v21.6.1
Edit: Here’s a Github gist with all of the files needed to reproduce this issue.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 29 (16 by maintainers)
Commits related to this issue
- Speedup types with huge databases. Fixes #867 — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to igalklebanov/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
- Speedup types with huge databases. Fixes #867 (#1080) — committed to kysely-org/kysely by koskimas 2 years ago
The issue is somehow related to
selectFromoverloads. If I replace all overloads with this single signature:the type checking time goes from 12 seconds to 0.7 seconds. That type also passes all Kysely tests except this one:
which tests generic usage of
selectFromwhich we don’t really support anyway.Fixed
updateTableanddeleteFromthe same way.Now I get this when there are no type errors
And this when there are:
Forgot to add that I’m immensely appreciative of the work done on Kysely. It’s a great tool that lets us work with our database without fear. 🙏
@koskimas You’re right! I could reproduce the issue when there’s a mistyped table name. Before the PR it’s ~28 s, with the PR it’s ~7 s. 🎉
There’s now a PR for this. @agj Would you be able to test the PR branch in your codebase?
You can try profiling the typescript type checker and figuring out which type causes the issue.
Yeah, sure. Here’s a Github gist with the relevant files, including an anonymized version of the database types.
@imwexpex There seems to be something in
jsonArrayFromandjsonObjectFromtypes combined with inline nested queries + huge database that kills typescript language server. I’ll take a look at those types.Having said that, you can already avoid the slowness and arguably make your queries much more readable by splitting them to helper functions like this:
would a split into multiple schemas on the same interface, differentiated by the prefix, alleviate the performance issue?
@agj I didn’t actually have any perf issues in your reproduction either before the fix when the types were correct, but once I mistyped something, I got the 12 second build time and 100s of thousands of instantations.
If you take 0.22.4 and test this
versus the branch, do you see a difference?
Well @koskimas, not sure if this is good or bad news. The issue was for version 0.22.2, and I just confirmed that the issue persists in that version as well as in 0.22.3.
However, both 0.22.4 and the PR’s version behave much better! In fact, indistinguishably so, both hover around 7 s for me, compared to the >60 s of the older versions. So, it looks like the changes in the PR don’t do much when it comes to this particular issue, but something in v0.22.4 fixed the issue.
I’m very happy that the issue is under control, and I’m sorry that I wasn’t able to identify in time that it was fixed in the latest release. 🙏
Also, if the issue was in intellisense and not in the build, why did you send the build benchmark? Did you actually check the intellisense performance with the new branch?
Well without any additional info, I won’t be able to help you. I’m not seeing slow intellisense in vscode in any of our tests, including @agj’s reproduction.
Intellisense uses the same type checker as the compiler.
@imwexpex Could you also build the PR branch and test if it fixes the peformance issues you’re having?
But we have the same issue with
updateTableand all otherQueryCreatormethods that have been split into multiple overloads 😞I’m not able to reproduce this. Any chance to get a full reproduction? A complete project I can run and profile?