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

[Merged by Bors] - Boa Gc implementation draft #2394

Closed
wants to merge 55 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
78032af
Early skeletal framework
nekevss Oct 21, 2022
dc892e6
Build out concept further
nekevss Oct 22, 2022
b2736ab
Complete initial work on alloc and collector
nekevss Oct 24, 2022
be81ba4
Ephemeron implementation and clean up
nekevss Oct 25, 2022
148fd7c
Couple more fixes
nekevss Oct 25, 2022
2eac402
Refactor Mark-Sweep
nekevss Oct 30, 2022
f2eefdf
Complete initial layout and refactor
nekevss Oct 31, 2022
295b01c
Build errors and first basic test
nekevss Nov 1, 2022
1109201
initial basic tests and some fixes
nekevss Nov 4, 2022
7f417ba
Promotions appear to work!
nekevss Nov 4, 2022
c4f5274
Promotion tests
nekevss Nov 4, 2022
727ea98
Transfer engine to gc and fixes
nekevss Nov 5, 2022
f95ad04
gc fixes and rustfmt
nekevss Nov 5, 2022
174b024
Addressing clippy lints with fixes or allow
nekevss Nov 5, 2022
585f5ee
Rustfmt
nekevss Nov 5, 2022
e40c40c
rebase and post rebase changes
nekevss Nov 5, 2022
e5f0066
Remove unneeded comment line
nekevss Nov 5, 2022
6339992
cargo.toml -> Cargo.toml
nekevss Nov 5, 2022
b20bc25
Changed back to drop_in_place on non-dump
nekevss Nov 5, 2022
61d09f1
Rustfmt
nekevss Nov 5, 2022
06b2c81
Rebase and fixes
nekevss Nov 5, 2022
cc97654
Root management fixes
nekevss Nov 6, 2022
bc66910
Revert changes to promotions from last commit
nekevss Nov 6, 2022
a53f67b
Rustfmt
nekevss Nov 6, 2022
3d3bac6
Adding boa focused tests
nekevss Nov 6, 2022
9ab82de
Removed divide in structs
nekevss Nov 6, 2022
196e963
Rebase and complete remove generational setup
nekevss Nov 7, 2022
602c3c1
Cleaning up remaining youth_heap variables
nekevss Nov 7, 2022
e56e3a5
Review changes
nekevss Nov 8, 2022
53ba41c
Missing newline in Cargo.toml
nekevss Nov 8, 2022
ae6cfdb
Redesign API
jedel1043 Nov 7, 2022
b7ae55b
Provide default for `is_marked_ephemeron`
jedel1043 Nov 8, 2022
bb5de24
Address errors from merge
nekevss Nov 9, 2022
b0732a4
Address reviews add root for ephemeron
nekevss Nov 9, 2022
0fe6c9a
Move tests to thread
jedel1043 Nov 9, 2022
e0df35c
Fix clippy lints
jedel1043 Nov 9, 2022
2f8c92e
Add safety lints and document some unsafe
jedel1043 Nov 9, 2022
336ff58
Document some unsafe pt. 2
jedel1043 Nov 9, 2022
6e00c4a
Safety documentation and basic allocation and dump test
nekevss Nov 10, 2022
4de6a58
Fix doc error
nekevss Nov 10, 2022
8d629be
Updated Trace docs and comments
nekevss Nov 10, 2022
36e6aa8
Safety doc on GcCell's Trace implementation
nekevss Nov 10, 2022
ec25eac
Add test and check eph drop call is not from Collector
nekevss Nov 10, 2022
465c8bd
Fmt and clippy lints on GcCell Trace methods
nekevss Nov 10, 2022
37d263f
Upgrade and clone methods with more tests
nekevss Nov 11, 2022
5ffe1b3
cargo fmt
nekevss Nov 11, 2022
51d0ca5
Removed print, oops
nekevss Nov 11, 2022
a68e5b5
Review updates - debug for GcBoxHeader, more documentation
nekevss Nov 11, 2022
4603cce
Remove `gc` from `boa_examples`
jedel1043 Nov 11, 2022
f85f746
Address review and new clippy lints
nekevss Nov 13, 2022
2ca8320
Forgot derive on Allocator
nekevss Nov 13, 2022
3d5005d
Remove use_self allows
nekevss Nov 13, 2022
973d117
Missed one
nekevss Nov 13, 2022
10d5d26
Add inline and docs to BorrowFlag & other review changes
nekevss Nov 14, 2022
7b477b6
Rustfmt
nekevss Nov 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add inline and docs to BorrowFlag & other review changes
  • Loading branch information
nekevss committed Nov 14, 2022
commit 10d5d26976eb78d27c1d55ef0be2cd0ce0527664
41 changes: 40 additions & 1 deletion boa_gc/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ use std::ops::{Deref, DerefMut};

use crate::trace::{Finalize, Trace};

/// `BorrowFlag` represent the internal state of a `GcCell` and
/// keeps track of the amount of current borrows.
#[derive(Copy, Clone)]
pub(crate) struct BorrowFlag(usize);
Razican marked this conversation as resolved.
Show resolved Hide resolved

/// `BorrowState` represents the various states of a `BorrowFlag`
///
/// - Reading: the value is currently being read/borrowed.
/// - Writing: the value is currently being written/borrowed mutably.
/// - Unused: the value is currently unrooted.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum BorrowState {
Reading,
Expand All @@ -22,9 +29,11 @@ const WRITING: usize = !1;
const UNUSED: usize = 0;

/// The base borrowflag init is rooted, and has no outstanding borrows.
pub(crate) const BORROWFLAG_INIT: BorrowFlag = BorrowFlag(1);
pub(crate) const BORROWFLAG_INIT: BorrowFlag = BorrowFlag(ROOT);

impl BorrowFlag {
Copy link
Member

Choose a reason for hiding this comment

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

I think all functions in this impl would benefit from an #[inline] hint. I would also appreciate some documentation.

/// Check the current `BorrowState` of `BorrowFlag`.
#[inline]
pub(crate) fn borrowed(self) -> BorrowState {
match self.0 & !ROOT {
UNUSED => BorrowState::Unused,
Expand All @@ -33,20 +42,32 @@ impl BorrowFlag {
}
}

/// Check whether the borrow bit is flagged.
#[inline]
pub(crate) fn rooted(self) -> bool {
self.0 & ROOT > 0
}

/// Set the `BorrowFlag`'s state to writing.
#[inline]
pub(crate) fn set_writing(self) -> Self {
// Set every bit other than the root bit, which is preserved
Self(self.0 | WRITING)
}

/// Remove the root flag on `BorrowFlag`
#[inline]
pub(crate) fn set_unused(self) -> Self {
// Clear every bit other than the root bit, which is preserved
Self(self.0 & ROOT)
}

/// Increments the counter for a new borrow.
///
/// # Panic
/// - This method will panic if the current `BorrowState` is writing.
/// - This method will panic after incrementing if the borrow count overflows.
#[inline]
pub(crate) fn add_reading(self) -> Self {
assert!(self.borrowed() != BorrowState::Writing);
// Add 1 to the integer starting at the second binary digit. As our
Expand All @@ -64,6 +85,11 @@ impl BorrowFlag {
flags
}

/// Decrements the counter to remove a borrow.
///
/// # Panic
/// - This method will panic if the current `BorrowState` is not reading.
#[inline]
pub(crate) fn sub_reading(self) -> Self {
assert!(self.borrowed() == BorrowState::Reading);
// Subtract 1 from the integer starting at the second binary digit. As
Expand All @@ -75,12 +101,23 @@ impl BorrowFlag {
Self(self.0 - 0b10)
}

/// Set the root flag on the `BorrowFlag`.
#[inline]
pub(crate) fn set_rooted(self, rooted: bool) -> Self {
// Preserve the non-root bits
Self((self.0 & !ROOT) | (usize::from(rooted)))
}
}

impl Debug for BorrowFlag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowFlag")
.field("Rooted", &self.rooted())
.field("State", &self.borrowed())
.finish()
}
}

/// A mutable memory location with dynamically checked borrow rules
/// that can be used inside of a garbage-collected pointer.
///
Expand Down Expand Up @@ -544,10 +581,12 @@ impl<T: Trace + ?Sized + Debug> Debug for GcCell<T> {
match self.flags.get().borrowed() {
BorrowState::Unused | BorrowState::Reading => f
.debug_struct("GcCell")
.field("flags", &self.flags.get())
.field("value", &self.borrow())
.finish(),
BorrowState::Writing => f
.debug_struct("GcCell")
.field("flags", &self.flags.get())
.field("value", &"<borrowed>")
.finish(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ impl<K: Trace + ?Sized, V: Trace + ?Sized> EphemeronBox<K, V> {
}

/// Calls [`Trace::weak_trace()`][crate::Trace] on value
///
#[inline]
fn weak_trace_value(&self) {
// SAFETY: Value is a sized element that must implement trace. The
Expand Down Expand Up @@ -135,6 +134,11 @@ unsafe impl<K: Trace + ?Sized, V: Trace + ?Sized> Trace for EphemeronBox<K, V> {
#[inline]
unsafe fn unroot(&self) {}

// An `EphemeronBox`'s key is set to None once it has been finalized.
//
// NOTE: while it is possible for the `key`'s pointer value to be
// resurrected, we should still consider the finalize the ephemeron
// box and set the `key` to None.
#[inline]
fn run_finalizer(&self) {
Finalize::finalize(self);
Expand Down
4 changes: 2 additions & 2 deletions boa_gc/src/internals/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod eph_box;
pub(crate) use eph_box::EphemeronBox;
mod ephemeron_box;
pub(crate) use ephemeron_box::EphemeronBox;

mod gc_box;
pub(crate) use gc_box::GcBox;