Skip to content

Commit

Permalink
Merge pull request #41562 from Nadeeshan96/v1.2.45-file-too-large
Browse files Browse the repository at this point in the history
Create `init` functions in multiple classes
  • Loading branch information
warunalakshitha authored Nov 6, 2023
2 parents cb5f711 + 7287947 commit f914e44
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
import org.wso2.ballerinalang.compiler.util.Names;
import org.wso2.ballerinalang.compiler.util.TypeDefBuilderHelper;
import org.wso2.ballerinalang.compiler.util.TypeTags;
import org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnosticSource;
import org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos;
import org.wso2.ballerinalang.util.Flags;
import org.wso2.ballerinalang.util.Lists;
Expand Down Expand Up @@ -259,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 @@ -6310,13 +6312,20 @@ 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
* @return initial init function but trimmed in size
*/
private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env) {
int splitInitFuncClassCount = 1;
int splitFuncCount = 0;
DiagnosticPos packageNodePos = packageNode.pos;
String packageCUnitName = packageNodePos.src.cUnitName;
DiagnosticPos newFuncPos = getNewFuncPos(packageNodePos, packageCUnitName, splitInitFuncClassCount);
splitInitFuncClassCount++;
int methodSize = INIT_METHOD_SPLIT_SIZE;
BLangBlockFunctionBody funcBody = (BLangBlockFunctionBody) packageNode.initFunction.body;
if (!isJvmTarget) {
Expand All @@ -6341,6 +6350,12 @@ private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env)
if (i > 0 && (i % methodSize == 0 || isAssignmentWithInitOrRecordLiteralExpr(statement))) {
generatedFunctions.add(newFunc);
newFunc = createIntermediateInitFunction(packageNode, env);
splitFuncCount++;
if (splitFuncCount % INIT_FUNC_COUNT_PER_CLASS == 0) {
newFuncPos = getNewFuncPos(packageNodePos, packageCUnitName, splitInitFuncClassCount);
splitInitFuncClassCount++;
}
newFunc.pos = newFuncPos;
newFuncBody = (BLangBlockFunctionBody) newFunc.body;
symTable.rootScope.define(names.fromIdNode(newFunc.name), newFunc.symbol);
}
Expand All @@ -6361,6 +6376,12 @@ private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env)
if (newFuncBody.stmts.size() + chunkStmts.size() > methodSize) {
generatedFunctions.add(newFunc);
newFunc = createIntermediateInitFunction(packageNode, env);
splitFuncCount++;
if (splitFuncCount % INIT_FUNC_COUNT_PER_CLASS == 0) {
newFuncPos = getNewFuncPos(packageNodePos, packageCUnitName, splitInitFuncClassCount);
splitInitFuncClassCount++;
}
newFunc.pos = newFuncPos;
newFuncBody = (BLangBlockFunctionBody) newFunc.body;
symTable.rootScope.define(names.fromIdNode(newFunc.name), newFunc.symbol);
}
Expand All @@ -6382,6 +6403,12 @@ private BLangFunction splitInitFunction(BLangPackage packageNode, SymbolEnv env)
if (i > 0 && i % methodSize == 0) {
generatedFunctions.add(newFunc);
newFunc = createIntermediateInitFunction(packageNode, env);
splitFuncCount++;
if (splitFuncCount % INIT_FUNC_COUNT_PER_CLASS == 0) {
newFuncPos = getNewFuncPos(packageNodePos, packageCUnitName, splitInitFuncClassCount);
splitInitFuncClassCount++;
}
newFunc.pos = newFuncPos;
newFuncBody = (BLangBlockFunctionBody) newFunc.body;
symTable.rootScope.define(names.fromIdNode(newFunc.name), newFunc.symbol);
}
Expand Down Expand Up @@ -6423,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 f914e44

Please sign in to comment.