Skip to content

Appendix on memory usage #1302

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

Merged
merged 7 commits into from
Apr 10, 2025

Conversation

beta-ziliani
Copy link
Contributor

No description provided.

@beta-ziliani beta-ziliani requested review from a team as code owners April 8, 2025 22:18
Copy link

changeset-bot bot commented Apr 8, 2025

⚠️ No Changeset found

Latest commit: 66fe790

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@ggiraldez ggiraldez left a comment

Choose a reason for hiding this comment

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

Left some suggestions.

Co-authored-by: Gustavo Giráldez <[email protected]>
Copy link
Member

@alcuadrado alcuadrado left a comment

Choose a reason for hiding this comment

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

I like this guide. It's clear, and the suggestion isn't resorting to manual memory management.

@@ -0,0 +1,39 @@
# 9.2. Considerations on memory usage
Copy link
Contributor

@OmarTawfik OmarTawfik Apr 10, 2025

Choose a reason for hiding this comment

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

I think there is a difference between the title Considerations on memory usage and the slug slang-memory-management. I suggest consolidating it here + index.md + NAVIGATION.md above:

Suggested change
# 9.2. Considerations on memory usage
# 9.2. Slang Memory Management


## Internal vs external memory

JavaScript engines distinguish between the memory space that contains JavaScript objects, called _internal memory_ or _heap space_, and the space used for Wasm components, called _external memory_. The Slang objects that you interact with reside in internal memory. However, a Slang object, such as a `Cursor`, holds a _handle_ to a Wasm component that manages a corresponding resource in the Rust implementation. In the case of a `Cursor`, that resource contains among other things _a reference to the entire parsing tree_. Therefore, an instance of the `Cursor` class in JavaScript is lightweight in terms of internal memory as it only contains the handle number. But it is associated with a significant amount of external memory—the actual cursor in Wasm-land.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
JavaScript engines distinguish between the memory space that contains JavaScript objects, called _internal memory_ or _heap space_, and the space used for Wasm components, called _external memory_. The Slang objects that you interact with reside in internal memory. However, a Slang object, such as a `Cursor`, holds a _handle_ to a Wasm component that manages a corresponding resource in the Rust implementation. In the case of a `Cursor`, that resource contains among other things _a reference to the entire parsing tree_. Therefore, an instance of the `Cursor` class in JavaScript is lightweight in terms of internal memory as it only contains the handle number. But it is associated with a significant amount of external memory—the actual cursor in Wasm-land.
JavaScript engines distinguish between the memory space that contains JavaScript objects, called _internal memory_ or _heap space_, and the space used for Wasm components, called _external memory_. The Slang objects that you interact with reside in internal memory. However, a Slang object, such as a `Cursor`, holds a _handle_ to a Wasm component that manages a corresponding resource in the Rust implementation. In the case of a `Cursor`, that resource contains among other things _a reference to the entire parsing tree_. Therefore, an instance of the `Cursor` class in JavaScript is lightweight in terms of internal memory as it only contains the handle number. But it is associated with a significant amount of external memory—the actual cursor+tree in Wasm-land.


In our tests, adding this timeout was sufficient to give the `FinalizationRegistry` time to properly clean up the objects. The external memory usage grows at the start, but eventually stabilizes and remains steady.

In an interactive environment, such as a browser, it should not be necessary to add such a pause, as the engine has several opportunities to perform cleanup while waiting for user input.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
In an interactive environment, such as a browser, it should not be necessary to add such a pause, as the engine has several opportunities to perform cleanup while waiting for user input.
In an interactive environment, such as an IDE or a browser, it should not be necessary to add such a pause, as the engine has several opportunities to perform cleanup while waiting for user input.

Copy link
Contributor

@OmarTawfik OmarTawfik left a comment

Choose a reason for hiding this comment

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

Love this. Left a couple of suggestions.

@beta-ziliani beta-ziliani added this pull request to the merge queue Apr 10, 2025
Merged via the queue into NomicFoundation:main with commit 101a84c Apr 10, 2025
2 checks passed
@beta-ziliani beta-ziliani deleted the feat/memory-user-guide branch April 10, 2025 16:59
@OmarTawfik OmarTawfik linked an issue Apr 11, 2025 that may be closed by this pull request
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.

make sure jco resources are disposed
4 participants