馃悰 Spread block hashes to avoid pathological set/dict collisions - #608
Merged
Conversation
MiWeiss
force-pushed
the
fix/block-hash
branch
3 times, most recently
from
September 2, 2026 20:01
8f5851d to
620a443
Compare
`Block.__hash__` only mixed in type, start_line and raw. Blocks created programmatically have neither a start_line nor a raw value, so every such block of a given type hashed identically. Building a set or dict of N constructed blocks thus degenerated into N^2 `__eq__` calls, each of which compares the full `__dict__` (walking every `Field` of an `Entry`). `Entry`, `String`, `Preamble`, `ExplicitComment` and `ImplicitComment` now mix their cheap, hashable identifiers (entry type/key, key, text) into the hash. Values, fields and parser metadata stay excluded, as they may be mutable, expensive to hash or outright unhashable after middleware ran. Deduplicating 2000 constructed entries: 0.375s -> 0.0004s. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
MiWeiss
force-pushed
the
fix/block-hash
branch
from
September 2, 2026 20:03
620a443 to
5aceb0c
Compare
Collaborator
Author
|
lgtm |
MiWeiss
force-pushed
the
fix/block-hash
branch
from
September 2, 2026 20:38
ac12393 to
fc48829
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.
Block.__hash__uses(type, start_line, raw), bothNonefor blocks built in code, so every such block of a type shares one hash.set()then falls back to__eq__, which compares the full__dict__.set()over N distinct entries:Fix: mix in discriminators already compared by
__eq__, namely entry type and key, string key, preamble and comment text. Values, fields and metadata stay out, as they can be unhashable after middleware. Error-path blocks stay on the base hash; their__eq__includes anException, so instances are never equal anyway.Tests: 5 cases, including the equal-implies-equal-hash invariant and unhashable values. Suite 2581 passed, from 2576.
馃 Generated with Claude Code