-
Notifications
You must be signed in to change notification settings - Fork 13
RFC: api redesign prototype and notes based on gc-arena #68
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
shruti2522
wants to merge
6
commits into
boa-dev:main
Choose a base branch
from
shruti2522:api-redesign-prototype
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
6 commits
Select commit
Hold shift + click to select a range
cf29ff4
RFC:add api redesign prototype based on gc-arena
shruti2522 f661622
refactor: split api_prototype.rs, added Array.prototype.push to prove…
shruti2522 2dca871
test: verify memory safety with Miri
shruti2522 142d613
add O(1) pinned roots, pool allocation and lifetime safety tests
shruti2522 8310612
feat(gc): implement intrusive_collections inspired RootLink
shruti2522 bcde269
place link before gc_ptr in Root<T>
shruti2522 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,169 @@ | ||
| # GC API Redesign Proposal | ||
|
|
||
| **Status**: RFC | ||
|
|
||
| ## Problem | ||
|
|
||
| Current `boa_gc` uses implicit rooting via `Clone`/`Drop` on `Gc<T>`. Every clone touches root counts, adding overhead in hot VM paths. It also needs `thread_local`, blocking `no_std`. | ||
|
|
||
| This proposes lifetime-branded `Gc<'gc, T>` for zero cost pointers and explicit `Root<T>` for persistence. | ||
|
|
||
| ## Core API | ||
|
|
||
| ### Gc Pointer | ||
|
|
||
| ```rust | ||
| pub struct Gc<'gc, T: Trace + ?Sized + 'gc> { | ||
| ptr: NonNull<GcBox<T>>, | ||
| _marker: PhantomData<(&'gc T, *const ())>, | ||
| } | ||
|
|
||
| impl<'gc, T: Trace + ?Sized + 'gc> Copy for Gc<'gc, T> {} | ||
| ``` | ||
|
|
||
| ### Mutability via GcRefCell | ||
| ```rust | ||
| pub struct GcRefCell<T: Trace> { | ||
| inner: RefCell<T>, | ||
| } | ||
| ``` | ||
| `GcRefCell` safely traces internal values statically behind a dynamically borrowed `RefCell`, providing `GcRef` and `GcRefMut` access similar to native `Rc/RefCell` combinations. Allows internal JavaScript arrays and objects to be mutated during the GC trace safely. | ||
|
|
||
| ### Weak Reference Separation | ||
| ```rust | ||
| pub struct WeakGc<T: Trace + ?Sized> { | ||
| ptr: NonNull<GcBox<T>>, | ||
| } | ||
|
|
||
| impl<T: Trace + ?Sized> WeakGc<T> { | ||
| pub fn upgrade<'gc>(&self, cx: &MutationContext<'gc>) -> Option<Gc<'gc, T>> { ... } | ||
| } | ||
| ``` | ||
| Weak references drop their tie to the single `'gc` lifetime. Instead, they are upgraded back into strong `Gc` pointers only when explicitly bound against an active safe `MutationContext<'gc>`. | ||
|
|
||
| The `'gc` lifetime ties the pointer to its collector. Copying is free, no root count manipulation. | ||
|
|
||
| ### Root for Persistence | ||
|
|
||
| ```rust | ||
| pub struct Root<T: Trace> { | ||
| link: RootLink, // Intrusive list node (prev/next only), at offset 0 so bare link* == Root* | ||
| gc_ptr: NonNull<GcBox<T>>, // T: Sized keeps this thin for type erased offset_of! | ||
| /// Cross collector misuse detection only, plays no role in unlinking. | ||
| collector_id: u64, | ||
| _marker: PhantomData<*const ()>, | ||
| } | ||
|
|
||
| impl<T: Trace> Root<T> { | ||
| pub fn get<'gc>(&self, cx: &MutationContext<'gc>) -> Gc<'gc, T> { | ||
| assert_eq!(self.collector_id, cx.collector.id); | ||
| // ... | ||
| } | ||
| } | ||
|
|
||
| impl<T: Trace> Drop for Root<T> { | ||
| fn drop(&mut self) { | ||
| // O(1) self unlink: splice prev/next together, no list reference needed | ||
| if self.link.is_linked() { | ||
| unsafe { | ||
| RootLink::unlink(NonNull::from(&self.link)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| `Root<T>` escapes the `'gc` lifetime. Returned as `Pin<Box<Root<T>>>` for stable addresses (required by the intrusive list). Stores `collector_id` to catch cross-collector misuse at runtime — it is **not** used during unlink; `Drop` only touches the embedded `prev`/`next` pointers. | ||
|
|
||
| **No `Rc` required.** A root only needs its own embedded `prev`/`next` pointers to remove itself from the list. The `Collector` owns a **sentinel** node; insertion and removal are pure pointer surgery with no allocation and no reference counting. | ||
|
|
||
| ### MutationContext | ||
|
|
||
| ```rust | ||
| pub struct MutationContext<'gc> { | ||
| collector: &'gc Collector, | ||
| } | ||
|
|
||
| impl<'gc> MutationContext<'gc> { | ||
| pub fn alloc<T: Trace>(&self, value: T) -> Gc<'gc, T> { ... } | ||
| pub fn root<T: Trace>(&self, gc: Gc<'gc, T>) -> Pin<Box<Root<T>>> { ... } | ||
| pub fn collect(&self) { ... } | ||
| } | ||
| ``` | ||
|
|
||
| Uses `&self` with `RefCell` inside for multiple concurrent allocations. | ||
|
|
||
| ### Sentinel Node & Root Traversal | ||
|
|
||
| The `Collector` owns one **pinned sentinel** `RootLink` (a bare link node with no payload): | ||
|
|
||
| ```text | ||
| Collector::sentinel -> root_a.link -> root_b.link -> root_c.link -> None | ||
| ``` | ||
|
|
||
| Roots insert themselves immediately after the sentinel via `RootLink::link_after`. During collection, `RootLink::iter_from_sentinel(sentinel)` starts from `sentinel.next`, so the sentinel itself is never yielded. For each link, `gc_ptr` is recovered via `offset_of!(Root<i32>, gc_ptr)` and used to mark the allocation. | ||
|
|
||
| ### Entry Point | ||
|
|
||
| ```rust | ||
| pub struct GcContext { | ||
| collector: Collector, | ||
| } | ||
|
|
||
| impl GcContext { | ||
| pub fn new() -> Self { ... } | ||
| pub fn mutate<R>(&self, f: impl for<'gc> FnOnce(&MutationContext<'gc>) -> R) -> R { ... } | ||
| } | ||
| ``` | ||
|
|
||
| By owning the `Collector`, `GcContext` defines the entire host timeline. The `for<'gc>` pattern from gc-arena creates a unique lifetime isolating active context mutations per arena. | ||
|
|
||
| ### Tracing Mechanism | ||
| ```rust | ||
| pub trait Trace { | ||
| fn trace(&mut self, tracer: &mut Tracer); | ||
| } | ||
|
|
||
| pub trait Finalize { | ||
| fn finalize(&self) {} | ||
| } | ||
| ``` | ||
| Note: `trace` takes `&mut self` instead of `&self`, ensuring that potential moving collectors have exclusive layout rights during traces. | ||
|
|
||
| ## vs Current Oscars | ||
|
|
||
| | | Current | Proposed | | ||
| |---|---------|----------| | ||
| | **Pointer** | `Gc<T>` | `Gc<'gc, T>` | | ||
| | **Lifetime** | `'static` + `extend_lifetime()` | `'gc` branded | | ||
| | **Rooting** | Implicit (inc/dec on clone/drop) | Explicit (`Root<T>`) | | ||
| | **Copy cost** | Cell write | Zero | | ||
| | **Drop cost** | TLS access (futex lock) | Zero (Copy type) | | ||
| | **Isolation** | Runtime only | Compile-time + runtime validation | | ||
|
|
||
| ## Why This Works | ||
|
|
||
| **no_std Compatible**: No `thread_local` needed. | ||
|
|
||
| **Performance**: `Gc` copying is just memcpy, no root count overhead. | ||
|
|
||
| **Allocation**: Uses `mempool3::PoolAllocator` with size-class pooling instead of individual `Box` allocations, avoiding fragmentation. | ||
|
|
||
| **Safety**: | ||
| - Cross-context caught at compile time for `Gc` | ||
| - Cross-collector caught at runtime for `Root` | ||
| - Explicit `!Send`/`!Sync` prevents threading bugs | ||
| - Intrusive sentinel based linked list for O(1) insertion and self-unlink | ||
| - `Root` holds **no `Rc`**, unlink is pure pointer surgery on embedded `prev`/`next` | ||
| - `Pin<Box<Root<T>>>` guarantees stable node addresses while linked | ||
|
|
||
| ## Open Questions | ||
|
|
||
| - FFI boundaries (native functions receiving `Gc` pointers) | ||
| - Migration path (thousands of `Gc<T>` uses in Boa) | ||
| - Real benchmark numbers | ||
|
|
||
| ## References | ||
|
|
||
| - gc-arena: https://github.com/kyren/gc-arena | ||
| - boa#2631: https://github.com/boa-dev/boa/issues/2631 | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also the drop cost is a required access to a TLS which uses a futex lock in most implementations. It's very expensive for the current GC. It would be free for the proposed one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, completely missed that. Added TLS futex drop cost to the table