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

GossipScore refactor #247

Merged

Conversation

Nashatyrev
Copy link
Collaborator

GossipScore consumes significant CPU time under high network load.
This is initial refactoring before optimizations

  • Make GossipRouter.score property overridable
  • Extract 2 interfaces GossipRouterEventListener and GossipScore. Rename implementation to DefaultGossipScore
  • Make them deal with PeerId instead of PeerHandler
  • To retain the possibility to access PeerHandler add P2PService:.peerIdToPeerHandlerMap
  • P2PService: hide mutable collections, expose immutable

- extract 2 interfaces GossipRouterEventListener and GossipScore
- Make GossipScore to operate with PeerId instead of PeerHandler
…erHandler

- P2PService: hide mutable collections, expose immutable
- P2PService: add peerIdToPeerHandlerMap to have optimized access to PeerHandler by its PeerId
Copy link
Contributor

@ajsutton ajsutton left a comment

Choose a reason for hiding this comment

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

LGTM.

Comment on lines +59 to +63
open val score: GossipScore by lazy {
DefaultGossipScore(scoreParams, executor, curTimeMillis).also {
eventBroadcaster.listeners += it
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm guessing there's some fun kotlin magic that means this works even though eventBroadcaster is only defined and initialised after this?

It would probably make me feel better to just move the val eventBroadcaster = GossipRouterEventBroadcaster() line above this one though. :) I'm a simple Java programmer...

Copy link
Collaborator Author

@Nashatyrev Nashatyrev Jul 15, 2022

Choose a reason for hiding this comment

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

It works just because the score in lazy initialized: the code inside lazy block is executed on the first score usage
TBH I was trying to invent 2-in-1 'builder+class' pattern with Kotlin but it's far from what it's intended to be and tends to be spaghetti-code pattern

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Was trying to refactor that quickly with the classic Builder pattern but seemed to open a can of worms so it's definitely worth a separate PR

Copy link
Contributor

Choose a reason for hiding this comment

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

ah right - I hadn't spotted the lazy there at all, mostly because it's a kotlin feature so I'm just not used to it. I'm fine leaving it as-is but sounds like you're not happy with the pattern anyway so go with what you think.

@Nashatyrev Nashatyrev merged commit 6bde649 into libp2p:develop Jul 15, 2022
@Nashatyrev Nashatyrev deleted the optimization/gossip-score-refactor branch July 18, 2022 16:59
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.

None yet

2 participants