Skip to content

Commit

Permalink
Add some caching to TypeScriptSignatureBuilder
Browse files Browse the repository at this point in the history
Cache some signatures to improve parser performance.
  • Loading branch information
knutwannheden committed Oct 23, 2023
1 parent 2a4077e commit 99f4c4f
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/main/java/org/openrewrite/TypeScriptSignatureBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import org.openrewrite.javascript.internal.tsc.generated.TSCTypeFlag;
import org.openrewrite.javascript.tree.TsType;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.*;

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

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

Map<TSCNode, String> signatures = new IdentityHashMap<>();

@Override
public String signature(@Nullable Object object) {
if (object == null) {
return "{undefined}";
}

TSCNode node = (TSCNode) object;
String cached = signatures.get(node);
if (cached != null) {
return cached;
}

switch (node.syntaxKind()) {
case SourceFile:
return mapSourceFileFqn((TSCNode.SourceFile) node);
cached = mapSourceFileFqn((TSCNode.SourceFile) node);
break;
case ClassDeclaration:
case EnumDeclaration:
case InterfaceDeclaration:
return node.hasProperty("typeParameters") && !node.getNodeListProperty("typeParameters").isEmpty() ?
parameterizedSignature(node) : classSignature(node);
case ArrayType:
return arraySignature(node);
cached = arraySignature(node);
break;
case EnumMember:
return mapEnumMember(node);
case Identifier:
Expand All @@ -69,7 +75,8 @@ public String signature(@Nullable Object object) {
case TypeOperator:
return mapTypeOperator(node);
case TypeParameter:
return genericSignature(node);
cached = genericSignature(node);
break;
case ExpressionWithTypeArguments:
case TypeReference:
case TypeQuery:
Expand All @@ -78,8 +85,14 @@ public String signature(@Nullable Object object) {
return TsType.Union.getFullyQualifiedName();
case PropertyDeclaration:
case VariableDeclaration:
return variableSignature(node);
cached = variableSignature(node);
break;
}
if (cached != null) {
signatures.put(node, cached);
return cached;
}

TSCType type = node.getTypeChecker().getTypeAtLocation(node);
return mapType(type);
}
Expand Down Expand Up @@ -299,8 +312,9 @@ private String mapParameter(TSCNode node) {

private String mapQualifiedName(TSCNode node) {
String left = signature(node.getNodeProperty("left"));
if (left.contains("<")) {
left = left.substring(0, left.indexOf('<'));
int index = left.indexOf('<');
if (index != -1) {
left = left.substring(0, index);
}
return left + "$" + node.getNodeProperty("right").getText();
}
Expand Down

0 comments on commit 99f4c4f

Please sign in to comment.