perf: Optimize strip_trailing_slash with O(1) index check (#19679)

* perf: Optimize strip_trailing_slash with O(1) index check

Replace rstrip("/") with direct index check for O(1) performance
instead of O(n) string scanning.

Results:
- strip_trailing_slash: 311ms → 13ms (96% faster)
- get_standard_logging_object_payload: 6.11s → 5.80s (5% faster)

* Handle multiple trailing slashes in strip_trailing_slash

Use rstrip for correctness when URL ends with "//" or more,
otherwise use O(1) index check for single trailing slash.
This commit is contained in:
ryan-crabbe 2026-01-23 17:12:08 -08:00 committed by GitHub
parent 5c61586e65
commit 0133d50a45
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4652,7 +4652,10 @@ class StandardLoggingPayloadSetup:
@staticmethod
def strip_trailing_slash(api_base: Optional[str]) -> Optional[str]:
if api_base:
return api_base.rstrip("/")
if api_base.endswith("//"):
return api_base.rstrip("/")
if api_base[-1] == "/":
return api_base[:-1]
return api_base
@staticmethod