-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix bun --hot per-reload memory retention (DirEntry reuse, ref_strings balance) #36675
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
13
commits into
main
Choose a base branch
from
claude/f0581dc1/hot-reload-leak-regression
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.
+748
−205
Open
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
49b5de4
Fix bun --hot per-reload memory retention (resolver DirEntry reuse, C…
robobun 241e4f5
Address review: honor stale in entries_at_locked; drop RSS bound; tra…
robobun 4a37a8f
Do not clear JSC CodeCache on --hot reload
robobun 3692635
resolver: do not cache a handle the close-guard will close
robobun cf1ee3b
test: make refStringsNeverExceedCodeCache falsifiable for the refcoun…
robobun fee976d
resolver: hold entries_mutex across DirEntry.data reads in resolve()
robobun 85e82c0
resolver: drop entries_mutex before watcher.watch() in load_as_file
robobun 32b0f3a
resolver: remove adopted open_dir from open_dirs in dir_info_cached_miss
robobun 554a772
resolver: remove dead open_dirs cleanup in dir_info_cached_miss
robobun 07b7e99
Replace manual refcount and fd cleanup with RAII owners
robobun c695426
ci: retrigger
robobun feb261e
filesystem_router: make base_dir non-optional; resolver: seed cached …
robobun d9bf584
resolver: close a caller-transferred handle before adopting prev_fd
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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //! Per-size-bucket live-allocation histogram for the Rust global allocator. | ||
| //! | ||
| //! Only compiled when `cfg(bun_track_alloc)` is set (via `BUN_TRACK_ALLOC=1` | ||
| //! at build time). Wraps the real allocator and maintains `(bytes, count)` | ||
| //! counters per power-of-two size bucket, readable from JS via | ||
| //! `hotReloadDiagnostics().allocHistogram`. This surfaces reachable-but-growing | ||
| //! native memory that LSAN (unreachable-only) cannot see. | ||
|
robobun marked this conversation as resolved.
|
||
|
|
||
| use core::alloc::{GlobalAlloc, Layout}; | ||
| use core::sync::atomic::{AtomicI64, Ordering}; | ||
|
|
||
| const BUCKETS: usize = 32; | ||
| static LIVE_BYTES: [AtomicI64; BUCKETS] = [const { AtomicI64::new(0) }; BUCKETS]; | ||
| static LIVE_COUNT: [AtomicI64; BUCKETS] = [const { AtomicI64::new(0) }; BUCKETS]; | ||
|
|
||
| #[inline] | ||
| fn bucket(size: usize) -> usize { | ||
| let b = usize::BITS - size.max(1).leading_zeros(); | ||
| (b as usize).min(BUCKETS - 1) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn add(size: usize) { | ||
| let b = bucket(size); | ||
| LIVE_BYTES[b].fetch_add(size as i64, Ordering::Relaxed); | ||
| LIVE_COUNT[b].fetch_add(1, Ordering::Relaxed); | ||
| } | ||
|
|
||
| #[inline] | ||
| fn sub(size: usize) { | ||
| let b = bucket(size); | ||
| LIVE_BYTES[b].fetch_sub(size as i64, Ordering::Relaxed); | ||
| LIVE_COUNT[b].fetch_sub(1, Ordering::Relaxed); | ||
| } | ||
|
|
||
| pub(crate) struct Tracked<A: GlobalAlloc>(pub(crate) A); | ||
|
|
||
| unsafe impl<A: GlobalAlloc> GlobalAlloc for Tracked<A> { | ||
| unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
| let p = unsafe { self.0.alloc(layout) }; | ||
| if !p.is_null() { | ||
| add(layout.size()); | ||
| } | ||
| p | ||
| } | ||
| unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
| sub(layout.size()); | ||
| unsafe { self.0.dealloc(ptr, layout) } | ||
| } | ||
| unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | ||
| let p = unsafe { self.0.alloc_zeroed(layout) }; | ||
| if !p.is_null() { | ||
| add(layout.size()); | ||
| } | ||
| p | ||
| } | ||
| unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { | ||
| let p = unsafe { self.0.realloc(ptr, layout, new_size) }; | ||
| if !p.is_null() { | ||
| sub(layout.size()); | ||
| add(new_size); | ||
| } | ||
| p | ||
| } | ||
| } | ||
|
|
||
| /// Writes `BUCKETS` pairs of `(live_bytes, live_count)` into `out` (caller | ||
| /// provides `out_len` i64 slots). Returns the bucket count written. | ||
|
robobun marked this conversation as resolved.
|
||
| #[unsafe(no_mangle)] | ||
| pub(crate) extern "C" fn Bun__trackedAllocHistogram(out: *mut i64, out_len: usize) -> usize { | ||
| let n = BUCKETS.min(out_len / 2); | ||
| for i in 0..n { | ||
| // SAFETY: caller passes a buffer of `out_len` i64s. | ||
| unsafe { | ||
| *out.add(i * 2) = LIVE_BYTES[i].load(Ordering::Relaxed); | ||
| *out.add(i * 2 + 1) = LIVE_COUNT[i].load(Ordering::Relaxed); | ||
| } | ||
| } | ||
| n | ||
| } | ||
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
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.
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.