Skip to content

perf: read the _merges slot directly instead of getattr#41

Merged
AmitMY merged 1 commit into
mainfrom
perf/merges-slot-access
Jun 27, 2026
Merged

perf: read the _merges slot directly instead of getattr#41
AmitMY merged 1 commit into
mainfrom
perf/merges-slot-access

Conversation

@AmitMY

@AmitMY AmitMY commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

_merges is a dataclass field with a default of None, so it's always initialized — getattr(self, "_merges", None) is equivalent to self._merges. Both call sites (get_merges, merge) are hot (merge runs per subgraph per training step), so dropping the getattr lookup helps.

-        cached = getattr(self, "_merges", None)
+        cached = self._merges

Output identical, tests pass. Measured back-to-back (50 texts / 200 merges; best of 3):

main this PR
BPE 0.399s 0.386s (−3%)
BNE n=4 0.786s 0.771s (−2%)
Boundless 0.480s 0.469s (−2%)

Memory unchanged.

🤖 Generated with Claude Code

_merges is a dataclass field (default None), so it is always initialized;
getattr(self, "_merges", None) can just be self._merges. Both call sites
(get_merges, merge) are on the hot path — merge runs per subgraph per
training step — so dropping the getattr lookup helps.

~2-3% faster across BPE/BNE/Boundless; memory unchanged. Output identical;
tests pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@AmitMY AmitMY merged commit b98db1f into main Jun 27, 2026
3 checks passed
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