Skip to content

Reduce memory usage of holding BIR and source files #44185

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

heshanpadmasiri
Copy link
Member

@heshanpadmasiri heshanpadmasiri commented Jun 25, 2025

Purpose

Reduce memory usage of holding BIR and source files by holding them as weak references. If the actual values are GCed we will reload the file from disk seamlessly.

Also more aggressively prune negative atoms in ListInhabited

Approach

Describe how you are implementing the solutions along with the design details.

Samples

Provide high-level details about the samples related to this feature.

Remarks

List any other known issues, related PRs, TODO items, or any other notes related to the PR.

Check List

  • Read the Contributing Guide
  • Updated Change Log
  • Checked Tooling Support (#)
  • Added necessary tests
    • Unit Tests
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR reduces memory usage by holding BIR and source file contents as weak references and lazy-loading them when needed.

  • Introduces abstract BIRPackageFile and StringTextDocument with eager and lazy variants.
  • Updates ModuleContext to store BIR bytes in a WeakReference and adds a lazy reload path.
  • Refactors document loading (ProjectFiles, GeneralFSPackageRepository, etc.) to use Supplier for on-demand file reading.

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/ballerina-test-utils/src/main/java/org/ballerinalang/test/BCompileUtil.java Update import and use BIRPackageFile getter for binary content
misc/ballerina-bindgen/src/main/java/org/ballerinalang/bindgen/utils/BindgenFileGenerator.java Add Supplier import for lazy file loading
compiler/ballerina-tools-api/src/main/java/io/ballerina/tools/text/TextDocuments.java Support lazy text document creation with Supplier
compiler/ballerina-tools-api/src/main/java/io/ballerina/tools/text/StringTextDocument.java Refactor to abstract base and implement eager/lazy variants
compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/PackageFileWriter.java Use BIRPackageFile getter for writing binary content
compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/CompiledBinaryFile.java Remove legacy BIRPackageFile inner class
compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/BIRPackageFile.java Introduce abstract BIRPackageFile with eager and lazy implementations
compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/symbols/BPackageSymbol.java Update import to new BIRPackageFile class
compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/packaging/converters/FileSystemSourceInput.java Change syntax tree loading to use lazy content supplier
compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRBinaryWriter.java Simplify references to BIRPackageFile constants
compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/BIRPackageSymbolEnter.java Migrate definePackage to use ModuleContext and lazy BIRPackageFile
compiler/ballerina-lang/src/main/java/org/ballerinalang/repository/fs/GeneralFSPackageRepository.java Use supplier for lazy document loading
compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/ProjectFiles.java Refactor document loading to use Supplier
compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/PackageConfigCreator.java Use Supplier-based DocumentConfig for lazy content
compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/DocumentData.java Abstract DocumentData with eager and lazy subclasses
compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/BalaFiles.java Refactor BalaFiles document loading to use lazy Supplier
compiler/ballerina-lang/src/main/java/io/ballerina/projects/ModuleDescriptor.java Make moduleCompilationId() public for lazy BIR loader
compiler/ballerina-lang/src/main/java/io/ballerina/projects/ModuleContext.java Store BIR bytes in WeakReference and add lazy loader
compiler/ballerina-lang/src/main/java/io/ballerina/projects/MdDocumentContext.java Update to use Supplier-based DocumentConfig
compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java Refactor to use DocumentConfig abstraction for content
compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentConfig.java Introduce lazy DocumentConfig abstraction with Supplier
compiler/ballerina-lang/src/main/java/io/ballerina/projects/CodeModifierManager.java Create DocumentConfig with Supplier for modified docs
compiler/ballerina-lang/src/main/java/io/ballerina/projects/CodeGeneratorManager.java Use Supplier for generated document content
Comments suppressed due to low confidence (3)

compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/BIRPackageFile.java:41

  • [nitpick] Class name 'EagerBirPackageFile' uses 'Bir' casing, but 'BIR' is treated as an acronym elsewhere. Rename to 'EagerBIRPackageFile' for consistency.
    public static class EagerBirPackageFile extends BIRPackageFile {

compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/BIRPackageFile.java:61

  • [nitpick] Class name 'LazyBirPackageFile' uses 'Bir' casing, but 'BIR' is an acronym. Consider renaming to 'LazyBIRPackageFile' to keep naming consistent.
    public static class LazyBirPackageFile extends BIRPackageFile {

compiler/ballerina-lang/src/main/java/io/ballerina/projects/ModuleContext.java:76

  • [nitpick] The constant name 'DEFAULT_BIR_BYTE' suggests a single byte but holds a byte array reference. Rename to 'DEFAULT_BIR_BYTES' or 'DEFAULT_BIR_BYTES_REF' for clarity.
    private static final WeakReference<byte[]> DEFAULT_BIR_BYTE = new WeakReference<>(new byte[0]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant