Skip to content

Reduce Generic Parameter Proliferation with dyn Trait Objects #4309

@joostjager

Description

@joostjager

Core types have excessive generic parameters that harm maintainability:

  • ChannelManager - 10 type parameters
  • OnionMessenger - 9 type parameters
  • PeerManager - 8 type parameters

This makes certain refactors nearly impossible. Adding a new dependency to ChannelManager means threading a new generic through dozens of impl blocks, all their where clauses, and every downstream consumer. The friction discourages improvements that would otherwise be straightforward.

Performance concerns are overstated

The traits behind these parameters do I/O, networking, disk access, and cryptographic operations - all measured in microseconds to milliseconds. A vtable call costs ~1 nanosecond. The overhead is unmeasurable noise.

There are no hot paths here. Lightning nodes are I/O-bound, not CPU-bound. No one has ever profiled LDK and found vtable dispatch as a bottleneck - because it isn't one.

Precedent

DynSigner already exists in the codebase and demonstrates that dynamic dispatch works fine for signing operations - one of the more performance-sensitive areas.

Proposal

Start with ChannelManager and convert object-safe traits to dyn. Candidates:

  • BroadcasterInterface
  • FeeEstimator
  • Logger
  • EntropySource

Traits with associated types (Watch, SignerProvider) or generic methods (Router) would need more work to become object-safe.

Expected outcome

Significantly reduce ChannelManager's type parameter count. Make future refactors tractable. No measurable performance change.

(co-authored by Claude)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions