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

refactor(gossipsub): don't store messages within Arc #3243

Merged
merged 8 commits into from
Dec 20, 2022

Conversation

thomaseizinger
Copy link
Contributor

@thomaseizinger thomaseizinger commented Dec 14, 2022

Description

Currently, we store messages to be sent to the ConnectionHandler in an Arc. However, we never actually clone these messages as we can see with this patch, hence we remove this wrapping.

Related: #3242

Notes

@AgeManning I think we talked about this at some point but I forget the reasoning for why this is done. Can you recall it?

Links to any relevant issues

Open Questions

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

@AgeManning
Copy link
Contributor

So originally it came from here: #898 (comment)

I think the idea was that we were allocating GossipsubRpc messages for every peer we needed to send them to. The Arc was to avoid the allocation.

Looking at the current code it does seem like we are just cloning the raw GossipsubRpc. I believe at one point we were just cloning the Arc. See for example here: https://github.com/libp2p/rust-libp2p/blob/master/protocols/gossipsub/src/behaviour.rs#L539

I think somewhere along the line it has changed. The send_message() probably took an Arc and we cloned the Arc. Looks like now we are just allocating and the Arc is pointless.

I'm indifferent if we want to keep the Arc to avoid the allocation or just remove it all together. I think this has come up quite a few times in the past, so its probably better to remove it so we don't forget again in the future.

@thomaseizinger
Copy link
Contributor Author

Thanks for digging up the conversation! I tried to check whether we are cloning the message some where by removing Clone from the event: https://github.com/libp2p/rust-libp2p/pull/3243/files#diff-4656833730e135d1bfaa19578445814deab8bbb2a315a526546810003aebf6a1L67

It compiled without errors which puzzled me initially but it makes sense if we are cloning the entire RPC message. I think I have an idea how to implement this in a more obvious way that isn't accidentally refactored away as easily.

In case we are sending the same message to a lot of peers, this should
reduce the memory footprint as we are only storing a pointer to the
message until we actually hand it to the connection handler.
This reverts commit 87b15be.
@thomaseizinger
Copy link
Contributor Author

I opted to revert the commits again. The tests were broken because they only check the events buffer and don't actually poll the behaviour. I figured it is not worth revising all these tests. Given that the optimisation was already "refactored away" in a previous iteration, this PR doesn't actually make anything worse other than removing the Arc and that is what I want / need / fell out of #3242.

@AgeManning
Copy link
Contributor

Yeah sounds good to me.

The optimization was never benched and its dubious as to how much it is saving us.

@thomaseizinger thomaseizinger requested a review from jxs December 16, 2022 04:50
@thomaseizinger
Copy link
Contributor Author

CI is broken because of #3252.

@thomaseizinger
Copy link
Contributor Author

Friendly ping @mxinden @jxs :)

Copy link
Member

@jxs jxs left a comment

Choose a reason for hiding this comment

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

LGTM!

@mergify mergify bot merged commit 88fa8e6 into master Dec 20, 2022
@thomaseizinger thomaseizinger deleted the refactor-gossipsub/remove-arc-event branch February 24, 2023 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants