Skip to content

perf(export): avoid duplicate read and JSON parse of existing graph.json in to_json - #5646

Closed
Gaurav-meena95 wants to merge 2 commits into
Graphify-Labs:v8from
Gaurav-meena95:perf/export-to-json-single-read
Closed

Gaurav-meena95 wants to merge 2 commits into
Graphify-Labs:v8from
Gaurav-meena95:perf/export-to-json-single-read

Conversation

@Gaurav-meena95

Copy link
Copy Markdown
Contributor

What does this PR do?

Eliminates the duplicate disk read and json.loads() parse of an existing graph.json in to_json() (graphify/export.py) when force=False and G.graph has no hyperedges key.

Problem

When overwriting an existing graph.json with force=False and no "hyperedges" key on G.graph, to_json() read and parsed the entire graph.json file twice:

  1. Once in the shrink guard (existing_data = json.loads(raw)).
  2. A second time in the hyperedge-wipeout check (_prev = json.loads(existing_path.read_text(...))).

Details

Target Previous Updated Rationale
to_json() (graphify/export.py) Read & json.loads() existing graph.json twice Capture _prev_hyperedges = existing_data.get("hyperedges") during the shrink-guard parse and reuse it Avoid duplicate file I/O and full JSON parse while preserving force=True, empty-file, and oversized-file behavior
tests/test_export.py No assertion on parse count during overwrite Added test_to_json_parses_existing_file_only_once_for_guards Pin single-parse behavior for both force=False and force=True

Type of change

  • Refactor / Performance
  • Tests or CI

How was this tested?

uv run pytest tests/test_export.py tests/test_export_control_characters.py tests/test_export_path_length.py -q
uv run ruff check graphify/export.py tests/test_export.py
PATH="$(pwd)/.venv/bin:$PATH" uv run pre-commit run --all-files
uv run graphify update .

Measured on graphify-out/graph.json (28,629 nodes / 61,344 edges, 28.8 MB graph.json): json.loads calls dropped from 2 to 1 (0.787s total overwrite).

@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Gaurav-meena95. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 30, 2026

@graphify-labs graphify-labs 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.

Graphify review View in Graphify

Nothing blocking · 6 possible issues to check · Merge confidence 5/5

6 possible issues to check · High impact

The change reuses the existing-file parse from the shrink check when checking for hyperedge metadata.

6 possible issues we couldn't confirm
  • The perf test counts every json.loads call in the process, so unrelated parsing during to_json would fail it and it can't isolate the graph. · tests/test_export.py:1986
  • The test named for avoiding duplicate reads only counts JSON parses, so a second read of graph.json would still pass · tests/test_export.py:1976
  • The stderr check after the force=True write can match output from an earlier call and does not confirm the warning came from the call under · tests/test_export.py:1996
  • +3 more in Graphify

They don't change the score.

Code graph · 1165 functions depend on this change
  • 1 function became more coupled. to_json() (124 callers).
  • 54 of 658 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 433c742 · 2 earlier findings not re-checked

Comment thread graphify/export.py Resolved
safishamsi pushed a commit that referenced this pull request Nov 30, 2026
safishamsi added a commit that referenced this pull request Nov 30, 2026
Extractor accuracy (Swift #5641, Apex #5627, Python #5647, TypeScript
#5604/#5614/#5612, Bash caller-var #5602), GitHub Actions run-step
extraction (#5603), incremental correctness (#5634/#5638/#5636), symlink
and semantic-cache fixes (#5617/#5618), export/report hardening and perf
(#5643/#5644/#5645/#5646/#5619/#5620/#5621), and CLI/docs/skill/install
work (#5629/#5628/#5626/#5624/#5623/#5625/#5642/#5583). AST cache schema
59 to 60; Swift/Apex/Python/Bash/GHA inputs re-extract on first build.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.124 (just released to PyPI), cherry-picked with your authorship preserved so it shows under your name in the history. Thanks @Gaurav-meena95 for avoiding the duplicate graph.json read and parse in to_json — verified green in the full suite and, where applicable, smoke-tested against the published package. 🙏

@safishamsi safishamsi closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants