Skip to content

perf(global): update one repo's slice by streaming, not by loading the whole store - #24

Merged
Azeem1985 merged 2 commits into
v8from
feat/streaming-global-add
Aug 25, 2026
Merged

Azeem1985 merged 2 commits into
v8from
feat/streaming-global-add

Conversation

@Azeem1985

Copy link
Copy Markdown

What changed

graphify global add rebuilt the entire global graph to change one repo in it: load global-graph.json into 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_add and 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.

 graphify/exporters/node_link_stream.py |  20 +-
 graphify/global_graph.py               | 245 ++++++++++++++++++----
 graphify/paths.py                      |  18 +-
 tests/test_global_graph_streaming.py   | 363 +++++++++++++++++++++++++++++++++
 4 files changed, 597 insertions(+), 49 deletions(-)

Why the slice is addressable

prefix_graph_for_global already stamps every node with repo and rewrites its id to <tag>::<local_id>. A repo's slice is therefore exactly "the nodes whose repo equals 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:

peak RSS wall
whole-load ~5.0 GB + ~0.9 GB swap 3m49s
streamed see PR comment see PR comment

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.py keeps the pre-change algorithm verbatim as an oracle and asserts the streamed output against it:

  • byte-identical output for the ordinary case (build a store, then update a repo in it);
  • graph-identical (nodes, links, all attributes) for the external-dedup case;
  • neighbouring repo slices hash identically across an update, and the updated one does not;
  • global_remove byte-identical to the whole-load remove;
  • empty store, and emptied store ("nodes": [] still parses);
  • bounded memory: tracemalloc peak 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 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.

…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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread graphify/global_graph.py
Comment on lines +228 to +232
# 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
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