microsoft / microsoft/TypeScript

Proposal: Flatten the AST to speed up tsgo

未关闭
#63,807 5 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Possible Improvement
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

We may be able to improve tsgo performance by changing the AST representation from a pointer-based tree to a flat array.

Problem

More than 20% of tsgo’s execution time is spent in GC, so optimizing GC should make it run faster.
For example, when running against the VSCode repository with GOGC=off, execution becomes 1.24 times faster:

Repository default (s) GOGC=off (s) Ratio Command
VSCode 6.567 5.292 1.24 ± 0.11 times faster npm run compile-check-ts-native

According to pprof, most of the GC time is spent scanning objects, and the parser AST accounts for about 50% of inuse_space. This suggests that reducing the scan cost of the AST could reduce GC time.

Proposed solution

I propose adopting a flat AST. Flattening an AST means converting its tree data structure into a simple slice.

As background, Go’s garbage collector does not scan the backing array of a slice when the slice element type contains no pointers. This optimization can eliminate the scan cost of large arrays.

If we can apply this to the AST, we should be able to reduce the amount of memory scanned during each GC cycle, which could reduce GC time.

As a first step, we could remove pointers from the Node struct like this:

type Arena struct {  
	Node    []Node  
	Data    []nodeData // still contains 2 pointers because of interface boxing  
}  
  
type NodeId uint32  
  
type Node struct {  
	Parent      NodeId // replace the parent pointer  
	Kind        Kind  
	Flags       NodeFlags  
	Loc         core.TextRange  
}

This would reduce the number of pointers in the AST and allow scanning to be skipped for []Node.

Note that nodeData still contains pointers. It may be possible to optimize this further by making Arena hold separate arrays for each type.

Trade-offs

Pros:

  • Better locality, which may improve CPU cache efficiency
  • Pointer-free Node

Cons:

  • Less intuitive API
  • More complex dynamic updates and deletion in arrays

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

该 issue 没有指定文件或测试;先使用 pprof 对 tsgo 进行性能分析,并比较默认 GC 与 GOGC=off 下的 VSCode 命令 npm run compile-check-ts-native。仔细阅读提议的 Arena、Node、NodeId 和 nodeData 表示方式。完成的标准应是具有经过验证的扁平 AST 设计,并且 GC 或执行性能得到可测量的改善,但未指定验收目标。

由索引模型根据 Issue 内容生成。

评估

技术栈
go
领域
compilers, performance
Issue 类型
重构
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。