Skip to content

Cache edges, degree, adj properties of Graph classes - #5614

Merged
stefanv merged 2 commits into
networkx:mainfrom
dschult:cache-nodes-property
May 12, 2022
Merged

stefanv merged 2 commits into
networkx:mainfrom
dschult:cache-nodes-property

Conversation

@dschult

@dschult dschult commented May 8, 2022

Copy link
Copy Markdown
Member

Use cached_property for caching the view properties of graph classes (not G.name which can be changed and thus should be a regular non-cached property).

The changed properties include: G.edges, G.degree, G.adj (and G.in_edges, G.out_edges, G.in_degree, G.out_degree, G.pred, G.succ). Added tests that multiple calls return the same object (caching is successful).

Changes to Graph, DiGraph, MultiGraph, MultiDiGraph and AntiGraph. Also reportviews.py had to change one __setstate__ function.

@dschult dschult added this to the networkx-2.8.1 milestone May 8, 2022
@dschult
dschult requested review from jarrodmillman and stefanv May 8, 2022 06:59
@stefanv

stefanv commented May 11, 2022

Copy link
Copy Markdown
Contributor

@dschult Just double checking: I assumed when I made my original PR that the AdjacencyViews update themselves dynamically (they are called views, after all!). Is that assumption correct?

@dschult

dschult commented May 11, 2022

Copy link
Copy Markdown
Member Author

Yes, that is correct. The Views point to the graph data structures directly to report their information. That means you are safe unless you e.g. replace the dict G._adj. Then you have to clear the cached_property. Similarly, if someone changes G._node, they should delattr(self, "nodes") to reset the cached_property.

@stefanv
stefanv merged commit f50fc70 into networkx:main May 12, 2022
@stefanv

stefanv commented May 12, 2022

Copy link
Copy Markdown
Contributor

Thanks @dschult!

MridulS added a commit to MridulS/networkx that referenced this pull request Jun 18, 2022
@dschult
dschult deleted the cache-nodes-property branch November 25, 2022 15:42
MridulS pushed a commit to MridulS/networkx that referenced this pull request Feb 4, 2023
* Make all graph properties cached properties
* one test function is not needed due to test inheritance
cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
* Make all graph properties cached properties
* one test function is not needed due to test inheritance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants