Skip to content

Commit

Permalink
Merge pull request ballerina-platform#41078 from NipunaMadhushan/redu…
Browse files Browse the repository at this point in the history
…ce-observability-memory

Reduce `DefaultObservabilitySymbolCollector` object memory
  • Loading branch information
NipunaMadhushan authored Oct 26, 2023
2 parents 6d12828 + 759c0cf commit 8ac9786
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import io.ballerina.projects.Project;
import io.ballerina.projects.util.ProjectUtils;
import org.ballerinalang.diagramutil.DiagramUtil;
import org.ballerinalang.observability.anaylze.model.DocumentHolder;
import org.ballerinalang.observability.anaylze.model.ModuleHolder;
import org.ballerinalang.observability.anaylze.model.PackageHolder;
import org.wso2.ballerinalang.compiler.spi.ObservabilitySymbolCollector;
import org.wso2.ballerinalang.util.RepoUtils;
Expand Down Expand Up @@ -88,25 +86,10 @@ public void process(Project project) {
if (!isObservabilityIncluded) {
return;
}
Package currentPackage = project.currentPackage();
PackageCompilation packageCompilation = currentPackage.getCompilation();
packageCompilation.diagnosticResult(); // Trigger Compilation

packageHolder.setOrg(currentPackage.packageOrg().toString());
packageHolder.setName(currentPackage.packageName().toString());
packageHolder.setVersion(currentPackage.packageVersion().toString());

// Skip this part if project has not updated
if (ProjectUtils.isProjectUpdated(project)) {
for (ModuleId moduleId : currentPackage.moduleIds()) {
SemanticModel semanticModel = packageCompilation.getSemanticModel(moduleId);
Module module = currentPackage.module(moduleId);
for (DocumentId documentId : module.documentIds()) {
Document document = module.document(documentId);
JsonElement syntaxTreeJSON = DiagramUtil.getSyntaxTreeJSON(document, semanticModel);
packageHolder.addSyntaxTree(module.descriptor(), document.name(), syntaxTreeJSON);
}
}
packageHolder.setSyntaxTreeDataString(generateSyntaxTreeJsonString(project));
}
}

Expand All @@ -123,7 +106,7 @@ public void writeToExecutable(Path executableFile, Project project) throws IOExc
// Writing Syntax Tree Json
String syntaxTreeDataString;
if (ProjectUtils.isProjectUpdated(project)) {
syntaxTreeDataString = generateSyntaxTreeJsonString(packageHolder);
syntaxTreeDataString = packageHolder.getSyntaxTreeDataString();
} else {
// When project is not updated read Syntax Tree Json from Observability Symbols Jar
Path observeJarCachePath = project.targetDir()
Expand Down Expand Up @@ -173,7 +156,15 @@ private static String bytesToHex(byte[] hash) {
return hexString.toString();
}

private String generateSyntaxTreeJsonString(PackageHolder packageHolder) throws IOException {
private String generateSyntaxTreeJsonString(Project project) {
Package currentPackage = project.currentPackage();
PackageCompilation packageCompilation = currentPackage.getCompilation();
packageCompilation.diagnosticResult(); // Trigger Compilation

packageHolder.setOrg(currentPackage.packageOrg().toString());
packageHolder.setName(currentPackage.packageName().toString());
packageHolder.setVersion(currentPackage.packageVersion().toString());

final String ballerinaVersion = RepoUtils.getBallerinaVersion();
StringBuilder jsonStringBuilder = new StringBuilder().append("{\"")
.append(BALLERINA_VERSION_KEY).append("\":\"").append(ballerinaVersion).append("\",\"")
Expand All @@ -182,39 +173,48 @@ private String generateSyntaxTreeJsonString(PackageHolder packageHolder) throws
.append(PACKAGE_VERSION_KEY).append("\":\"").append(packageHolder.getVersion()).append("\",\"")
.append(PACKAGE_MODULES_KEY).append("\":{");

String[] moduleKeys = packageHolder.getModules().keySet().toArray(new String[0]);
Arrays.sort(moduleKeys);
for (int i = 0, packageNamesLength = moduleKeys.length; i < packageNamesLength; i++) {
String moduleKey = moduleKeys[i];
ModuleHolder moduleHolder = packageHolder.getModules().get(moduleKey);

if (i != 0) {
jsonStringBuilder.append(",");
}
ModuleId[] moduleIds = currentPackage.moduleIds().toArray(new ModuleId[0]);
Arrays.sort(moduleIds, (moduleId1, moduleId2) -> {
String moduleName1 = moduleId1.moduleName();
String moduleName2 = moduleId2.moduleName();
return moduleName1.compareTo(moduleName2);
});

for (ModuleId moduleId : moduleIds) {
SemanticModel semanticModel = packageCompilation.getSemanticModel(moduleId);
Module module = currentPackage.module(moduleId);
String moduleName = module.descriptor().name().toString();

jsonStringBuilder.append("\"").append(moduleKey).append("\":{\"")
.append(MODULE_NAME_KEY).append("\":\"").append(moduleHolder.getName()).append("\",\"")
jsonStringBuilder.append("\"").append(moduleName).append("\":{\"")
.append(MODULE_NAME_KEY).append("\":\"").append(moduleName).append("\",\"")
.append(MODULE_DOCUMENTS_KEY).append("\":{");

String[] documentKeys = moduleHolder.getDocuments().keySet().toArray(new String[0]);
Arrays.sort(documentKeys);
for (int j = 0, documentNamesLength = documentKeys.length; j < documentNamesLength; j++) {
String documentKey = documentKeys[j];
DocumentHolder documentHolder = moduleHolder.getDocuments().get(documentKey);
String syntaxTreeDataString = documentHolder.getSyntaxTree().toString();

if (j != 0) {
jsonStringBuilder.append(",");
}
jsonStringBuilder.append("\"").append(documentKey).append("\":{\"")
DocumentId[] documentIds = module.documentIds().toArray(new DocumentId[0]);
Arrays.sort(documentIds, (documentId1, documentId2) -> {
String documentName1 = module.document(documentId1).name();
String documentName2 = module.document(documentId2).name();
return documentName1.compareTo(documentName2);
});

for (DocumentId documentId : documentIds) {
Document document = module.document(documentId);
JsonElement syntaxTreeJSON = DiagramUtil.getSyntaxTreeJSON(document, semanticModel);

jsonStringBuilder.append("\"").append(document.name()).append("\":{\"")
.append(DOCUMENT_NAME_KEY).append("\":\"")
.append(documentHolder.getDocumentName()).append("\",\"")
.append(document.name()).append("\",\"")
.append(DOCUMENT_SYNTAX_TREE_KEY).append("\":")
.append(syntaxTreeDataString)
.append("}");
.append(syntaxTreeJSON.toString())
.append("}")
.append(",");
}
jsonStringBuilder.append("}}");
jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1);
jsonStringBuilder.append("}}")
.append(",");
}
jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1);

return jsonStringBuilder.append("}}").toString();
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
*/
package org.ballerinalang.observability.anaylze.model;

import com.google.gson.JsonElement;
import io.ballerina.projects.ModuleDescriptor;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Hold data related to a package.
Expand All @@ -34,16 +28,10 @@ public class PackageHolder {
private String org;
private String name;
private String version;
private final Map<String, ModuleHolder> modulesMap = new ConcurrentHashMap<>();

public void addSyntaxTree(ModuleDescriptor moduleDescriptor, String documentName, JsonElement syntaxTreeJson) {
String moduleName = moduleDescriptor.name().toString();
ModuleHolder moduleHolder = this.modulesMap.computeIfAbsent(moduleName, k -> new ModuleHolder(moduleName));
moduleHolder.addSyntaxTree(documentName, syntaxTreeJson);
}
private String syntaxTreeDataString;

public Map<String, ModuleHolder> getModules() {
return Collections.unmodifiableMap(modulesMap);
public void setSyntaxTreeDataString(String syntaxTreeDataString) {
this.syntaxTreeDataString = syntaxTreeDataString;
}

public String getOrg() {
Expand All @@ -69,4 +57,8 @@ public String getVersion() {
public void setVersion(String version) {
this.version = version;
}

public String getSyntaxTreeDataString() {
return syntaxTreeDataString;
}
}

0 comments on commit 8ac9786

Please sign in to comment.