Merge pull request #15271 from erik-krogh/fastTS

JS: faster TypeScript extraction
This commit is contained in:
Erik Krogh Kristensen 2024-01-10 21:02:34 +01:00 коммит произвёл GitHub
Родитель 52d3e3da31 06c1fff770
Коммит 51fe477ed1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 26 добавлений и 0 удалений

Просмотреть файл

@ -421,16 +421,42 @@ export class TypeTable {
return id;
}
/**
* Caches the result of `getId`: `type -> [id (not unfolded), id (unfolded)]`.
*
* A value of `undefined` means the value is not yet computed,
* and `number | null` corresponds to the return value of `getId`.
*/
private idCache = new WeakMap<ts.Type, [number | null | undefined, number | null | undefined]>();
/**
* Gets the canonical ID for the given type, generating a fresh ID if necessary.
*
* Returns `null` if we do not support extraction of this type.
*/
public getId(type: ts.Type, unfoldAlias: boolean): number | null {
let cached = this.idCache.get(type) ?? [undefined, undefined];
let cachedValue = cached[unfoldAlias ? 1 : 0];
if (cachedValue !== undefined) return cachedValue;
let result = this.getIdRaw(type, unfoldAlias);
cached[unfoldAlias ? 1 : 0] = result;
return result;
}
/**
* Gets the canonical ID for the given type, generating a fresh ID if necessary.
*
* Returns `null` if we do not support extraction of this type.
*/
public getIdRaw(type: ts.Type, unfoldAlias: boolean): number | null {
if (this.typeRecursionDepth > 100) {
// Ignore infinitely nested anonymous types, such as `{x: {x: {x: ... }}}`.
// Such a type can't be written directly with TypeScript syntax (as it would need to be named),
// but it can occur rarely as a result of type inference.
// Caching this value is technically incorrect, as a type might be seen at depth 101 and then we cache the fact that it can't be extracted.
// Then later the type is seen at a lower depth and could be extracted, but then we immediately give up because of the cached failure.
return null;
}
// Replace very long string literal types with `string`.