Repository navigation
Bun.Transpiler: tell the GC its size, keep a heap only for object and array defines #47294
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
robobun
wants to merge
4
commits into
main
Choose a base branch
from
robobun/56ca1cba/transpiler-no-heap-per-instance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bcc1ba1
Bun.Transpiler: tell the GC how much memory an instance holds
robobun 94ebe6a
Bun.Transpiler: do not keep a mimalloc heap for each instance
robobun fbe86dd
Bun.Transpiler: keep string define values with the instance
robobun 367b8f4
OwnedValues: the unit test checks that a node owns its bytes
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| use core::ptr::NonNull; | ||
|
|
||
| use crate::expr::Data; | ||
| use crate::{E, StoreRef}; | ||
|
|
||
| /// Nodes and string bytes on the global heap, for values that outlive every arena and AST store. | ||
| #[derive(Default)] | ||
| pub struct OwnedValues { | ||
| bytes: Vec<Box<[u8]>>, | ||
| // The boxes are load-bearing: a `StoreRef` points into each, and a bare `Vec` moves its items when it grows. | ||
| #[expect(clippy::vec_box)] | ||
| strings: Vec<Box<E::EString>>, | ||
| } | ||
|
|
||
| /// The node keeps its address, and nothing writes to it through `list` before the drop. | ||
| fn keep<T>(list: &mut Vec<Box<T>>, node: T) -> StoreRef<T> { | ||
| list.push(Box::new(node)); | ||
| let node: &mut T = list.last_mut().expect("pushed above"); | ||
| StoreRef::from(NonNull::from(node)) | ||
| } | ||
|
|
||
| impl OwnedValues { | ||
| /// A string node with its own copy of `bytes`. | ||
| pub fn string(&mut self, bytes: &[u8]) -> Data { | ||
| self.bytes.push(bytes.into()); | ||
| let node = E::EString::init(self.bytes.last().expect("pushed above")); | ||
| Data::EString(keep(&mut self.strings, node)) | ||
| } | ||
|
|
||
| /// About how many bytes the values take. | ||
| pub fn memory_cost(&self) -> usize { | ||
| self.bytes.iter().map(|bytes| bytes.len()).sum::<usize>() | ||
| + self.bytes.capacity() * size_of::<Box<[u8]>>() | ||
| + self.strings.len() * size_of::<E::EString>() | ||
| + self.strings.capacity() * size_of::<Box<E::EString>>() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn a_string_stays_readable_while_more_are_added() { | ||
| let mut values = OwnedValues::default(); | ||
| let mut texts: Vec<Vec<u8>> = (0..65).map(|i| vec![b'a' + (i % 26) as u8; i]).collect(); | ||
| let expected = texts.clone(); | ||
| let nodes: Vec<Data> = texts.iter().map(|text| values.string(text)).collect(); | ||
| // A node that only borrowed its text would read these bytes. | ||
| for text in &mut texts { | ||
| text.fill(b'#'); | ||
| } | ||
| for (node, text) in nodes.iter().zip(&expected) { | ||
| let Data::EString(string) = node else { | ||
| panic!("not a string"); | ||
| }; | ||
| assert_eq!(string.slice8(), text.as_slice()); | ||
| } | ||
| // With a capacity above the length, a cost that counted the length would not match. | ||
| assert!(values.bytes.capacity() > nodes.len() && values.strings.capacity() > nodes.len()); | ||
| let bytes: usize = expected.iter().map(Vec::len).sum(); | ||
| let slots = values.bytes.capacity() * size_of::<Box<[u8]>>() | ||
| + values.strings.capacity() * size_of::<Box<E::EString>>(); | ||
| assert_eq!( | ||
| values.memory_cost(), | ||
| bytes + nodes.len() * size_of::<E::EString>() + slots | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.