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

FnMut debug handler #1696

Merged
merged 4 commits into from
May 30, 2023
Merged

FnMut debug handler #1696

merged 4 commits into from
May 30, 2023

Conversation

chipshort
Copy link
Collaborator

@chipshort chipshort commented May 26, 2023

closes #1673

Not exactly sure why, but I had to remove the lifetime from DebugInfo to get this to work (otherwise I always get lifetime errors when adding the RefCell).

Copy link
Contributor

@maurolacy maurolacy left a comment

Choose a reason for hiding this comment

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

LGTM.

// Test with
// cargo integration-test debug_timing -- --nocapture
#[test]
fn debug_timing() {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏼 Nice tests.

@maurolacy
Copy link
Contributor

maurolacy commented May 29, 2023

Not exactly sure why, but I had to remove the lifetime from DebugInfo to get this to work (otherwise I always get lifetime errors when adding the RefCell).

Perhaps related to the removed comment about accessing referenced data in the debug implementation without cloning it?

@@ -447,7 +447,7 @@ pub fn do_debug<A: BackendApi + 'static, S: Storage + 'static, Q: Querier + 'sta
let message_data = read_region(&data.memory(&mut store), message_ptr, MAX_LENGTH_DEBUG)?;
let msg = String::from_utf8_lossy(&message_data);
let gas_remaining = data.get_gas_left(&mut store);
(*debug_handler)(
debug_handler.borrow_mut()(
Copy link
Member

Choose a reason for hiding this comment

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

Can the same be achieved with https://doc.rust-lang.org/std/rc/struct.Rc.html#method.get_mut directly? The Rc<RefCell< feels redundant. Not sure, but would one of the two work?

    # either
    debug_handler: Option<Rc<DebugHandlerFn>>,
    
    # or
    debug_handler: Option<RefCell<DebugHandlerFn>>,

But I might be wrong about this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

After double-checking, I believe we need some kind of pointer around the RefCell, because otherwise the type is unsized (internally it contains a Cell, which directly contains the DebugHandlerFn, which has no fixed size).
RefCell also cannot be cloned (at least without cloning the contained thing).

We also cannot use the Rc::get_mut function, because that returns None if we have multiple Rcs of the debug handler. We always keep one Rc in the context data and clone it in order to use it in this line, so we always have multiple Rcs here. Since this would allow holding an immutable and mutable borrow at the same time.

Rc<RefCell<_>> is apparently also a common pattern

Copy link
Member

Choose a reason for hiding this comment

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

We also cannot use the Rc::get_mut function, because that returns None if we have multiple Rcs of the debug handler. We always keep one Rc in the context data and clone it in order to use it in this line, so we always have multiple Rcs here. Since this would allow holding an immutable and mutable borrow at the same time.

Oh, right, excllent. To rephrase what you are saying, the reference count must be 1 in order to get mutable access. This is what the code of Rc::get_mut checks:

    fn is_unique(this: &Self) -> bool {
        Rc::weak_count(this) == 0 && Rc::strong_count(this) == 1
    }

Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Great stuff

@webmaster128 webmaster128 merged commit 6a5c3bc into main May 30, 2023
@webmaster128 webmaster128 deleted the 1673-mut-debughandler branch May 30, 2023 15:37
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.

Change Fn to FnMut in debug handler
3 participants