Use a value -> member map for decoding ASN.1 value sets#14965
Merged
Conversation
Replaces the linear scan over enum members in value set decoding with an O(1) lookup in a value -> member map, built once when the class is registered and stored in Type::ValueSet. This is now possible because the ASN.1 wrapper types implement __hash__ (#14963). Member values that implement __eq__ but not __hash__ (e.g. Null) can't be used as dict keys; for those the map is None and decoding falls back to the previous linear scan, preserving behavior. https://claude.ai/code/session_01FrUdGjs6fKGSQ6E6WQPGB1
Per review: value set member values must be hashable. The value -> member map is now built unconditionally at registration time, so an enum with unhashable member values (e.g. Null) raises TypeError when decorated, and decoding is always an O(1) map lookup. https://claude.ai/code/session_01FrUdGjs6fKGSQ6E6WQPGB1
- Use a {} literal for the value map in test_fields_of_variant_type
(the annotation stays: mypy can't infer the type of an empty literal)
- Drop test_fail_unhashable_member_values
https://claude.ai/code/session_01FrUdGjs6fKGSQ6E6WQPGB1
reaperhulk
approved these changes
Jun 7, 2026
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.
Resolves the performance TODO from #14962: value set decoding did a linear scan over the enum members to map the decoded value back to a member.
Type::ValueSetnow stores a value -> member map (aPyDict), built once when the class is decorated, and decoding is a single O(1) dict lookup. This is possible now that the ASN.1 wrapper types implement__hash__(#14963).As a consequence, value set member values must be hashable: an enum whose member values implement
__eq__but not__hash__(e.g.Null) now raisesTypeErrorat decoration time.https://claude.ai/code/session_01FrUdGjs6fKGSQ6E6WQPGB1
Generated by Claude Code