Skip to content

Commit

Permalink
SONARPY-2467 Fix performance bottleneck of GlobalSymbolComputation du…
Browse files Browse the repository at this point in the history
…e to DjangoViewsVisitor
  • Loading branch information
guillaume-dequenne-sonarsource committed Dec 13, 2024
1 parent 538c939 commit 16ab469
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.sonar.python.types.v2.FunctionType;
import org.sonar.python.types.v2.PythonType;
import org.sonar.python.types.v2.TriBool;
import org.sonar.python.types.v2.TypeCheckBuilder;
import org.sonar.python.types.v2.TypeChecker;
import org.sonar.python.types.v2.UnknownType;

Expand Down Expand Up @@ -223,11 +224,21 @@ public Collection<Symbol> stubFilesSymbols() {
private class DjangoViewsVisitor extends BaseTreeVisitor {

String fullyQualifiedModuleName;
private TypeCheckBuilder confPathCall = null;
private TypeCheckBuilder pathCall = null;

public DjangoViewsVisitor(String fullyQualifiedModuleName) {
this.fullyQualifiedModuleName = fullyQualifiedModuleName;
}

@Override
public void visitFileInput(FileInput fileInput) {
TypeChecker typeChecker = new TypeChecker(new BasicTypeTable(new ProjectLevelTypeTable(ProjectLevelSymbolTable.this)));
confPathCall = typeChecker.typeCheckBuilder().isTypeWithName("django.urls.conf.path");
pathCall = typeChecker.typeCheckBuilder().isTypeWithName("django.urls.path");
super.visitFileInput(fileInput);
}

@Override
public void visitCallExpression(CallExpression callExpression) {
super.visitCallExpression(callExpression);
Expand All @@ -246,9 +257,8 @@ public void visitCallExpression(CallExpression callExpression) {
}

private boolean isCallRegisteringDjangoView(CallExpression callExpression) {
TypeChecker typeChecker = new TypeChecker(new BasicTypeTable(new ProjectLevelTypeTable(ProjectLevelSymbolTable.this)));
TriBool isConfPathCall = typeChecker.typeCheckBuilder().isTypeWithName("django.urls.conf.path").check(callExpression.callee().typeV2());
TriBool isPathCall = typeChecker.typeCheckBuilder().isTypeWithName("django.urls.path").check(callExpression.callee().typeV2());
TriBool isConfPathCall = confPathCall.check(callExpression.callee().typeV2());
TriBool isPathCall = pathCall.check(callExpression.callee().typeV2());
return isConfPathCall.equals(TriBool.TRUE) || isPathCall.equals(TriBool.TRUE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ void importedStubModules() {
""");
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
projectLevelSymbolTable.addModule(tree, "my_package", pythonFile("mod.py"));
assertThat(projectLevelSymbolTable.typeShedDescriptorsProvider().stubModules()).containsExactlyInAnyOrder("math", "os");
assertThat(projectLevelSymbolTable.typeShedDescriptorsProvider().stubModules()).containsExactlyInAnyOrder("math", "os", "django", "django.urls.conf", "django.urls");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ void write_cpd_tokens_to_cache() throws IOException {

assertThat(writeCache.getData().keySet()).containsExactlyInAnyOrder(
"python:cache_version", "python:files", "python:descriptors:moduleKey:pass.py", "python:imports:moduleKey:pass.py",
"python:cpd:data:moduleKey:pass.py", "python:cpd:stringTable:moduleKey:pass.py", "python:content_hashes:moduleKey:pass.py");
"python:cpd:data:moduleKey:pass.py", "python:cpd:stringTable:moduleKey:pass.py", "python:content_hashes:moduleKey:pass.py", "python:typeshed_modules");

byte[] tokenData = writeCache.getData().get("python:cpd:data:moduleKey:pass.py");
byte[] stringTable = writeCache.getData().get("python:cpd:stringTable:moduleKey:pass.py");
Expand Down

0 comments on commit 16ab469

Please sign in to comment.