Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 99f4c4f

Browse files
committed
Add some caching to TypeScriptSignatureBuilder
Cache some signatures to improve parser performance.
1 parent 2a4077e commit 99f4c4f

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/main/java/org/openrewrite/TypeScriptSignatureBuilder.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
import org.openrewrite.javascript.internal.tsc.generated.TSCTypeFlag;
2727
import org.openrewrite.javascript.tree.TsType;
2828

29-
import java.util.HashSet;
30-
import java.util.List;
31-
import java.util.Set;
32-
import java.util.StringJoiner;
29+
import java.util.*;
3330

3431
import static org.openrewrite.javascript.internal.tsc.TSCProgramContext.CompilerBridgeSourceKind.ApplicationCode;
3532

@@ -39,23 +36,32 @@ public class TypeScriptSignatureBuilder implements JavaTypeSignatureBuilder {
3936
@Nullable
4037
Set<String> typeVariableNameStack;
4138

39+
Map<TSCNode, String> signatures = new IdentityHashMap<>();
40+
4241
@Override
4342
public String signature(@Nullable Object object) {
4443
if (object == null) {
4544
return "{undefined}";
4645
}
4746

4847
TSCNode node = (TSCNode) object;
48+
String cached = signatures.get(node);
49+
if (cached != null) {
50+
return cached;
51+
}
52+
4953
switch (node.syntaxKind()) {
5054
case SourceFile:
51-
return mapSourceFileFqn((TSCNode.SourceFile) node);
55+
cached = mapSourceFileFqn((TSCNode.SourceFile) node);
56+
break;
5257
case ClassDeclaration:
5358
case EnumDeclaration:
5459
case InterfaceDeclaration:
5560
return node.hasProperty("typeParameters") && !node.getNodeListProperty("typeParameters").isEmpty() ?
5661
parameterizedSignature(node) : classSignature(node);
5762
case ArrayType:
58-
return arraySignature(node);
63+
cached = arraySignature(node);
64+
break;
5965
case EnumMember:
6066
return mapEnumMember(node);
6167
case Identifier:
@@ -69,7 +75,8 @@ public String signature(@Nullable Object object) {
6975
case TypeOperator:
7076
return mapTypeOperator(node);
7177
case TypeParameter:
72-
return genericSignature(node);
78+
cached = genericSignature(node);
79+
break;
7380
case ExpressionWithTypeArguments:
7481
case TypeReference:
7582
case TypeQuery:
@@ -78,8 +85,14 @@ public String signature(@Nullable Object object) {
7885
return TsType.Union.getFullyQualifiedName();
7986
case PropertyDeclaration:
8087
case VariableDeclaration:
81-
return variableSignature(node);
88+
cached = variableSignature(node);
89+
break;
8290
}
91+
if (cached != null) {
92+
signatures.put(node, cached);
93+
return cached;
94+
}
95+
8396
TSCType type = node.getTypeChecker().getTypeAtLocation(node);
8497
return mapType(type);
8598
}
@@ -299,8 +312,9 @@ private String mapParameter(TSCNode node) {
299312

300313
private String mapQualifiedName(TSCNode node) {
301314
String left = signature(node.getNodeProperty("left"));
302-
if (left.contains("<")) {
303-
left = left.substring(0, left.indexOf('<'));
315+
int index = left.indexOf('<');
316+
if (index != -1) {
317+
left = left.substring(0, index);
304318
}
305319
return left + "$" + node.getNodeProperty("right").getText();
306320
}

0 commit comments

Comments
 (0)