-
Notifications
You must be signed in to change notification settings - Fork 219
PERF: Faster transform direction if already passed an Enum #1205
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
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1205 +/- ##
=======================================
Coverage 96.25% 96.25%
=======================================
Files 20 20
Lines 1791 1791
=======================================
Hits 1724 1724
Misses 67 67 Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
StrEnum looks nice, but is Python 3.11+. |
I wonder if it would be simpler to add the change there: Line 14 in e345729
if isinstance(item, cls):
return item |
Adding the change directly to Enums would be cleaner, but does add a bit of a speed hit still... (I assume going from Cython -> Python -> Cython adds some overhead?)
I think we could get away with just subclassing string here and defining: I think the question if you want to go this route is whether losing case sensitivity is a deal breaker or not? |
To make the code simpler, thoughts about adding this function: cdef PJ_DIRECTION get_pj_direction(object direction) except *:
if not isinstance(direction, TransformDirection):
direction = TransformDirection.create(direction)
return _PJ_DIRECTION_MAP[direction] and doing this: cdef PJ_DIRECTION pj_direction = get_pj_direction(direction) |
Have you tested the performance of this implementation? |
👍 That seems like it would be nice! However, I think we should decide on the next comment first.
Yes, it is actually faster because you don't need an isinstance check. You can go right into your dictionary with both Downsides are that |
How much of a performance improvement do you get compared to this PR? I think that if it is significant, I don't think that being case insensitive is as important. |
I am wondering if this would be faster and still allow the error message and support case insensitive input: cdef PJ_DIRECTION get_pj_direction(object direction) except *:
try:
return _PJ_DIRECTION_MAP[direction]
except KeyError:
direction = TransformDirection.create(direction)
return _PJ_DIRECTION_MAP[direction] |
ba18da6
to
05c9a62
Compare
I like your most recent suggestion of the try/except inside a helper function. We can add |
05c9a62
to
f76dc26
Compare
Thanks @greglucas 👍 |
Currently,
Enum.create()
adds quite a bit of overhead to the transform calls, so we probably don't want to call it when unnecessary. Here I addedisinstance()
checks to handle it in a completely backwards compatible way.I think it might be even nicer to remove
Enum.create()
entirely and move over to aStrEnum
/BaseEnum(str, Enum)
and then the dictionary lookup could be either"FORWARD"
orTransformDirection.FORWARD
with the caveat that you'd lose the case insensitive conversion, but the code would be a bit cleaner everywhere then.