Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move object method bodies into a separate class when original class is too large #41409

Merged
merged 31 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4cd5409
Generate separate class for object methods
Nadeeshan96 Sep 12, 2023
c68d0ce
Fix split class method call
Nadeeshan96 Sep 13, 2023
3322a12
Remove unnecessary code
Nadeeshan96 Sep 13, 2023
40fba6f
Fix creating object split class
Nadeeshan96 Sep 13, 2023
b0ef3ad
Clean the code
Nadeeshan96 Sep 13, 2023
9c34aef
Fix setOnInitialization calls
Nadeeshan96 Sep 14, 2023
7850e4f
Merge branch 'master-file-too-large-41279' into fix-file-too-large-is…
Nadeeshan96 Sep 14, 2023
037d987
Merge branch 'master' into master-file-too-large-41279
Nadeeshan96 Sep 15, 2023
b487158
Change java bytecode version
Nadeeshan96 Sep 15, 2023
3dabad3
Merge branch 'master-file-too-large-41279' into fix-file-too-large-is…
Nadeeshan96 Sep 15, 2023
fad698e
Split object methods across multiple classes
Nadeeshan96 Sep 15, 2023
9f308d3
Fix error stacktrace
Nadeeshan96 Sep 15, 2023
1415350
Split class methods based on method count
Nadeeshan96 Sep 18, 2023
47b217b
Add test with too large files
Nadeeshan96 Sep 18, 2023
8f2d66f
Merge branch 'master' into master-file-too-large-41279
Nadeeshan96 Sep 19, 2023
cc45972
Merge branch 'master-file-too-large-41279' into fix-file-too-large-is…
Nadeeshan96 Sep 19, 2023
1b65578
Split all object methods
Nadeeshan96 Sep 19, 2023
d350412
Fix object split method local variable table
Nadeeshan96 Oct 3, 2023
d12187e
Merge branch 'master' into fix-file-too-large-issue-41279
Nadeeshan96 Oct 3, 2023
4569272
Merge branch 'master' into fix-file-too-large-issue-41279
Nadeeshan96 Oct 17, 2023
1da8144
Add local var table for split class original methods
Nadeeshan96 Oct 17, 2023
cefd8d6
Revert "Split all object methods"
Nadeeshan96 Oct 18, 2023
b7e14c2
Add class file suffix
Nadeeshan96 Oct 18, 2023
3ef756a
Remove unnecessary null checks
Nadeeshan96 Oct 18, 2023
1bbe26b
Merge branch 'master' into fix-file-too-large-issue-41279
Nadeeshan96 Oct 27, 2023
48b4ec5
Rename JvmCodeGenUtil.populateMethodDesc method
Nadeeshan96 Oct 30, 2023
f329a76
Remove unnecessary type tag checking
Nadeeshan96 Oct 30, 2023
5d14031
Address PR review comments
Nadeeshan96 Oct 30, 2023
7cef69a
Edit large file test to increase coverage
Nadeeshan96 Oct 30, 2023
541dd2d
Merge branch 'master' into fix-file-too-large-issue-41279
Nadeeshan96 Nov 15, 2023
35bc64f
Merge branch 'master' into fix-file-too-large-issue-41279
Nadeeshan96 Nov 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ErrorValue extends BError implements RefValue {
private final Object details;

private static final String GENERATE_OBJECT_CLASS_PREFIX = "$value$";
private static final String SPLIT_CLASS_SUFFIX_REGEX = "\\$split\\$\\d";
private static final String GENERATE_PKG_INIT = "___init_";
private static final String GENERATE_PKG_START = "___start_";
private static final String GENERATE_PKG_STOP = "___stop_";
Expand Down Expand Up @@ -441,7 +442,7 @@ private Optional<StackTraceElement> filterStackTraceElement(StackTraceElement st
}

private String cleanupClassName(String className) {
return className.replace(GENERATE_OBJECT_CLASS_PREFIX, "");
return className.replace(GENERATE_OBJECT_CLASS_PREFIX, "").replaceAll(SPLIT_CLASS_SUFFIX_REGEX, "");
Nadeeshan96 marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean isCompilerAddedName(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import static io.ballerina.projects.util.ProjectConstants.BIN_DIR_NAME;
import static io.ballerina.projects.util.ProjectConstants.DOT;
import static io.ballerina.projects.util.ProjectUtils.getThinJarFileName;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CLASS_FILE_SUFFIX;
Nadeeshan96 marked this conversation as resolved.
Show resolved Hide resolved

/**
* This class represents the Ballerina compiler backend that produces executables that runs on the JVM.
Expand Down Expand Up @@ -756,7 +757,7 @@ public String getWarning(boolean listClasses) {
}

private void addConflictedJars(JarLibrary jarLibrary, HashMap<String, JarLibrary> copiedEntries, String entryName) {
if (entryName.endsWith(".class") && !entryName.endsWith("module-info.class")) {
if (entryName.endsWith(CLASS_FILE_SUFFIX) && !entryName.endsWith("module-info.class")) {
JarLibrary conflictingJar = copiedEntries.get(entryName);

// Ignore if conflicting jars has same name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ public static String getMethodDesc(List<BType> paramTypes, BType retType, BType
generateReturnType(retType);
}

public static String getMethodDesc(List<BType> paramTypes, BType retType, String attachedTypeClassName) {
Nadeeshan96 marked this conversation as resolved.
Show resolved Hide resolved
return INITIAL_METHOD_DESC + "L" + attachedTypeClassName + ";" + populateMethodDesc(paramTypes) +
generateReturnType(retType);
}

public static String populateMethodDesc(List<BType> paramTypes) {
StringBuilder descBuilder = new StringBuilder();
for (BType type : paramTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public class JvmConstants {

// code generation related constants.
public static final String MODULE_INIT_CLASS_NAME = "$_init";
public static final String OBJECT_SELF_INSTANCE = "self";
public static final String UNION_TYPE_CONSTANT_CLASS_NAME = "constants/$_union_type_constants";
public static final String ERROR_TYPE_CONSTANT_CLASS_NAME = "constants/$_error_type_constants";
public static final String TUPLE_TYPE_CONSTANT_CLASS_NAME = "constants/$_tuple_type_constants";
Expand Down Expand Up @@ -380,10 +381,13 @@ public class JvmConstants {
public static final String START_OF_HEADING_WITH_SEMICOLON = ":\u0001";
public static final String CREATE_INTEROP_ERROR_METHOD = "createInteropError";
public static final String LAMBDA_PREFIX = "$lambda$";
public static final String SPLIT_CLASS_SUFFIX = "$split$";
public static final String POPULATE_METHOD_PREFIX = "$populate";
public static final String ADD_METHOD = "add";
public static final String TEST_EXECUTION_STATE = "__gH7W16nQmp0TestExecState__";
public static final String GET_TEST_EXECUTION_STATE = "$getTestExecutionState";
public static final String STRAND_LOCAL_VARIABLE_NAME = "__strand";
public static final String CLASS_FILE_SUFFIX = ".class";

// scheduler related constants
public static final String SCHEDULE_FUNCTION_METHOD = "scheduleFunction";
Expand Down Expand Up @@ -446,6 +450,7 @@ public class JvmConstants {
public static final int MAX_CALLS_PER_CLIENT_METHOD = 100;
public static final int MAX_CONSTANTS_PER_METHOD = 100;
public static final int MAX_CALLS_PER_FUNCTION_CALL_METHOD = 100;
public static final int MAX_METHOD_COUNT_PER_BALLERINA_OBJECT = 100;
/*
MAX_STRINGS_PER_METHOD is calculated as below.
No of instructions required for create ballerina string constant object = 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.MAP_VALUE;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.MATH_UTILS;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.MODULE_INIT_CLASS_NAME;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.OBJECT_SELF_INSTANCE;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.OBJECT_TYPE_IMPL;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.REG_EXP_FACTORY;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.SHORT_VALUE;
Expand Down Expand Up @@ -432,7 +433,7 @@ public void generateVarLoad(MethodVisitor mv, BIRNode.BIRVariableDcl varDcl, int

switch (varDcl.kind) {
case SELF:
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, this.indexMap.get(OBJECT_SELF_INSTANCE));
return;
case CONSTANT:
case GLOBAL:
Expand Down Expand Up @@ -1517,7 +1518,7 @@ void generateObjectStoreIns(BIRNonTerminator.FieldAccess objectStoreIns) {
this.mv.visitTypeInsn(CHECKCAST, className);
visitKeyValueExpressions(objectStoreIns);
// invoke setOnInitialization() method
this.mv.visitMethodInsn(INVOKESPECIAL, className, "setOnInitialization",
this.mv.visitMethodInsn(INVOKEVIRTUAL, className, "setOnInitialization",
SET_ON_INIT, false);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.isExternFunc;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.toNameString;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.BALLERINA;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CLASS_FILE_SUFFIX;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CONSTANT_INIT_METHOD_PREFIX;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CURRENT_MODULE_VAR_NAME;
import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ENCODED_DOT_CHARACTER;
Expand Down Expand Up @@ -243,7 +244,7 @@ private static void generateLockForVariable(ClassWriter cw) {
private static void generateStaticInitializer(ClassWriter cw, String className, BIRPackage birPackage,
boolean isInitClass, boolean serviceEPAvailable,
AsyncDataCollector asyncDataCollector,
JvmConstantsGen jvmConstantsGen, boolean isTestablePackage) {
JvmConstantsGen jvmConstantsGen) {
if (!isInitClass && asyncDataCollector.getStrandMetadata().isEmpty()) {
return;
}
Expand Down Expand Up @@ -432,11 +433,11 @@ private void generateModuleClasses(BIRPackage module, Map<String, byte[]> jarEnt
}
JvmCodeGenUtil.visitStrandMetadataFields(cw, asyncDataCollector.getStrandMetadata());
generateStaticInitializer(cw, moduleClass, module, isInitClass, serviceEPAvailable,
asyncDataCollector, jvmConstantsGen, isTestable);
asyncDataCollector, jvmConstantsGen);
cw.visitEnd();

byte[] bytes = getBytes(cw, module);
jarEntries.put(moduleClass + ".class", bytes);
jarEntries.put(moduleClass + CLASS_FILE_SUFFIX, bytes);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,6 @@ static String getStrandMetadataVarName(String typeName, String parentFunction) {
}

private void loadFpReturnType(BIROperand lhsOp) {

BType futureType = JvmCodeGenUtil.getImpliedType(lhsOp.variableDcl.type);
BType returnType = symbolTable.anyType;
if (futureType.tag == TypeTags.FUTURE) {
Expand All @@ -1289,12 +1288,10 @@ private int getJVMIndexOfVarRef(BIRNode.BIRVariableDcl varDcl) {
}

private void loadVar(BIRNode.BIRVariableDcl varDcl) {

jvmInstructionGen.generateVarLoad(this.mv, varDcl, this.getJVMIndexOfVarRef(varDcl));
}

private void storeToVar(BIRNode.BIRVariableDcl varDcl) {

jvmInstructionGen.generateVarStore(this.mv, varDcl, this.getJVMIndexOfVarRef(varDcl));
}

Expand Down
Loading