ITensor / NamedGraphs.jl

Extension of `Graphs.jl` to graphs with named vertices.
MIT License
6 stars 3 forks source link

Make types of fields in `PartitionedGraph` more specific #63

Open mtfishman opened 6 months ago

mtfishman commented 6 months ago

The partitioned_vertices and which_partition fields of PartitionedGraph are currently abstract: https://github.com/mtfishman/NamedGraphs.jl/blob/d561823168be7d042fcfac31162c6c2495f10ba4/src/Graphs/partitionedgraphs/partitionedgraph.jl#L5-L6. To improve performance and type stability they should be parametrized, see https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-fields-with-abstract-type. @JoeyT1994

JoeyT1994 commented 6 months ago

So you mean like:

struct PartitionedGraph{V,PV,G<:AbstractGraph{V},PG<:AbstractGraph{PV}, PV<:AbstractDict, WP<:AbstractDict} <:
       AbstractPartitionedGraph{V,PV}
  graph::G
  partitioned_graph::PG
  partitioned_vertices::PV
  which_partition::WP
end

instead?

mtfishman commented 6 months ago

I was thinking like this:

struct PartitionedGraph{V,PV,G<:AbstractGraph{V},PG<:AbstractGraph{PV}} <:
       AbstractPartitionedGraph{V,PV}
  graph::G
  partitioned_graph::PG
  partitioned_vertices::Dictionary{PV,V}
  which_partition::Dictionary{V,PV}
end

which restricts the storage to be Dictionary but either style fixes the type stability issue. Basically the point is that you should try to expose the types of the fields as much as possible in the type parameters of PartitionedGraph (there are reasons for not doing that if you want the object to be more dynamic, like the ITensor type, but that is fairly uncommon and there isn't a good reason for doing that here).