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

About CJS loaded code as different Source with multi-context sharing one engine #729

Closed
tellmewhattodo opened this issue May 1, 2023 · 4 comments
Assignees

Comments

@tellmewhattodo
Copy link

tellmewhattodo commented May 1, 2023

I found graal-js used a lot of memory to hold the CJS loaded Source ,which bothered me alot;

I enabled cjs like this, using a protected in memory file system:

private static final Map<String,String> EXPERIMENTAL_OPTIONS= new HashMap<>() {{
        put("js.commonjs-require", "true");
        put("js.commonjs-require-cwd", COMMON_JS_HOME_DIR);
}};
Context c=Context.Builder GraalJavaScriptContextBuilder = Context.newBuilder()
            .allowIO(true)
            .fileSystem(new ReadOnlyFileSystem())

When about 50+ context (shared with one engine) created and loaded a lot of cjs module separately, I noticed that memory usage reached high;

heap histogram like this ,and almost all Source is cjs module;

Class name,Bytes,Count
java.lang.ref.WeakReference,319935648,9997989
byte[],296328224,2146794
org.graalvm.compiler.truffle.runtime.OptimizedAssumption$Entry,223965072,9331878
char[],109149352,9208
com.oracle.truffle.api.strings.TruffleString,101205120,2108440
com.oracle.truffle.js.nodes.access.JSReadCurrentFrameSlotNodeGen,74348600,1858715
java.lang.Object[],67313040,1244053
com.oracle.truffle.api.source.SourceSectionLoaded,55399152,2308298
com.oracle.truffle.js.nodes.access.PropertyGetNode,44968640,1124216
com.oracle.truffle.js.nodes.access.JSWriteCurrentFrameSlotNodeGen,44640736,797156
com.oracle.truffle.js.nodes.access.PropertyNode,35647960,891199

I checked CJS builtinNode and found that all cjs source loaded through this method:
com.oracle.truffle.js.builtins.GlobalBuiltins.JSFileLoadingOperation#sourceFromTruffleFile.

When same file was loaded to different contexts, it generated different com.oracle.truffle.api.source.Source instance.
In my view, it will lead to repeated code parsing, JIT compiling, and a lot of other waste, I think it's the main reason for memory abnormal usage.
I'd like to know is there a way around, to let the same file loaded as same com.oracle.truffle.api.source.Source instance.
In other words, it's allowing setting engine level file system to share cjs files among multiple contexts.

using GraalVM 22.1.0 with GraalJS 22.1.0;
Ubuntu+Mac

@tellmewhattodo
Copy link
Author

when I want to have a test, I found the Endorse Mechanism doesn't work on JDK11/17,
Is there a way to let me test this, or is there anyone write something about this ?

@oubidar-Abderrahim
Copy link
Member

Could you please verify with the latest version of GraalVM (22.3.2) if the issue is still there please share a reproducer example that illustrate the issue. thank you

@oubidar-Abderrahim oubidar-Abderrahim self-assigned this May 16, 2023
@tellmewhattodo
Copy link
Author

Could you please verify with the latest version of GraalVM (22.3.2) if the issue is still there please share a reproducer example that illustrate the issue. thank you

Is there any modification at 22.3.2 ?
I'll have a try

@oubidar-Abderrahim
Copy link
Member

Closing this issue for inactivity, if you still face the issue in the latest version of Graal, please reopen or create a new issue. Thank you

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

No branches or pull requests

2 participants