-
Notifications
You must be signed in to change notification settings - Fork 0
perf: replace repr-based sorting with str-based and enforce sorted JSON keys #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -347,7 +347,7 @@ def _stable_repr_to( | |
| _visited.add(obj_id) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| buf.write("{") | ||
| first = True | ||
| for item in sorted(obj, key=repr): | ||
| for item in sorted(obj, key=str): | ||
| if not first: | ||
| buf.write(", ") | ||
| first = False | ||
|
|
@@ -362,7 +362,7 @@ def _stable_repr_to( | |
| _visited.add(obj_id) | ||
| buf.write("frozenset({") | ||
| first = True | ||
| for item in sorted(obj, key=repr): | ||
| for item in sorted(obj, key=str): | ||
| if not first: | ||
| buf.write(", ") | ||
| first = False | ||
|
|
@@ -377,7 +377,7 @@ def _stable_repr_to( | |
| _visited.add(obj_id) | ||
| buf.write("{") | ||
| first = True | ||
| for key, val in sorted(obj.items(), key=lambda p: repr(p[0])): | ||
| for key, val in sorted(obj.items(), key=lambda p: str(p[0])): | ||
| if not first: | ||
| buf.write(", ") | ||
| first = False | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P0Switching fromreprtostras sort key breaks determinism for mixed-type collections where different elements share the samestr()representation (e.g.,1and'1'). Python'ssortedpreserves original iteration order when keys compare equal, but for unordered containers like sets that iteration order is arbitrary and can vary between runs. Identical inputs may then produce different canonical representations and different hashes, causing cache misses or false hits. Either keepreprfor a guaranteed total order, or—if mixed types are not supported—clearly document the restriction and add a check or test to detect the case.