[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
diff --git a/torch/_guards.py b/torch/_guards.py
index 902794c..386618a 100644
--- a/torch/_guards.py
+++ b/torch/_guards.py
@@ -645,9 +645,9 @@
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