microsoft / microsoft/TypeScript

Performance issue if many symbols are declared in repeated namespaces

Abierto
#53,565 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Experience Enhancement Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Bug Report

🔎 Search Terms

addDeclarationToSymbol, pushIfUnique, performance namespace bind

🕗 Version & Regression Information

This is the behavior in every version I tried: v4.9.4, v5.0.2

⏯ Playground Link

Repro in: https://github.com/frigus02/test-ts-add-declaration-to-symbol-set

Playground link with relevant code

💻 Code

We have a .d.ts generator that emits symbols in nested namespaces. For simplicity the generator always emits the entire namespace. Example:

declare namespace __generated.foo.bar {
  export class A {}
}
declare namespace __generated.foo.bar {
  export class B {}
}
declare namespace __generated.foo.bar.baz {
  export class C {}
}
// Repeat that a few thousand times

It seems this pattern runs into a performance issue inside addDeclarationToSymbol. It calls appendIfUnique(symbol.declarations, node), which iterates through the symbol.declarations array. The code above leads to lots of declarations of the symbols __generated, __generated.foo, etc.

The patch tsc_patch.diff uses a Set rather than iterating the array. This seems to speed up the build time significantly.

🙁 Actual behavior / 🙂 Expected behavior

The build is slower than it could be. A real project (which I can't share publicly) takes ~40 seconds to build currently. With the patch is takes ~16 seconds.

The example in https://github.com/frigus02/test-ts-add-declaration-to-symbol-set shows a "Total time" of 5.61s vs 1.19s.

Question

I realize that we could change the code generator to fix this. Unfortunately that is trickier than it seems and we likely can't do that in the next ~year.

Do you think such a patch (using a Set) is feasible here? If yes, I'd be happy to prepare a PR.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en addDeclarationToSymbol y su llamada a appendIfUnique; después reproduce el caso de un namespace repetido usando el repositorio de pruebas enlazado y el ejemplo de Playground. Compara el comportamiento actual y el propuesto basado en Set, comprobando la unicidad de las declaraciones y el rendimiento del compilador; se considera terminado cuando la regresión está cubierta y la ralentización reportada está solucionada sin cambiar las declaraciones emitidas.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers, performance
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.