pnevyk / gryf

Graph data structure library aspiring to be convenient, versatile, correct and performant.
MIT License
73 stars 1 forks source link

Override default implementations by more efficient ones where it makes sense #6

Open pnevyk opened 2 years ago

pnevyk commented 2 years ago

The core traits Vertices{Base,Mut,}, Edges{Base,Mut,} and Neighbors contain default implementations for some methods that can be expressed in the means of others. These implementations are often inefficient, however, and should be overridden by reasonable implementations in each graph storage.

This applies both to graph storages (AdjList, AdjMatrix, EdgeList) and their adapters (Stable, Frozen, operators, ...). The latter should always explicitly delegate the behavior to the underlying graph (i.e., implementing fn <function>(...) { self.graph.<function>(...) } or equivalent), but this is often guaranteed because of using derive macros.

A few known examples:

pnevyk commented 2 months ago

Recommended approach to find the optimization opportunities:

  1. Find an impl Trait for Storage item, where Trait is any graph-related trait and Storage is AdjList, AdjMatrix, etc.
  2. Use your Rust Analyzer (or a different tool with equivalent functionality) to "implement default members", which inlines the default implementations to the concrete impl Trait for Storage.
  3. Knowing the internal representation of the storage at hand, determine whether the default implementation can be improved. If so, improve it. If not, delete it.