microsoft / microsoft/TypeScript

Investigate removing the concept of object allocators

Open
#52,958 0 comments 0 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Feb 25, 2023.

Committed Domain: Performance Experimentation Needed Infrastructure Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Today we have the concept of an "object allocator". This is basically a set of functions, each which returns a constructor function for a commonly used data structure in the compiler.

/** @internal */
export interface ObjectAllocator {
    getNodeConstructor(): new (kind: SyntaxKind, pos: number, end: number) => Node;
    getTokenConstructor(): new <TKind extends SyntaxKind>(kind: TKind, pos: number, end: number) => Token<TKind>;
    getIdentifierConstructor(): new (kind: SyntaxKind.Identifier, pos: number, end: number) => Identifier;
    getPrivateIdentifierConstructor(): new (kind: SyntaxKind.PrivateIdentifier, pos: number, end: number) => PrivateIdentifier;
    getSourceFileConstructor(): new (kind: SyntaxKind.SourceFile, pos: number, end: number) => SourceFile;
    getSymbolConstructor(): new (flags: SymbolFlags, name: __String) => Symbol;
    getTypeConstructor(): new (checker: TypeChecker, flags: TypeFlags) => Type;
    getSignatureConstructor(): new (checker: TypeChecker, flags: SignatureFlags) => Signature;
    getSourceMapSourceConstructor(): new (fileName: string, text: string, skipTrivia?: (pos: number) => number) => SourceMapSource;
}

Why do this? Because for our batch compiler, TypeScript uses constructors with more bare-bones objects. For the most part, there are no members on the prototype, and we work purely with instance-set data.

On the other hand, the language service and TypeScript's API provides constructors that are attached to richer prototypes. For example, Nodes constructed by the language service have methods like getStart(), getEnd(), getSourceFile(), etc. These are partially for convenience and if my memory serves correctly, partially to power backwards-compatibility going back to TypeScript 1.0's tree API.

However, managing the two is slightly annoying, and creates layers of indirection. It would be nice to see if we can move away from this indirection and whether it can eliminate runtime overhead at all.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.