Skip to content

perf(export): fast-path _strip_diacritics for ASCII strings and hoist unicodedata import - #5619

Open
Gaurav-meena95 wants to merge 1 commit into
Graphify-Labs:v8from
Gaurav-meena95:perf/export-strip-diacritics-ascii-fastpath
Open

Gaurav-meena95 wants to merge 1 commit into
Graphify-Labs:v8from
Gaurav-meena95:perf/export-strip-diacritics-ascii-fastpath

Conversation

@Gaurav-meena95

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds a pure-C text.isascii() fast path to _strip_diacritics() and hoists import unicodedata to module level in graphify/export.py, cutting to_json() runtime nearly in half on large graphs with bit-for-bit identical output.

Problem

to_json() calls _strip_diacritics(node.get("label", "")).lower() for every node (norm_label), and to_obsidian() calls it per note. On a ~28.3k-node graph, _strip_diacritics() imported unicodedata per call and ran unicodedata.normalize("NFKD", ...) + "".join(c for c in nfkd if not unicodedata.combining(c)) (~1.21M generator iterations) on pure-ASCII strings where NFKD decomposition is an identity operation.

Details

Target Previous Updated Rationale
graphify/export.py Per-call import unicodedata + unconditional NFKD + combining() loop Module-level import unicodedata + if text.isascii(): return text ASCII strings contain no combining marks and are invariant under NFKD
tests/test_export.py No direct unit test for _strip_diacritics ASCII fast path vs accented Unicode Added test_export_strip_diacritics_ascii_fast_path_and_unicode_folding Pin ASCII, None, int, and diacritic folding behaviour

Benchmark

Measured to_json() on graphify-out/graph.json (28,540,252 bytes output):

Metric Before After Delta
to_json() 0.935s 0.473s -49.4% (~2x faster)

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

@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 29, 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

Looks safe to merge · Merge confidence 5/5

No issues reported · High impact

The change moves unicodedata to module scope and returns ASCII strings directly from _strip_diacritics.

Code graph · 1142 functions depend on this change
  • 53 of 652 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 2e98bbe

This branch has not been deployed

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

1 participant