@@ -290,12 +290,18 @@ def test_repr(self):
290290 self .assertEqual (repr (a0 ), "()" )
291291 self .assertEqual (repr (a2 ), "(0, 1, 2)" )
292292
293+ # Checks that t is not tracked without any GC collections.
294+ def _not_tracked_instantly (self , t ):
295+ self .assertFalse (gc .is_tracked (t ), t )
296+
297+ # Checks that t is not tracked after GC collection.
293298 def _not_tracked (self , t ):
294299 # Nested tuples can take several collections to untrack
295300 gc .collect ()
296301 gc .collect ()
297302 self .assertFalse (gc .is_tracked (t ), t )
298303
304+ # Checks that t continues to be tracked even after GC collection.
299305 def _tracked (self , t ):
300306 self .assertTrue (gc .is_tracked (t ), t )
301307 gc .collect ()
@@ -307,13 +313,19 @@ def test_track_literals(self):
307313 # Test GC-optimization of tuple literals
308314 x , y , z = 1.5 , "a" , []
309315
310- self ._not_tracked (())
311- self ._not_tracked ((1 ,))
312- self ._not_tracked ((1 , 2 ))
313- self ._not_tracked ((1 , 2 , "a" ))
314- self ._not_tracked ((1 , 2 , (None , True , False , ()), int ))
315- self ._not_tracked ((object (),))
316+ # We check that those objects aren't tracked at all.
317+ # It's essential for the GC performance, see gh-139951.
318+ self ._not_tracked_instantly (())
319+ self ._not_tracked_instantly ((1 ,))
320+ self ._not_tracked_instantly ((1 , 2 ))
321+ self ._not_tracked_instantly ((1 , 2 , "a" ))
322+ self ._not_tracked_instantly ((1 , 2 ) * 5 )
323+ self ._not_tracked_instantly ((12 , 10 ** 10 , 'a_' * 100 ))
324+ self ._not_tracked_instantly ((object (),))
325+
316326 self ._not_tracked (((1 , x ), y , (2 , 3 )))
327+ self ._not_tracked ((1 , 2 , (None , True , False , ()), int ))
328+ self ._not_tracked ((object (), ()))
317329
318330 # Tuples with mutable elements are always tracked, even if those
319331 # elements are not tracked right now.
@@ -343,6 +355,12 @@ def check_track_dynamic(self, tp, always_track):
343355 self ._tracked (tp (tuple ([obj ]) for obj in [x , y , z ]))
344356 self ._tracked (tuple (tp ([obj ]) for obj in [x , y , z ]))
345357
358+ t = tp ([1 , x , y , z ])
359+ self .assertEqual (type (t ), tp )
360+ self ._tracked (t )
361+ self .assertEqual (type (t [:]), tuple )
362+ self ._tracked (t [:])
363+
346364 @support .cpython_only
347365 def test_track_dynamic (self ):
348366 # Test GC-optimization of dynamically constructed tuples.
0 commit comments