[dynamo][guards] Avoid unnecessary stack copies. (#119115)

There is no need to make a `frame_summary_stack` copy in case it's not modified. Proposed change uses copy-on-write functional approach that is easy to understand and is more efficient in case `self.loc_in_frame` is `None`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/119115
Approved by: https://github.com/Skylion007
This commit is contained in:
Taras Tsugrii 2024-02-10 21:55:57 +00:00 committed by PyTorch MergeBot
parent 568740f080
commit 2c8722182e

View File

@ -645,9 +645,9 @@ class TracingContext:
self = TracingContext.try_get()
if self is None:
return traceback.StackSummary()
stack = list(self.frame_summary_stack)
stack = self.frame_summary_stack
if self.loc_in_frame is not None:
stack.append(self.loc_in_frame)
stack = stack + [self.loc_in_frame]
return traceback.StackSummary.from_list(stack)
# Call this when you want to call into some code that isn't necessarily