microsoft / microsoft/TypeScript

Proposal: Flatten the AST to speed up tsgo

Open
#63,807 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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](https://www.cs.cornell.edu/~asampson/blog/flattening.html).

As background, Go’s garbage collector [does not scan the backing array of a slice](https://go.dev/wiki/CompilerOptimizations#non-scannable-objects) 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests; start by profiling tsgo with pprof and comparing the VSCode command npm run compile-check-ts-native under default GC and GOGC=off. Read the proposed Arena, Node, NodeId, and nodeData representation carefully. Done would require a validated flat AST design with measured GC or execution improvements, but no acceptance target is specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
compilers, performance
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.