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

Relating large unions of literal types performs poorly #53191

Closed
ahejlsberg opened this issue Mar 10, 2023 · 1 comment Β· Fixed by #53192
Closed

Relating large unions of literal types performs poorly #53191

ahejlsberg opened this issue Mar 10, 2023 · 1 comment Β· Fixed by #53192
Assignees
Labels
Domain: Performance Reports of unusually slow behavior Fix Available A PR has been opened for this issue

Comments

@ahejlsberg
Copy link
Member

πŸ•— Version & Regression Information

All versions.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Digits = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type T9XXXX = `9${Digits}${Digits}${Digits}${Digits}`;
type TXXXXX = `${Exclude<Digits, '0'>}${Digits}${Digits}${Digits}${Digits}`;

let x: Exclude<TXXXXX, T9XXXX> = '88888';

πŸ™ Actual behavior

The example above takes ~45s to compile on a reasonably fast machine. The underlying issue is that as we relate large unions of primitive types, we have a fast path for matches, but not for mismatches. Because the majority of the constituents of TXXXXX are not present in T9XXXX, we spend a lot of time in the slow path that attempts to find matches by iterating through all constituents.

πŸ™‚ Expected behavior

We can do much better by implementing a fast path for mismatches.

@ahejlsberg ahejlsberg self-assigned this Mar 10, 2023
@ahejlsberg ahejlsberg added this to the TypeScript 5.1.0 milestone Mar 10, 2023
@RyanCavanaugh RyanCavanaugh added the Domain: Performance Reports of unusually slow behavior label Mar 10, 2023
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Mar 10, 2023
@fatcerberus
Copy link

The example above takes ~45s to compile on a reasonably fast machine.

So more out of morbid curiosity than anything...

fatcerberus@Bruces-MacBook-Pro temp % time npx tsc wtf.ts
npx tsc wtf.ts  60.97s user 0.17s system 101% cpu 1:00.28 total
fatcerberus@Bruces-MacBook-Pro temp % node -v
v19.7.0

That was with an M2 Pro. Holy crap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Performance Reports of unusually slow behavior Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants