Repository navigation
perf(global): update one repo's slice by streaming, not by loading the whole store - #24
Conversation
…e whole store `graphify global add` rebuilt the entire global graph to change one repo in it. It loaded global-graph.json into NetworkX, pruned the repo, merged the new slice, and wrote everything back. Memory therefore scaled with the SIZE OF THE STORE rather than the SIZE OF THE CHANGE. Measured on a real 226-repo, 1.21M-node, 2.88M-link, 1.888GB global graph, refreshing one 26,668-node repo (2.2% of the nodes): peak ~5.0GB RSS plus ~0.9GB swap, 3m49s. On a 7.6GB box that is an OOM kill; adding a memory guard to stop the kills just converts the crash into a store that silently stops being refreshed. The slice is addressable. `prefix_graph_for_global` already stamps every node with `repo` and rewrites its id to `<tag>::<local_id>`, so a repo's slice is exactly "the nodes whose repo equals the tag, plus the links incident to them". That makes this a streaming edit, and the maker already wrote the reader for this shape: `exporters/node_link_stream` (built so a DB push could walk a 1.9GB graph in ~50MB). `global_add`/`global_remove` now stream the existing file element by element, copy every other repo's nodes and links through untouched, drop the target repo's, append the new slice, and atomically replace. Peak memory becomes O(changed repo + the global external-label index). Correctness first: the output is byte-for-byte what json.dump(node_link_data(G), indent=2) wrote before, and the new tests check that against the pre-change algorithm kept verbatim as an oracle. All of the whole-load path's semantics are reproduced, including the external-library dedup that rewires an incoming edge onto an existing global node, the self-loop drop that rewiring can cause, and add_edge's first-position/last-attributes collapse of duplicates. The one deliberate difference: when an incoming edge is rewired onto a pre-existing external node, the streamed file emits it at the end of the links array instead of inside that node's adjacency block. Same graph, same attributes, different array position — asserted as graph equality rather than byte equality for that case. Also here, both in service of the above: - node_link_stream: the scan now also records `directed` and the graph attrs. A push ignores them; a rewriter has to reproduce the header verbatim, and reading them is free because the scan already walks every top-level key. - paths._atomic_replace: opt-in `fsync=`. Off by default because the usual callers write tens of thousands of small exporter files per run, where a per-file device flush is pure cost. The global graph — gigabytes, minutes to rebuild, one copy — pays it. The graph-size cap still guards the source graph, which is genuinely loaded whole. It deliberately no longer gates the global file on this path: nothing materializes it any more, so capping it would only cap growth for no reason.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0668354cf4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| # A rewired incoming link can land on an edge that already | ||
| # exists between two surviving nodes; ``add_edge`` overwrote its | ||
| # attributes, so the old copy must not be emitted twice. | ||
| if _pair(source, target) in merged_links: | ||
| continue |
There was a problem hiding this comment.
Merge surviving attributes before replacing an edge
When an incoming edge is remapped onto an edge already connecting two surviving external nodes, the previous implementation's G.add_edge(..., **data) updated the existing NetworkX attribute dictionary, preserving any old attributes not supplied by the incoming edge. This branch instead discards the old link and later emits only the incoming dictionary, so fields such as weights or provenance disappear after global add; merge the surviving link's attributes into the replacement before skipping it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed and fixed in 835a25a — the reference oracle in the same PR uses G.add_edge(u, v, **edata), which is exactly this update-not-replace semantics, so this was a real deviation from the parity the PR claims. Merged the surviving link's attributes under the incoming ones (incoming wins on key conflicts, matching dict.update) before dropping the old copy. Added test_streamed_add_merges_attributes_on_rewired_external_edge, which builds two repos sharing two external nodes connected by an edge, verified it fails on the pre-fix code (weight/provenance silently dropped) and passes after. Full suite (9 tests) green.
add_edge(u, v, **attr) updates the existing edge dict rather than replacing it, so when a rewired incoming edge lands on a pair already connecting two surviving external nodes, attributes the old edge has and the incoming edge doesn't must survive. The streamed rewrite was dropping the old edge outright and emitting only the incoming one. Codex P2 on PR #24.
What changed
graphify global addrebuilt the entire global graph to change one repo in it: loadglobal-graph.jsoninto NetworkX, prune the repo, merge the new slice, write everything back. Memory scaled with the size of the store, not the size of the change.global_addandglobal_removenow stream the existing file element by element, copy every other repo's nodes and links through untouched, drop the target repo's, append the new slice, and atomically replace.Why the slice is addressable
prefix_graph_for_globalalready stamps every node withrepoand rewrites its id to<tag>::<local_id>. A repo's slice is therefore exactly "the nodes whoserepoequals the tag, plus the links incident to them" — no separate index needed, and no change to the file format.And the reader for this shape already existed:
exporters/node_link_stream, written so a DB push could walk a 1.9GB graph in ~50MB. This reuses it.Measured, on a real store
226 repos, 1,211,189 nodes, 2,884,473 links, 1.888GB. Refreshing one 26,668-node repo — 2.2% of the nodes:
On a 7.6GB box the old path was OOM-killed twice. Adding a memory guard to stop the kills just converted the crash into a store that silently stopped being refreshed.
Correctness
Correctness first, memory second.
tests/test_global_graph_streaming.pykeeps the pre-change algorithm verbatim as an oracle and asserts the streamed output against it:global_removebyte-identical to the whole-load remove;"nodes": []still parses);tracemallocpeak stays under a quarter of the store's file size while updating a 40-node repo inside a ~24,000-node store.All of the whole-load path's semantics are reproduced, including the external-library dedup that rewires an incoming edge onto an existing global node, the self-loop drop that rewiring can cause, and
add_edge's first-position/last-attributes collapse of duplicates.One deliberate difference: when an incoming edge is rewired onto a pre-existing external node, the streamed file emits it at the end of the links array instead of inside that node's adjacency block. Same graph, same attributes, different array position — hence graph equality rather than byte equality for that case, asserted explicitly.
Also here
node_link_stream: the scan now also recordsdirectedand the graph attrs. A push ignores them; a rewriter has to reproduce the header verbatim, and reading them is free because the scan already walks every top-level key.paths._atomic_replace: opt-infsync=. Off by default because the usual callers write tens of thousands of small exporter files per run, where a per-file device flush is pure cost. The global graph — gigabytes, minutes to rebuild, one copy — pays it.