Skip to content

Commit

Permalink
Refactor splitting init functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadeeshan96 committed Nov 6, 2023
1 parent d25015f commit 7287947
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
import static org.wso2.ballerinalang.compiler.desugar.ASTBuilderUtil.createStatementExpression;
import static org.wso2.ballerinalang.compiler.desugar.ASTBuilderUtil.createVariable;
import static org.wso2.ballerinalang.compiler.util.Constants.DESUGARED_MAPPING_CONSTR_KEY;
import static org.wso2.ballerinalang.compiler.util.Constants.INIT_FUNC_COUNT_PER_CLASS;
import static org.wso2.ballerinalang.compiler.util.Constants.INIT_METHOD_SPLIT_SIZE;
import static org.wso2.ballerinalang.compiler.util.Names.GEN_VAR_PREFIX;
import static org.wso2.ballerinalang.compiler.util.Names.IGNORE;
Expand Down Expand Up @@ -6311,7 +6312,8 @@ private void visitBinaryLogicalExpr(BLangBinaryExpr binaryExpr) {
}

/**
* Split packahe init function into several smaller functions.
* Split package init function into several smaller functions and put them into multiple classes
* if the function count is very high.
*
* @param packageNode package node
* @param env symbol environment
Expand All @@ -6322,9 +6324,8 @@ private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env)
int splitFuncCount = 0;
DiagnosticPos packageNodePos = packageNode.pos;
String packageCUnitName = packageNodePos.src.cUnitName;
DiagnosticPos newFuncPos = new DiagnosticPos(new BDiagnosticSource(packageNodePos.src.pkgID,
packageCUnitName + "$" + splitInitFuncClassCount), packageNodePos.sLine,
packageNodePos.eLine, packageNodePos.sCol, packageNodePos.eCol);
DiagnosticPos newFuncPos = getNewFuncPos(packageNodePos, packageCUnitName, splitInitFuncClassCount);
splitInitFuncClassCount++;
int methodSize = INIT_METHOD_SPLIT_SIZE;
BLangBlockFunctionBody funcBody = (BLangBlockFunctionBody) packageNode.initFunction.body;
if (!isJvmTarget) {
Expand All @@ -6350,10 +6351,9 @@ private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env)
generatedFunctions.add(newFunc);
newFunc = createIntermediateInitFunction(packageNode, env);
splitFuncCount++;
if (splitFuncCount % INIT_METHOD_SPLIT_SIZE == 0) {
newFuncPos = new DiagnosticPos(new BDiagnosticSource(packageNodePos.src.pkgID,
packageCUnitName + "$" + splitInitFuncClassCount++), packageNodePos.sLine,
packageNodePos.eLine, packageNodePos.sCol, packageNodePos.eCol);
if (splitFuncCount % INIT_FUNC_COUNT_PER_CLASS == 0) {
newFuncPos = getNewFuncPos(packageNodePos, packageCUnitName, splitInitFuncClassCount);
splitInitFuncClassCount++;
}
newFunc.pos = newFuncPos;
newFuncBody = (BLangBlockFunctionBody) newFunc.body;
Expand All @@ -6377,10 +6377,9 @@ private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env)
generatedFunctions.add(newFunc);
newFunc = createIntermediateInitFunction(packageNode, env);
splitFuncCount++;
if (splitFuncCount % INIT_METHOD_SPLIT_SIZE == 0) {
newFuncPos = new DiagnosticPos(new BDiagnosticSource(packageNodePos.src.pkgID,
packageCUnitName + "$" + splitInitFuncClassCount++), packageNodePos.sLine,
packageNodePos.eLine, packageNodePos.sCol, packageNodePos.eCol);
if (splitFuncCount % INIT_FUNC_COUNT_PER_CLASS == 0) {
newFuncPos = getNewFuncPos(packageNodePos, packageCUnitName, splitInitFuncClassCount);
splitInitFuncClassCount++;
}
newFunc.pos = newFuncPos;
newFuncBody = (BLangBlockFunctionBody) newFunc.body;
Expand All @@ -6405,10 +6404,9 @@ private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env)
generatedFunctions.add(newFunc);
newFunc = createIntermediateInitFunction(packageNode, env);
splitFuncCount++;
if (splitFuncCount % INIT_METHOD_SPLIT_SIZE == 0) {
newFuncPos = new DiagnosticPos(new BDiagnosticSource(packageNodePos.src.pkgID,
packageCUnitName + "$" + splitInitFuncClassCount++), packageNodePos.sLine,
packageNodePos.eLine, packageNodePos.sCol, packageNodePos.eCol);
if (splitFuncCount % INIT_FUNC_COUNT_PER_CLASS == 0) {
newFuncPos = getNewFuncPos(packageNodePos, packageCUnitName, splitInitFuncClassCount);
splitInitFuncClassCount++;
}
newFunc.pos = newFuncPos;
newFuncBody = (BLangBlockFunctionBody) newFunc.body;
Expand Down Expand Up @@ -6452,6 +6450,13 @@ private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env)
return generatedFunctions.get(0);
}

private static DiagnosticPos getNewFuncPos(DiagnosticPos packageNodePos, String packageCUnitName,
int splitInitFuncClassCount) {
return new DiagnosticPos(new BDiagnosticSource(packageNodePos.src.pkgID,
packageCUnitName + "$" + splitInitFuncClassCount), packageNodePos.sLine,
packageNodePos.eLine, packageNodePos.sCol, packageNodePos.eCol);
}

private boolean isAssignmentWithInitOrRecordLiteralExpr(BLangStatement statement) {
if (statement.getKind() == NodeKind.ASSIGNMENT) {
NodeKind exprKind = ((BLangAssignment) statement).getExpression().getKind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private Constants() {
public static final String OPEN_SEALED_ARRAY = "*";

public static final int INIT_METHOD_SPLIT_SIZE = 50;
public static final int INIT_FUNC_COUNT_PER_CLASS = 100;

public static final String UNICODE_REGEX = "\\\\u\\{([a-fA-F0-9]+)\\}";

Expand Down

0 comments on commit 7287947

Please sign in to comment.