Repository navigation
Remove call-stack inspection from DateTime.__add__ - #1032
Merged
Merged
Conversation
Secrus
force-pushed
the
remove-stack-walk
branch
from
October 6, 2026 00:02
5f9d55c to
e7e8ea4
Compare
Secrus
approved these changes
Oct 6, 2026
Secrus
left a comment
Collaborator
There was a problem hiding this comment.
Thank you for that improvement!
2 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While investigating #818, I found a big contribution to the slowdown to be a stack walk in
DateTime.__add__:It turns out this exists because
ZoneInfo.from_utc()expects arithmetic with standard library semantics when called as part ofastimezone. Using the stack walk,DateTimedetects this situation and reverts to stdlib semantics. This approach has a high cost (it's slow, and has side effects like #820 ), but there is a relatively cheap alternative: convertDateTimeto adatetimebefore callingastimezone(). The same solution is proposed in #1006 (it doesn't remove the stack walk though).With this approach,
+becomes ±5x faster.in_tz/astimezonebecome ±10x faster.There's a theoretical behaviour change: calling
+from a function that happens to be calledastimezone()will get a different result.Pull Request Check List
Updated documentation for changed code.n/aAI disclosure: I used Claude Code to code this up. I have reviewed the code thoroughly.
edit: clarity relationto #1006