Skip to content

Commit e014121

Browse files
ahilgerfacebook-github-bot
authored andcommitted
faster enum inequality comparison
Summary: Remove some legacy code needed to handle thrift-py3 vs thrift-python enum comparison. Before they were merged, singleton comparison would fail. Now that thrift-py3 enums *are* the exact same class and singleton objects as thrift-python, singleton comparison inequality comparison is much faster than the string comparisons used in kludgy logic to accommodate py3 vs python. Reviewed By: Sunnie912 Differential Revision: D73465400 fbshipit-source-id: 4e3cd5d2019aa4feb4ecc6de12cba2cef045623e
1 parent b10f270 commit e014121

File tree

1 file changed

+2
-22
lines changed
  • third-party/thrift/src/thrift/lib/python

1 file changed

+2
-22
lines changed

third-party/thrift/src/thrift/lib/python/types.pyx

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,30 +2470,10 @@ class EnumMeta(type):
24702470
'__module__',
24712471
]
24722472

2473-
# check whether two classes are equivalent, meaning
2474-
# they have the same class name, py3 namespace, and thrift module name.
2475-
# The trailing `.types` or `.thrift_types` is ignored.
2476-
cdef inline _fbthrift_enum_equivalent(a, b):
2477-
cdef a_cls = a.__class__
2478-
cdef b_cls = b.__class__
2479-
if a_cls.__name__ != b_cls.__name__:
2480-
return False
2481-
cdef str a_module = a_cls.__module__
2482-
cdef str b_module = b_cls.__module__
2483-
# ignore trailing .types or .thrift_types
2484-
cdef int a_types = a_module.rfind(".")
2485-
cdef int b_types = b_module.rfind(".")
2486-
return a_module[:a_types] == b_module[:b_types]
2487-
24882473
cdef inline bint _enum_eq_(self, other):
24892474
if isinstance(other, Enum):
2490-
if self is other:
2491-
return True
2492-
# handle py3 vs python comparison
2493-
return (
2494-
self._fbthrift_value_ == other._fbthrift_value_ and
2495-
_fbthrift_enum_equivalent(self, other)
2496-
)
2475+
# enums are singletons, so use `is` to compare
2476+
return self is other
24972477
if cFollyIsDebug and isinstance(other, (bool, float)):
24982478
warnings.warn(
24992479
f"Did you really mean to compare {type(self)} and {type(other)}?",

0 commit comments

Comments
 (0)