damccorm opened a new pull request, #41110:
URL: https://github.com/apache/beam/pull/41110
### Description
Optimizes `Timestamp` and `Duration` comparison operators and
datetime/RFC3339 helpers in `sdks/python/apache_beam/utils/timestamp.py`:
1. **Direct `__le__` and `__gt__` implementations on `Timestamp` and
`Duration`:**
- Previously, `__le__` evaluated `self < other or self == other` and
`__gt__` evaluated `not (self < other or self == other)`, invoking
`Timestamp.of(other)` / `Duration.of(other)` twice whenever the first
comparison returned `False`.
- Implementing `__le__` directly and delegating `__gt__` to `not self <=
other` halves the number of `Timestamp.of` / `Duration.of` conversions on `<=`
and `>` comparisons (**1.50x faster** comparisons).
2. **Precompute module-level epoch datetimes, `_POW_10` tuple, and RFC3339
regex:**
- Hoists `_EPOCH_DATETIME_UTC`, `_EPOCH_DATETIME_NAIVE`, and
`_RFC3339_SUBMICRO_RE` to module constants instead of calling
`datetime.datetime.fromtimestamp(0, pytz.utc)` and `.replace(tzinfo=None)` or
recompiling the regex on every conversion, and replaces the `_POW_10` `dict`
with a `tuple` for faster indexed lookup.
### Testing
- Verified with `pytest sdks/python/apache_beam/utils/timestamp_test.py`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]