Hi Anthon, happy new year! I've spent another couple minutes on it, and it seems that this is the full fix.
Would be happy to see this finally addressed :)
# HG changeset patch# User Harmen Stoppels <me@harmenstoppels.nl># Date 1767266026 -3600# Thu Jan 01 12:13:46 2026 +0100# Node ID 670ee0e7ad049a1d7d97f0cabb8b182eb029c57b# Parent 7938d2acd452c3b5c8f4a1df2dcbc2c7661149acFixquadraticcomplexityandbrokenreferentialintegrityindeepcopyThiscommitaddressestwocriticalissuesinthedeepcopyimplementationfor`CommentedSeq`and`CommentedBase`:1.Referentialintegrity:in`CommentedBase.copy_attributes`,the`memo`dictionarywasincorrectlypassedasthedefaultvalueto`getattr()`insteadofbeingpassedasthesecondargumentto`copy.deepcopy()`.Thecall`copy.deepcopy(getattr(self, a, memo))`shouldhavebeen`copy.deepcopy(getattr(self, a), memo)`.Thiscaused`deepcopy`tostartafreshmemoizationcontextforeveryattribute,breakingreferentialintegrityforobjectssharedbetweenthesequenceanditsattributes(e.g.,comments).2.Quadraticcomplexity:in`CommentedSeq.__deepcopy__`,thecallto`self.copy_attributes`waslikelyaccidentallyindentedinsidetheloopthatcopiessequenceitems.Attributes(includingcomments)weredeep-copied`N`timesforasequenceoflength`N`.Combinedwithissue#1, this resulted insevere`N^2`performancedegradation.Thelineisunindentedsoitexecutesonceaftertheloopcompletes.diff-r7938d2acd452-r670ee0e7ad04comments.py---a/comments.pyWedDec3117:46:542025+0100+++b/comments.pyThuJan0112:13:462026+0100@@-448,7+448,7@@Tag.attrib,merge_attrib]:ifhasattr(self,a):ifmemoisnotNone:-setattr(t,a,copy.deepcopy(getattr(self,a,memo)))+setattr(t,a,copy.deepcopy(getattr(self,a),memo))else:setattr(t,a,getattr(self,a))returnt@@-562,7+562,7@@memo[id(self)]=resforkinself:res.append(copy.deepcopy(k,memo))-self.copy_attributes(res,memo=memo)+self.copy_attributes(res,memo=memo)returnresdef__add__(self,other:Any)->Any:
Any updates on this? It's a serious performance problem with a trivial fix.
I would not mind changing a performance issue, but the proposed change breaks a test that has been in existence since 2019:
So it is not that trivial.
Hi Anthon, happy new year! I've spent another couple minutes on it, and it seems that this is the full fix.
Would be happy to see this finally addressed :)