perf: make Node subclass bytes (C-level hash/equality)#43
Merged
Conversation
4c4bec0 to
a0d00c9
Compare
The training loop's cost is hashing and comparing candidate node-tuples: Counter(graph.get_merges()) hashes them every step, and merge scans compare them. With Node a frozen dataclass, __hash__/__eq__ were Python calls. Making Node a bytes subclass (a Node *is* its bytes) gives those operations C-level implementations — and tuples of bytes hash ~4x faster than tuples of dataclass instances. This speeds the recount path too, so unlike the disconnected-only incremental work it also helps Boundless/SuperBPE. It's net simpler: __eq__/__hash__/__len__ are inherited from bytes, and Node no longer needs a `value` field/property at all (a Node *is* its bytes) — Tree.merge just returns the token. bytes wins the MRO for the dunders we want; we keep GraphVertex.__str__ explicitly. Memory is unchanged (nodes are interned). ~1.7x BPE, ~2.3x BNE, ~1.8x Boundless; memory unchanged; output identical (digests unchanged); 138 tests pass. Note: drops the public Node.value attribute — a Node is now its own bytes, so use the node directly or bytes(node). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
a0d00c9 to
0e651f2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Answers "is there a faster counter?" — the bottleneck isn't
Counter(it's already C); it's hashing/comparing the candidate node-tuples it's fed.Counter(graph.get_merges())re-hashes them every step, and merge scans compare them. WithNodea frozen dataclass,__hash__/__eq__are Python calls.Make a
Nodebe its bytes:Now
__hash__/__eq__/__len__are bytes' C-level operations (and tuples of bytes hash ~4× faster than tuples of dataclass instances). It's net simpler (drops the custom__eq__/__hash__/__len__).Why this one matters for the whole pipeline: it speeds the recount path, so unlike the disconnected-only incremental work (#42) it also helps Boundless and SuperBPE — and the two compose.
Output identical (merge digests unchanged at 50/200 and 100/400), 138 tests pass, memory unchanged (nodes are interned, so the slightly larger object is negligible):
Note:
Nodenow compares equal to a rawbytesof the same value (bytes__eq__), but in the graph onlyNode/NodesSequenceare ever compared, so behavior is unchanged in practice.🤖 Generated with Claude Code