From 72879470a3a3b5c63c5533f2ca4df752dcf2407d Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 6 Nov 2023 14:45:21 +0530 Subject: [PATCH] Refactor splitting init functions --- .../compiler/desugar/Desugar.java | 37 +++++++++++-------- .../compiler/util/Constants.java | 1 + 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index d3788a5e6e56..67687dc65bd2 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -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; @@ -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 @@ -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) { @@ -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; @@ -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; @@ -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; @@ -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(); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Constants.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Constants.java index fa63e619c50c..59438cb4f165 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Constants.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Constants.java @@ -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]+)\\}";