Skip to content

Commit

Permalink
Monomorphize allocators for tsserver/public API, just like core compi…
Browse files Browse the repository at this point in the history
…ler (#58045)
  • Loading branch information
jakebailey authored Apr 5, 2024
1 parent 39daa13 commit 3456c6e
Show file tree
Hide file tree
Showing 114 changed files with 153 additions and 352 deletions.
1 change: 1 addition & 0 deletions src/compiler/_namespaces/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from "../factory/nodeFactory";
export * from "../factory/emitNode";
export * from "../factory/emitHelpers";
export * from "../factory/nodeTests";
export * from "../factory/nodeChildren";
export * from "../factory/utilities";
export * from "../factory/utilitiesPublic";
export * from "../parser";
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/factory/nodeChildren.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Node } from "../_namespaces/ts";

const nodeChildren = new WeakMap<Node, Node[] | undefined>();

/** @internal */
export function getNodeChildren(node: Node): Node[] | undefined {
return nodeChildren.get(node);
}

/** @internal */
export function setNodeChildren(node: Node, children: Node[]): Node[] {
nodeChildren.set(node, children);
return children;
}

/** @internal */
export function unsetNodeChildren(node: Node) {
nodeChildren.delete(node);
}
3 changes: 2 additions & 1 deletion src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ import {
setEmitFlags,
setIdentifierAutoGenerate,
setIdentifierTypeArguments,
setNodeChildren,
setParent,
setTextRange,
ShorthandPropertyAssignment,
Expand Down Expand Up @@ -6210,7 +6211,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
// @api
function createSyntaxList(children: Node[]) {
const node = createBaseNode<SyntaxList>(SyntaxKind.SyntaxList);
node._children = children;
setNodeChildren(node, children);
return node;
}

Expand Down
8 changes: 3 additions & 5 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ import {
unescapeLeadingUnderscores,
UnionOrIntersectionTypeNode,
UnionTypeNode,
unsetNodeChildren,
UpdateExpression,
VariableDeclaration,
VariableDeclarationList,
Expand Down Expand Up @@ -10002,9 +10003,7 @@ namespace IncrementalParser {

// Ditch any existing LS children we may have created. This way we can avoid
// moving them forward.
if (node._children) {
node._children = undefined;
}
unsetNodeChildren(node);

setTextRangePosEnd(node, node.pos + delta, node.end + delta);

Expand Down Expand Up @@ -10160,7 +10159,7 @@ namespace IncrementalParser {
const fullEnd = child.end;
if (fullEnd >= changeStart) {
child.intersectsChange = true;
child._children = undefined;
unsetNodeChildren(child);

// Adjust the pos or end (or both) of the intersecting element accordingly.
adjustIntersectingElement(child, changeStart, changeRangeOldEnd, changeRangeNewEnd, delta);
Expand Down Expand Up @@ -10349,7 +10348,6 @@ namespace IncrementalParser {

export interface IncrementalNode extends Node, IncrementalElement {
hasBeenIncrementallyParsed: boolean;
_children: Node[] | undefined;
}

interface IncrementalNodeArray extends NodeArray<IncrementalNode>, IncrementalElement {
Expand Down
1 change: 0 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9695,7 +9695,6 @@ export interface DiagnosticCollection {
// SyntaxKind.SyntaxList
export interface SyntaxList extends Node {
kind: SyntaxKind.SyntaxList;
_children: Node[];
}

// dprint-ignore
Expand Down
16 changes: 13 additions & 3 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ import {
getLineStarts,
getModeForUsageLocation,
getNameOfDeclaration,
getNodeChildren,
getNormalizedAbsolutePath,
getNormalizedPathComponents,
getOwnKeys,
Expand Down Expand Up @@ -511,7 +512,6 @@ import {
SymbolFlags,
SymbolTable,
SyntaxKind,
SyntaxList,
TaggedTemplateExpression,
TemplateExpression,
TemplateLiteral,
Expand Down Expand Up @@ -1162,8 +1162,11 @@ export function getTokenPosOfNode(node: Node, sourceFile?: SourceFileLike, inclu
// the syntax list itself considers them as normal trivia. Therefore if we simply skip
// trivia for the list, we may have skipped the JSDocComment as well. So we should process its
// first child to determine the actual position of its first token.
if (node.kind === SyntaxKind.SyntaxList && (node as SyntaxList)._children.length > 0) {
return getTokenPosOfNode((node as SyntaxList)._children[0], sourceFile, includeJsDoc);
if (node.kind === SyntaxKind.SyntaxList) {
const first = firstOrUndefined(getNodeChildren(node));
if (first) {
return getTokenPosOfNode(first, sourceFile, includeJsDoc);
}
}

return skipTrivia(
Expand Down Expand Up @@ -8155,6 +8158,7 @@ export interface ObjectAllocator {
}

function Symbol(this: Symbol, flags: SymbolFlags, name: __String) {
// Note: if modifying this, be sure to update SymbolObject in src/services/services.ts
this.flags = flags;
this.escapedName = name;
this.declarations = undefined;
Expand All @@ -8172,20 +8176,23 @@ function Symbol(this: Symbol, flags: SymbolFlags, name: __String) {
}

function Type(this: Type, checker: TypeChecker, flags: TypeFlags) {
// Note: if modifying this, be sure to update TypeObject in src/services/services.ts
this.flags = flags;
if (Debug.isDebugging || tracing) {
this.checker = checker;
}
}

function Signature(this: Signature, checker: TypeChecker, flags: SignatureFlags) {
// Note: if modifying this, be sure to update SignatureObject in src/services/services.ts
this.flags = flags;
if (Debug.isDebugging) {
this.checker = checker;
}
}

function Node(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number) {
// Note: if modifying this, be sure to update NodeObject in src/services/services.ts
this.pos = pos;
this.end = end;
this.kind = kind;
Expand All @@ -8199,6 +8206,7 @@ function Node(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number) {
}

function Token(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number) {
// Note: if modifying this, be sure to update TokenOrIdentifierObject in src/services/services.ts
this.pos = pos;
this.end = end;
this.kind = kind;
Expand All @@ -8210,6 +8218,7 @@ function Token(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number)
}

function Identifier(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: number) {
// Note: if modifying this, be sure to update TokenOrIdentifierObject in src/services/services.ts
this.pos = pos;
this.end = end;
this.kind = kind;
Expand All @@ -8222,6 +8231,7 @@ function Identifier(this: Mutable<Node>, kind: SyntaxKind, pos: number, end: num
}

function SourceMapSource(this: SourceMapSource, fileName: string, text: string, skipTrivia?: (pos: number) => number) {
// Note: if modifying this, be sure to update SourceMapSourceObject in src/services/services.ts
this.fileName = fileName;
this.text = text;
this.skipTrivia = skipTrivia || (pos => pos);
Expand Down
Loading

0 comments on commit 3456c6e

Please sign in to comment.