Repository navigation
FilterAdjacency: __len__ is recalculated unnecessarily #7377 - #7378
Conversation
|
Can you describe what impact this has on your projects or how this helps you? Storing information in two places is potentially problematic if one source changes. By caching the length value we need to ensure that the
|
|
I've edited my previous comment and the OP to show the link to the issue where a good description of the problem is contained. I will need to check for ways that this cached value might be out of sync with the underlying data. Another approach: |
|
Hi, I would like to avoid the copy approach. I deal with large graphs and apply multiple different views and dont want to worry about memory consumption. Also i optimized my filter functions a lot and their runtime is sufficient fast, also faster than copying. In my case NODE_OK is a function. |
|
Do you have any suggestions for how to make sure that an arbitrary user's functions are not resulting in a change in the length of the view? I don't see that we can guarantee that. And so caching the length may not be correct. As for copying the subgraph -- remember that it is only copying the subgraph, not the full graph. In terms of time, it should be faster if you end up looking at the edges more than once. In terms of memory, it will require an extra size of the subgraph -- not the size of the full graph. You might want to try it and see what the impact is. As another workaround, maybe you can store the length in a cache that you create (assuming that you know that your lengths will not change). If you don't want to store it on the subgraph object, could you store it somewhere else? |
|
For my case it would be enough to give the subgraph_view something like a cache_len: bool = False option. SG = G.subgraph(node_ok_func) this is also fine |
|
Is this a candidate for |
|
Here's another approach that might work. Since Users who define the length attribute are responsible for updating it when the set is a dynamic quantity. The length is stored on the object that defines the set of nodes (i.e., close to the data it describes). Thoughts? |
works for me! i am really impressed about the speed and the level of quality of your responses. Thank you again for that! |
|
Thanks! |
|
@dschult ready for re-review i dont think it is ok to use: if hasattr(self.NODE_OK, "nodes"):
return len(self.NODE_OK.nodes)otherwise the if this is not needed i also would update the |
|
I think the So, in the case with a To speed it up, change the last line of your change to do the iteration directly instead of having return sum(1 for n in self._atlas if self.NODE_OK(n))Thanks! |
|
At the moment in main the length would be: but in the new version if would be so if |
…workx into bugfix-for-issue-7377
|
@dschult review pls :) |
|
@dschult pls i could rly need this change |
dschult
left a comment
There was a problem hiding this comment.
I agree that we should not change functionality with this update.
Let's find a way to get the same answer as before -- but still check for a length attribute.
|
@dschult cant be removed even so it is unnecessary otherwise i think this is the final solution |
dschult
left a comment
There was a problem hiding this comment.
Right -- we don't want to change the __iter__ order.
So leave the __iter__ methods unchanged. Forming the intersection self.NODE_OK.nodes & self._atlas.keys() inside the iter method creates a whole new object with all values anyway and we want to avoid that if all we are doing is iterating.
We do want the intersection for the len computation because we know we will be looking at all values, so it's better than summing over the iterations in this case.
There is also a new blank line at line 5. If that just slipped in during other changes let's take it out. If linting checks are asking for it, leave it in. :)
Thanks!
|
Why are we changing the first part of the |
…workx into bugfix-for-issue-7377
|
only |
dschult
left a comment
There was a problem hiding this comment.
This change looks good to me. It'd be great to have more tests for views and filters, but I think that is a bigger issue than this PR.
I approve this PR
rossbar
left a comment
There was a problem hiding this comment.
This seems fine to me, but let's add a comment to the changes for future readers - I find the changes confusing even with the context of the discussion on GH!
|
I added some comments and also expanded t he docstring of the |
rossbar
left a comment
There was a problem hiding this comment.
Thanks @lejansenGitHub and @dschult !
…etworkx#7378) Optimize FilterAdjacency.__len__ with cached length Co-authored-by: Dan Schult <[email protected]>
len result is being stored for FilterAdjacency and FilterAtlas
Fixes #7377
Impacts of dynamic computation of len
The class FilterAdjacency is only used in views and therefore only works on frozen sets of edges and nodes. Despite that its len function is recalculated each time it is called instead of it being calculated and stored once upon its first call.
Current Behavior
len of FilterAdjacency is recalculated unnecessarily upon each call using up 80% of runtime for my application.
Expected Behavior
len of FilterAdjacency should calculated and stored once upon its first call. For all other calls the stored value is returned.
Steps to Reproduce
Any use of nx.subgraph_view shows this behaviour. Very poor performance shows in my case for number of nodes > 100.000.
Environment
Should be completely independent of env.
Python version: 3.11.8
NetworkX version: 3.2.1