Skip to content

Conversation

@sirosen
Copy link
Member

@sirosen sirosen commented Dec 4, 2025

A single startswith call, rather than an or (which will usually not
short-circuit) will make every call to send a request via SDK clients a
little faster.

A single startswith call, rather than an `or` (which will usually *not*
short-circuit) will make every call to send a request via SDK clients a
little faster.
@sirosen sirosen added the no-news-is-good-news This change does not require a news file label Dec 4, 2025
Copy link
Contributor

@derek-globus derek-globus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sirosen sirosen merged commit a208ff4 into globus:main Dec 4, 2025
8 checks passed
@kurtmckee
Copy link
Member

kurtmckee commented Dec 4, 2025

@sirosen If I'm interpreting your narrative text correctly, I need to point out that this is not a speed consideration because .startswith("http://") is not pre-evaluated and then fallen back to as a boolean value. That is, Python doesn't call .startswith() twice and then interpret the or:

return s.startswith("https://") or s.startswith("http://")
     # ^^^^^^^^^^^^^^^^^^^^^^^^    ^^^^^^^^^^^^^^^^^^^^^^^
     # True                        False

return True                     or False

Short-circuiting in Python completely prevents evaluation until it's needed. Thus Python interprets the above in this way:

return s.startswith("https://") or s.startswith("http://")
     # ^^^^^^^^^^^^^^^^^^^^^^^^
     # True

return True

You can see that evaluation of later values is completely suspended until preceding evaluations require it:

print(True  or  (1/0))  # prints 'True'
print(False and (1/0))  # prints 'False'

print(False or  (1/0))  # ZeroDivisionError
print(True  and (1/0))  # ZeroDivisionError

@sirosen
Copy link
Member Author

sirosen commented Dec 4, 2025

Indeed! I mostly was here for the stylistic cleanup, and then I noticed -- after the fact -- that the or would be slower when it does not short circuit. I mistakenly declared that it usually doesn't short circuit, but it usually does.
EDIT: that's not quite right, explanation below.

So it's a speedup in a very small set of cases, not the majority case.


I reread this again. I see that you're making the point that short-circuiting prevents calls.
That's actually exactly why this is a speed improvement. The original narrative text is fully correct. I've gotten more mixed up in reading your explanation but I was perfectly clear on this at the start.
Both of these startswith() calls evaluate to False in the common case.

So yes, this totally does speed up 99%+ of calls. Not that that's why I did it.

@sirosen sirosen deleted the slightly-tidier-startswith branch December 4, 2025 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-news-is-good-news This change does not require a news file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants