microsoft / microsoft/TypeScript

TypeScript should have optimized this conditional check

オープン
#56,172 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Domain: Performance Help Wanted Possible Improvement
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

overloads, conditional types, aliases, performance, caching

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about aliases and conditional types.
⏯ Playground Link

https://github.com/solana-labs/solana-web3.js/pull/1760

💻 Code

Context

I had a large conditional type of the form:

type Overloads<T> =
    T extends {
        (...args: infer A1): infer R1;
        (...args: infer A2): infer R2;
        (...args: infer A3): infer R3;
        (...args: infer A4): infer R4;
    }
  ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
  : T extends {
        (...args: infer A1): infer R1;
        (...args: infer A2): infer R2;
        (...args: infer A3): infer R3;
    }
  ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
  : T extends {
        (...args: infer A1): infer R1;
        (...args: infer A2): infer R2;
    }
  ? [(...args: A1) => R1, (...args: A2) => R2]
  : T extends {
        (...args: infer A1): infer R1;
    }
  ? [(...args: A1) => R1]
  : unknown;

This refactor reduced check times in my project by 100x:

type Overloads<T> = Overloads4<T>;
type Overloads4<T> = T extends {
    (...args: infer A1): infer R1;
    (...args: infer A2): infer R2;
    (...args: infer A3): infer R3;
    (...args: infer A4): infer R4;
}
    ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
    : Overloads3<T>;
type Overloads3<T> = T extends {
    (...args: infer A1): infer R1;
    (...args: infer A2): infer R2;
    (...args: infer A3): infer R3;
}
    ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
    : Overloads2<T>;
type Overloads2<T> = T extends {
    (...args: infer A1): infer R1;
    (...args: infer A2): infer R2;
}
    ? [(...args: A1) => R1, (...args: A2) => R2]
    : Overloads1<T>;
type Overloads1<T> = T extends {
    (...args: infer A1): infer R1;
}
    ? [(...args: A1) => R1]
    : unknown;

Repro

# Check out the repo before the change
git clone git@github.com:solana-labs/solana-web3.js.git
cd solana-web3.js
git checkout ce145c805ee15ce0c5ef97623d9cfd006cf0b00b
pnpm i
cd packages/library/
pnpm turbo compile:js compile:typedefs
pnpm tsc -p ./tsconfig.json --noEmit --generateTrace tracing_output_folder
# Open tracing_output_folder/trace.json in chrome://tracing

rm -rf tracing_output_folder/

# Move to the commit after the change
git checkout ba9b4c72cb0dfc0a36fed1a0641a19492dd94624
pnpm turbo compile:js compile:typedefs
pnpm tsc -p ./tsconfig.json --noEmit --generateTrace tracing_output_folder
# Open tracing_output_folder/trace.json in chrome://tracing
🙁 Actual behavior

Check times in the project were >10s and Intellisense recomputed the entire project on every keystroke (ie. it took >10s to get typehints and autocomplete).

🙂 Expected behavior

TypeScript should have been able to optimize (ie. cache) this check on its own, without the need for the developer to manually alias each conditional type.

Additional information about the issue

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

一覧にある2つのコミットと packages/library の TypeScript コマンドを使って比較を再現し、生成された tracing_output_folder/trace.json ファイルを chrome://tracing で調べます。条件型のチェックとキャッシュの動作を調査します。エイリアスなしの同等のチェックが、型の結果を変更せずに冗長な処理を回避できれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers, performance
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
32/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。