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

Reduce memory use #23

Closed
overlookmotel opened this issue Jan 21, 2024 · 1 comment
Closed

Reduce memory use #23

overlookmotel opened this issue Jan 21, 2024 · 1 comment

Comments

@overlookmotel
Copy link

Hi. I read about Ganesha on OXC discussion.

I'm not familiar with how wasm-pack works, so I could be wrong, but I think the new implementation uses more memory than it needs.

If I am understanding it right, a single ModuleTransformerImpl is created when the loader initializes, and it will stay alive throughout the NodeJS process.

When ModuleTransformerImpl is created, it creates an oxc_allocator::Allocator which it holds on to until the NodeJS process exits:

ganesha/src/lib.rs

Lines 43 to 45 in a9b97d8

pub fn new () -> ModuleTransformerImpl {
Self(Allocator::default())
}

oxc_allocator::Allocator is an arena allocator. It's fast because it allocates memory in large chunks, but does not free any memory until the Allocator is dropped.

So every time the loader parses a TS file, OXC parses the code to an AST. That AST is stored in the Allocator, which allocates more memory. Once transforming the file is complete, the AST has fulfilled its purpose and is not needed any more, but because you're reusing the same Allocator, the memory never gets freed.

NB: AST structures can be pretty big even for small files.

So I imagine you'd be better off creating a new Allocator each time in fn transform, and dropping it at the end of transform. Then the loader will not hold on to any memory after it's done transforming.

Hope this is helpful.

@egasimus
Copy link
Collaborator

Sounds about right, great catch! I'll make the necessary changes.

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