Skip to content

75 should_run base - #174

Open
eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:75-should-run-base
Open

eriknw wants to merge 1 commit into
python-graphblas:20-wiener-efficiencyfrom
eriknw:75-should-run-base

Conversation

@eriknw

@eriknw eriknw commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Add Dispatcher.should_run, which asks a wrapper's own rule (set with nxapi._utils.should_run) as can_run does, and two helpers for rules that read what NetworkX caches on a graph. No name gets a rule here; each comes with the change that needs it. NetworkX asks should_run only where it would convert a NetworkX graph for a call that names no backend, after can_run said yes, so a rule there moves only that call to NetworkX: a backend graph, and a call with backend="graphblas", are served whatever it says. The code is the code #151 and #153 each add, byte for byte, so both drop their copies when restacked on this; a merge of this with each of #151, #153 and #165 is clean.

  • remembered(graph, key, compute). A rule that reads a fact from the NetworkX graph before every call reads it once per version of the graph. The fact is kept in the graph's __networkx_cache__, which NetworkX empties when the graph changes through its methods, and which the caller clears after an edit in place, as NetworkX asks for its own conversions. Nothing is kept while nx.config.cache_converted_graphs is off, when an edit in place needs no clearing; in NetworkX's test mode, which converts afresh for each call and so lets a test edit data in place between calls; or for a view, since NetworkX does not empty a view's cache when the graph under it changes.
  • The rebuild verdict in can_run, for the names whose result keeps the input's data (k_truss, the k-core names, ego_graph, reverse and the operators), now goes through remembered. It used to be kept on views too. A subgraph view converted before its graph gained a second edge attribute was then answered from the stale conversion: k_truss gave the edge (0, 1) as {'weight': 1.0} where NetworkX gives {'weight': 1.0, 'colour': 'red'}. It is now declined, and NetworkX answers.
  • has_reusable_conversion(name, bound, graph_name). Whether NetworkX will give this call a cached conversion of the graph. The cache key is the one NetworkX's own conversion code makes for the call, run on a stand-in that records the key instead of converting, and the search is NetworkX's own, on a copy of the cache, since the search may write to it. It is False while caching is off; for a view (anything holding its graph as _graph), since NetworkX keeps a view's cache, conversions included, when the graph under it changes, so a rule that serves because a conversion is cached would serve the view from a stale one; in NetworkX's test mode, which converts afresh for a call that names no backend; and if a private name it reads from NetworkX has gone or changed shape: every such read is inside one guard, so a later NetworkX gives the answer that claims nothing rather than an error in the user's call. A rule that claims a call because a conversion is cached, or that reads a fact from one, should ask this: a conversion cached for other attributes, or one NetworkX will not use for this call, says nothing about it. Nothing calls it yet.
  • Tests. should_run is asked on the automatic path alone (a spy on it and on convert_from_nx: none for a backend graph, a conversion and no should_run for backend="graphblas"); a declining rule moves only the automatic call; a hook gets the arguments by name with NetworkX's defaults, and none acts on arguments that do not bind. A rule reads its graph once per version, again after a mutation or a cleared cache, and on every call with caching off; after an edit in place to one weight, the answer is NetworkX's both with caching off and with caching on and the cache cleared. Views are read afresh, and so is every fact in NetworkX's test mode; a view's cached conversion (of a subgraph, of a digraph reversed, and of a digraph as undirected) is not reusable, before or after its graph changes, where its graph's is. With each private name the helpers read removed or replaced by one of another shape (the registry, the cache search, the key, the failure marker, the test-mode flag, the conversion code), has_reusable_conversion says False and remembered computes afresh. has_reusable_conversion is checked against NetworkX's own cache search for every dispatched name, after five primers (none, a weighted, an unweighted and another attribute's conversion, and the call itself) and with weight as declared, None and another attribute. It agrees with NetworkX on all 1115 calls in dev and 3.7 and all 1095 in 3.6.1, of which NetworkX reused a conversion in 665 and 657. 24 mutants each fail these tests in dev, 3.7 and 3.6.1, among them should_run dropping the rule's reason or acting on arguments that do not bind, facts kept with caching off, in test mode, on views or outside the cache, a test mode that cannot be read taken as off, a guard that catches only some errors, and a cache read keyed by weight alone, by an exact key, or claimed with caching off, in test mode, for a view, or for any view but a subgraph.
  • NetworkX's tests through the backend, the whole suite in dev, 3.7 and 3.6.1: the same tests pass, skip and xfail before and after (none of 10343, 9930 and 6895 differs), with about 290000 conversions in each. The counts of calls served and declined differ only in tests whose counts also differ between two runs of one tree (the traveling salesman ascent, test_kcomponents, test_lukes and the double-edge swap and small-world tests, which draw on unseeded or shared random state), and by one call on a MultiDiGraph in 3.6.1, which both trees decline before any rule this changes.

Stacked on #121.

should_run asks a wrapper's own rule, set with nxapi._utils.should_run,
as can_run does. NetworkX asks it only on the automatic path, so a rule
there moves a call to NetworkX without declining a backend graph or a
call that names this backend.

A rule reads a fact from the NetworkX graph through remembered, which
keeps it in the graph's __networkx_cache__ until NetworkX empties it.
It keeps nothing while cache_converted_graphs is off or in NetworkX's
test mode, where an edit in place needs no clearing, or for a view,
whose cache NetworkX keeps when the graph under it changes. The rebuild
verdict in can_run now goes through it: a view converted before its
graph gained a second edge attribute was answered from the stale
conversion without that attribute, and is now left to NetworkX.

has_reusable_conversion says whether NetworkX will give a call a cached
conversion, from the key NetworkX's own conversion code makes for that
call. It says no for a view, whose cached conversion may be stale; in
NetworkX's test mode, which converts afresh; and if the private names
it reads have changed.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.71429% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.07%. Comparing base (5b54ace) to head (7933efc).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           20-wiener-efficiency     #174      +/-   ##
========================================================
+ Coverage                 92.88%   93.07%   +0.19%     
========================================================
  Files                       182      184       +2     
  Lines                     11227    11568     +341     
  Branches                   1274     1290      +16     
========================================================
+ Hits                      10428    10767     +339     
- Misses                      546      549       +3     
+ Partials                    253      252       -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant