Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of type attribution #71

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 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,47 +36,72 @@ 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() ?
cached = node.hasProperty("typeParameters") && !node.getNodeListProperty("typeParameters").isEmpty() ?
parameterizedSignature(node) : classSignature(node);
break;
case ArrayType:
return arraySignature(node);
cached = arraySignature(node);
break;
case EnumMember:
return mapEnumMember(node);
cached = mapEnumMember(node);
break;
case Identifier:
return mapIdentifier(node);
cached = mapIdentifier(node);
break;
case Parameter:
return mapParameter(node);
cached = mapParameter(node);
break;
case QualifiedName:
return mapQualifiedName(node);
cached = mapQualifiedName(node);
break;
case ThisKeyword:
return mapThis(node);
cached = mapThis(node);
break;
case TypeOperator:
return mapTypeOperator(node);
cached = mapTypeOperator(node);
break;
case TypeParameter:
return genericSignature(node);
cached = genericSignature(node);
break;
case ExpressionWithTypeArguments:
case TypeReference:
case TypeQuery:
return mapTypeReference(node);
cached = mapTypeReference(node);
break;
case UnionType:
return TsType.Union.getFullyQualifiedName();
cached = TsType.Union.getFullyQualifiedName();
break;
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
Original file line number Diff line number Diff line change
Expand Up @@ -3449,7 +3449,7 @@ private Space whitespace() {
if (comments.isEmpty()) {
initialSpace += lastToken();
} else {
comments = ListUtils.map(
comments = ListUtils.mapLast(
comments,
comment -> comment.withSuffix(comment.getSuffix() + lastToken())
);
Expand All @@ -3467,9 +3467,10 @@ private Space whitespace() {
Markers.EMPTY
);
if (comments.isEmpty()) {
comments = Collections.singletonList(comment);
comments = new ArrayList<>(1);
comments.add(comment);
} else {
comments = ListUtils.concat(comments, comment);
comments.add(comment);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
class CommentTest extends ParserTest {

@Test
void singleLineComment() {
void singleLineComments() {
rewriteRun(
javaScript(
"""
class Test {
// C1
// C2
// C3
}
"""
)
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/openrewrite/javascript/tree/ParserPerfTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.javascript.tree;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.test.RewriteTest;

class ParserPerfTest implements RewriteTest {

// from https://github.com/apache/camel/blob/main/components/camel-cometd/src/test/resources/webapp/dojo/org/cometd.js
@Language("typescript")
private static final String cometd = StringUtils.readFully(ParserPerfTest.class.getResourceAsStream("/perf/cometd.js"));

@Test
void tryParseCometd() {
rewriteRun(ParserTest.javaScript(cometd));
}
}
Loading
Loading