Skip to content

perf: a vertex fills its attributes directly in __init__ and add_vertex - #62

Merged
petercorke merged 2 commits into
mainfrom
fix/vertex-init-setattr
Oct 13, 2026
Merged

petercorke merged 2 commits into
mainfrom
fix/vertex-init-setattr

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Vertex.__setattr__ (added with the k-d tree in #60) tells the graph when v.coord is assigned, so that a stale spatial index is never used. It runs on every attribute assignment, including the seven in Vertex.__init__ and the two in Graph.add_vertex, and cost about 30% on vertex creation.

A vertex that is being made or added is in no graph, so there is no index to invalidate:

  • Vertex.__init__ updates __dict__ in one operation (with a comment on why), and the attributes are declared on the class so the types are still known. The duplicate self._edgelist = [] is gone.
  • add_vertex writes _name and _graph directly. The name setter does nothing but set _name for a vertex that is in no graph.
  • The hook is unchanged for what it is for: v.coord = x and v.coord += x on a vertex that is in a graph.
before after no hook at all
g.add_vertex([0, 0]) 3.79 µs 3.12 µs 2.90 µs
Vertex([0, 0]) 2.06 µs 1.62 µs 1.48 µs

12 new tests: the attributes of a new vertex, the coordinate being a copy, subclasses with their own __init__, the name table and back-pointer, renaming, a refused duplicate leaving the vertex free, and that assigning coord (= and +=) is still seen while other attributes are not a change. 750 pass, mypy clean.

🤖 Generated with Claude Code

…d graph directly

The coordinate hook in Vertex.__setattr__ made every attribute assignment in
the constructor and in add_vertex a Python-level call.  A vertex that is being
made, or added, is in no graph, so there is no spatial index for the hook to
invalidate: __init__ now updates __dict__ in one operation and add_vertex
writes _name and _graph directly.  Assigning coord to a vertex that is in a
graph is still seen.

add_vertex 3.79 -> 3.12 us, Vertex() 2.06 -> 1.62 us.  The attributes are
declared on the class so the types are still known, and the duplicate
assignment of _edgelist is gone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

codacy-production Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 15 complexity · 0 duplication

Metric Results
Complexity 15
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.24%. Comparing base (1cc79f3) to head (ed5af4f).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #62      +/-   ##
==========================================
+ Coverage   97.18%   97.24%   +0.06%     
==========================================
  Files           3        3              
  Lines        1562     1560       -2     
==========================================
- Hits         1518     1517       -1     
+ Misses         44       43       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@petercorke
petercorke merged commit 041033f into main Oct 13, 2026
12 checks passed
@petercorke
petercorke deleted the fix/vertex-init-setattr branch October 13, 2026 00:14
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