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

inject_connected and inject_disconnected might introduce small inefficiencies in NetworkBehaviour implementers #2434

Closed
divagant-martian opened this issue Jan 13, 2022 · 6 comments

Comments

@divagant-martian
Copy link
Contributor

Checking the implemented protocols (gossipsub, kad, relay, req/resp and floodsub, mdns and identify) all but floodsub and mdns implement at least a pair of the coupled methods: inject_connection_(closed|establised) and the corresponding inject_(disconnected|connected) from the NetworkBehaviour trait.

This seems to be because the protocols require pieces of information that are not given together: i.e connection direction and whether this connection is the last/first. In the general case the means the protocol implementing the trait retrieves the same Peer_id twice for every first connection and final disconnection. This could be avoided removing inject_disconnected and inject_connected and simply passing the number of connections as a parameter. If a protocol cares to check for the conditions for disconnections/connections it would mean to move the check that the swarm already does about the number of connections to the protocol.

@mxinden
Copy link
Member

mxinden commented Jan 13, 2022

I don't think the fact that we have both inject_connection_(closed|establised) and inject_(disconnected|connected) is an inefficiency in a performance sense. I would argue that the cost of two function calls is negligible compared to the cost of establishing the connections themselves. Especially as inject_(disconnected|connected) involves a PeerId only, which should fit a single CPU cache line. Could you expand on which logic you would expect to be a hot-path?

I do agree that having both inject_connection_(closed|establised) and inject_(disconnected|connected) is non-intuitive from a user-experience perspective.

@divagant-martian
Copy link
Contributor Author

implementation wise, protocols typically store information in a HashMap. Two calls = two searches

@divagant-martian divagant-martian changed the title inject_connected and inject_disconnected introduce small inefficiencies inject_connected and inject_disconnected might introduce small inefficiencies in NetworkBehaviour implementors Jan 14, 2022
@divagant-martian divagant-martian changed the title inject_connected and inject_disconnected might introduce small inefficiencies in NetworkBehaviour implementors inject_connected and inject_disconnected might introduce small inefficiencies in NetworkBehaviour implementers Jan 14, 2022
@divagant-martian
Copy link
Contributor Author

divagant-martian commented Jan 14, 2022

fixed title to make the point more clear.

On one hand it may be a bit confusing the existence of both methods; but beyond this it also splits the logic required to handle new and closed connections/peers in a two step process in each protocol. Doing what protocols require to "unify" back this logic is what typically will produce a small inefficiency. I also see as an advantage to removing the inject_disconnected/connected methods we no longer needing to state the promise/contract about when is each method called with respect to the other. Some protocols actually have checks in place for this.

It also aligns nicely with SwarmEvent::ConnectionEstablished

@thomaseizinger
Copy link
Contributor

On one hand it may be a bit confusing the existence of both methods

This has historical reasons because originally, rust-libp2p did not support multiple connections to the same peer and hence only inject_connected and inject_disconnected were needed.

@mxinden
Copy link
Member

mxinden commented Jan 19, 2022

@divagant-martian do you want to propose a patch combining the four methods into two?

@divagant-martian
Copy link
Contributor Author

divagant-martian commented Jan 19, 2022

That's my suggestion. If you ask if I would be giving a PR for this, yes. But I'd like to first gather thoughts on the matter. I guess that's the reason for opening this issue

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

No branches or pull requests

3 participants