diff --git a/Doc/data/python3.14.abi b/Doc/data/python3.14.abi index c5630fd8dd1bd8..43bc8dcc11ed21 100644 --- a/Doc/data/python3.14.abi +++ b/Doc/data/python3.14.abi @@ -1817,7 +1817,7 @@ - + @@ -1838,7 +1838,7 @@ - + @@ -1853,7 +1853,7 @@ - + @@ -1891,7 +1891,7 @@ - + @@ -1903,7 +1903,7 @@ - + @@ -1912,10 +1912,10 @@ - + - + @@ -2003,11 +2003,11 @@ - + - + @@ -2019,14 +2019,14 @@ - + - + @@ -2045,7 +2045,7 @@ - + @@ -2073,7 +2073,7 @@ - + @@ -2115,7 +2115,7 @@ - + @@ -2132,7 +2132,7 @@ - + @@ -2160,7 +2160,7 @@ - + @@ -2190,13 +2190,13 @@ - + - + @@ -2204,7 +2204,7 @@ - + @@ -2289,7 +2289,7 @@ - + @@ -2314,7 +2314,7 @@ - + @@ -2358,7 +2358,7 @@ - + @@ -3566,7 +3566,7 @@ - + @@ -3620,7 +3620,7 @@ - + @@ -3735,7 +3735,7 @@ - + @@ -3744,7 +3744,7 @@ - + @@ -3827,7 +3827,7 @@ - + @@ -3849,12 +3849,12 @@ - + - + @@ -3871,7 +3871,7 @@ - + @@ -4108,7 +4108,7 @@ - + @@ -4194,7 +4194,7 @@ - + @@ -4202,36 +4202,36 @@ - + - + - + - + - + - + - + @@ -4295,7 +4295,7 @@ - + @@ -4408,13 +4408,13 @@ - + - + @@ -5044,7 +5044,7 @@ - + @@ -5088,14 +5088,13 @@ - - - - - + + + + - + @@ -5261,10 +5260,6 @@ - - - - @@ -5277,7 +5272,7 @@ - + @@ -5296,7 +5291,7 @@ - + @@ -5451,57 +5446,57 @@ - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -5509,8 +5504,8 @@ - - + + @@ -5530,11 +5525,11 @@ - + - - + + @@ -5552,15 +5547,15 @@ - + - - + + @@ -5614,7 +5609,7 @@ - + @@ -5671,45 +5666,45 @@ - + - + - + - + - + - + - + - - + + @@ -5717,23 +5712,23 @@ - + - + - + - - + + - + - + @@ -5745,7 +5740,7 @@ - + @@ -5764,15 +5759,15 @@ - + - + - + @@ -5825,7 +5820,7 @@ - + @@ -5891,7 +5886,7 @@ - + @@ -5919,8 +5914,8 @@ - - + + @@ -5933,7 +5928,7 @@ - + @@ -5952,7 +5947,7 @@ - + @@ -5970,7 +5965,7 @@ - + @@ -5980,8 +5975,8 @@ - - + + @@ -5989,12 +5984,12 @@ - + - + @@ -6016,7 +6011,7 @@ - + @@ -6081,25 +6076,25 @@ - - - + + + - - + + - + - + - + - + @@ -6110,42 +6105,42 @@ - + - - + + - + - + - + - + - + - + - + @@ -6165,7 +6160,7 @@ - + @@ -6176,7 +6171,7 @@ - + @@ -6187,12 +6182,12 @@ - + - + @@ -6201,7 +6196,7 @@ - + @@ -6215,67 +6210,67 @@ - - + + - + - + - - + + - + - - + + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + @@ -6284,47 +6279,47 @@ - + - + - + - + - + - + - - + + - + - + - + - - - - - - - - - + + + + + + + + + - + @@ -6337,25 +6332,26 @@ - + - + - + - + - + - - + + + @@ -6363,7 +6359,7 @@ - + @@ -6382,7 +6378,7 @@ - + @@ -6427,7 +6423,7 @@ - + @@ -6435,7 +6431,7 @@ - + @@ -6459,7 +6455,7 @@ - + @@ -6479,126 +6475,126 @@ - + - + - - - + + + - - - + + + - - - - - - - + + + + + + + - - - - + + + + - - - - + + + + - - + + - - + + - - + + - - - + + + - - - - - - - - + + + + + + + + - - - + + + - - - - + + + + - - - - - - + + + + + + - - - + + + - - + + - - + + - - + + - - - - + + + + - + - + - + - + @@ -6610,11 +6606,11 @@ - + - - + + @@ -6661,7 +6657,7 @@ - + @@ -6685,16 +6681,16 @@ - + - + - + - + @@ -6706,12 +6702,12 @@ - + - - - + + + @@ -6719,15 +6715,15 @@ - + - - + + - - + + @@ -6738,7 +6734,7 @@ - + @@ -6750,7 +6746,7 @@ - + @@ -6759,9 +6755,9 @@ - - - + + + @@ -6769,19 +6765,19 @@ - + - + - + - + @@ -6794,7 +6790,7 @@ - + @@ -6842,66 +6838,66 @@ - - - + + + - + - + - - - + + + - + - + - - + + - - + + - - - + + + - + - + - - - + + + - + - + - - + + - - - + + + - + @@ -6917,15 +6913,15 @@ - + - - + + @@ -6943,12 +6939,12 @@ - + - + @@ -7015,17 +7011,17 @@ - + - + - + @@ -7043,12 +7039,12 @@ - - - - + + + + - + @@ -7057,28 +7053,28 @@ - + - + - - + + - + - + - + - + @@ -7087,29 +7083,29 @@ - + - + - + - + - + - + - + - - - + + + @@ -7199,7 +7195,7 @@ - + @@ -7231,7 +7227,7 @@ - + @@ -7309,7 +7305,7 @@ - + @@ -7333,7 +7329,7 @@ - + @@ -7342,25 +7338,25 @@ - + - + - + - - - + + + @@ -7517,7 +7513,7 @@ - + @@ -7681,7 +7677,7 @@ - + @@ -7757,7 +7753,7 @@ - + @@ -7770,8 +7766,8 @@ - - + + @@ -7779,8 +7775,8 @@ - - + + @@ -7788,15 +7784,15 @@ - + - - - + + + @@ -7806,7 +7802,7 @@ - + @@ -7828,7 +7824,7 @@ - + @@ -7857,7 +7853,7 @@ - + @@ -7970,8 +7966,8 @@ - - + + @@ -7997,8 +7993,8 @@ - - + + @@ -8013,7 +8009,7 @@ - + @@ -8031,21 +8027,21 @@ - - + + - + - - + + - + @@ -8053,8 +8049,8 @@ - - + + @@ -8078,73 +8074,73 @@ - + - + - + - + - + - + - + - + - + - - + + - - + + - + - + - + - + - + - + @@ -8164,7 +8160,7 @@ - + @@ -8178,8 +8174,8 @@ - - + + @@ -8209,8 +8205,8 @@ - - + + @@ -8254,21 +8250,21 @@ - + - - + + - + - + @@ -8303,12 +8299,12 @@ - - - - + + + + - + @@ -8320,7 +8316,7 @@ - + @@ -8335,15 +8331,15 @@ - + - + - + - + @@ -8355,7 +8351,7 @@ - + @@ -8370,22 +8366,21 @@ - + - + - - - - - - - + + + + + + - + @@ -8398,36 +8393,36 @@ - + - + - + - - - + + + - - - + + + - - + + - - - + + + @@ -8439,14 +8434,14 @@ - + - + @@ -8455,11 +8450,11 @@ - - + + - + @@ -8467,17 +8462,17 @@ - + - + - + @@ -8485,69 +8480,28 @@ - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + - + - + - + @@ -8571,11 +8525,11 @@ - + - + @@ -8585,7 +8539,7 @@ - + @@ -8598,16 +8552,16 @@ - - - + + + - + @@ -8616,14 +8570,14 @@ - + - + @@ -8669,7 +8623,7 @@ - + @@ -8679,32 +8633,32 @@ - - - + + + - + - + - + - + - + - - + + - + - + @@ -8713,41 +8667,41 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - + - + - + - - - + + + @@ -8766,7 +8720,7 @@ - + @@ -8774,7 +8728,7 @@ - + @@ -8810,7 +8764,7 @@ - + @@ -8818,7 +8772,7 @@ - + @@ -8849,25 +8803,25 @@ - + - + - + - + - + @@ -8916,123 +8870,123 @@ - + - - + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - - + + + - - - + + + - - - + + + - - + + - - - + + + - - + + - - - - - + + + + + - - + + - - + + - + @@ -9060,7 +9014,7 @@ - + @@ -9076,7 +9030,7 @@ - + @@ -9087,13 +9041,13 @@ - - + + - + - + @@ -9102,8 +9056,8 @@ - - + + @@ -9111,9 +9065,9 @@ - + - + @@ -9128,24 +9082,24 @@ - + - + - + - + - - - - - - + + + + + + @@ -9219,11 +9173,11 @@ - + - + @@ -9235,19 +9189,19 @@ - + - + - + @@ -9274,17 +9228,17 @@ - + - + - + @@ -9301,22 +9255,22 @@ - + - + - + - + - + - + @@ -9328,15 +9282,15 @@ - - + + - - - + + + @@ -9356,7 +9310,7 @@ - + @@ -9388,7 +9342,7 @@ - + @@ -9417,17 +9371,17 @@ - + - + - + @@ -9440,7 +9394,7 @@ - + @@ -9473,16 +9427,16 @@ - - + + - + - + @@ -9492,7 +9446,7 @@ - + @@ -9501,7 +9455,7 @@ - + @@ -9535,10 +9489,10 @@ - + - + @@ -9550,327 +9504,327 @@ - + - - + + - - + + - - - + + + - - - + + + - - - + + + - - - - + + + + - - - + + + - - - + + + - - + + - - - - - + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - - + + + - - - + + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - + - - + + - - + + - + - - - - - - - + + + + + + + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - + + + + + + - + - + - - + + - + - + - + - - + + - + - + - + - + - + - + - + - + @@ -9882,147 +9836,147 @@ - + - + - + - + - + - + - + - + - + - + - - + + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - - + + - + - - + + - + @@ -10037,65 +9991,65 @@ - + - + - + - - + + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - + - + @@ -10103,192 +10057,192 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + @@ -10301,12 +10255,12 @@ - - - - - - + + + + + + @@ -10331,17 +10285,17 @@ - + - + - - + + - + @@ -10356,7 +10310,7 @@ - + @@ -10366,7 +10320,7 @@ - + @@ -10444,11 +10398,11 @@ - + - + @@ -10456,21 +10410,21 @@ - - + + - - + + - + - + @@ -10490,12 +10444,12 @@ - + - + @@ -10559,7 +10513,7 @@ - + @@ -10574,10 +10528,10 @@ - - + + - + @@ -10589,8 +10543,8 @@ - - + + @@ -10601,7 +10555,7 @@ - + @@ -10609,11 +10563,11 @@ - - - + + + - + @@ -10621,10 +10575,10 @@ - - + + - + @@ -10636,7 +10590,7 @@ - + @@ -10645,15 +10599,15 @@ - + - - - + + + @@ -10665,12 +10619,12 @@ - + - + @@ -10692,8 +10646,8 @@ - - + + @@ -10714,8 +10668,8 @@ - - + + @@ -10723,8 +10677,8 @@ - - + + @@ -10738,14 +10692,14 @@ - + - - - + + + - + @@ -10755,11 +10709,11 @@ - + - + @@ -10767,25 +10721,25 @@ - + - + - + - + - + @@ -10803,11 +10757,11 @@ - + - + - + @@ -10818,41 +10772,41 @@ - + - - + + - + - + - + - + - + - + - + @@ -10864,7 +10818,7 @@ - + @@ -10876,13 +10830,13 @@ - + - - - - + + + + @@ -10919,22 +10873,22 @@ - - + + - + - + - + @@ -10959,13 +10913,13 @@ - - + + - + @@ -11029,7 +10983,7 @@ - + @@ -11069,7 +11023,7 @@ - + @@ -11094,18 +11048,18 @@ - + - + - + @@ -11131,19 +11085,19 @@ - + - + - + @@ -11164,67 +11118,67 @@ - - - + + + - + - - + + - - - - - + + + + + - + - + - + - - + + - + - - + + - - + + - + - - - + + + - + - + - + @@ -11236,10 +11190,10 @@ - + + + - - @@ -11261,42 +11215,42 @@ - + - + - + - + - + - + - + - + - + - + @@ -11323,7 +11277,7 @@ - + @@ -11332,33 +11286,33 @@ - + - - - - - - + + + + + + - + - + - + - + @@ -11370,8 +11324,8 @@ - - + + @@ -11399,43 +11353,43 @@ - + - + - - + + - - + + - - + + - - + + - + - + - + @@ -11451,7 +11405,7 @@ - + @@ -11463,7 +11417,7 @@ - + @@ -11484,14 +11438,14 @@ - + - - + + @@ -11503,17 +11457,17 @@ - + - + - + - + @@ -11605,7 +11559,7 @@ - + @@ -11734,7 +11688,7 @@ - + @@ -11758,7 +11712,7 @@ - + @@ -11833,37 +11787,37 @@ - + - - + + - + - + - + - + @@ -11871,7 +11825,7 @@ - + @@ -11909,14 +11863,14 @@ - + - + @@ -11930,10 +11884,10 @@ - - + + - + @@ -11945,22 +11899,22 @@ - + - + - + - - + + - + @@ -11990,37 +11944,37 @@ - - - + + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + @@ -12028,11 +11982,11 @@ - + - - + + @@ -12040,20 +11994,20 @@ - + - - - - - - - - - - - + + + + + + + + + + + @@ -12061,11 +12015,11 @@ - + - - + + @@ -12073,11 +12027,11 @@ - + - - + + @@ -12085,11 +12039,11 @@ - + - - + + @@ -12097,11 +12051,11 @@ - + - - + + @@ -12109,11 +12063,11 @@ - + - - + + @@ -12121,11 +12075,11 @@ - + - - + + @@ -12133,11 +12087,11 @@ - + - - + + @@ -12145,66 +12099,66 @@ - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -12235,12 +12189,12 @@ - + - + - + @@ -12255,286 +12209,286 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -12549,28 +12503,28 @@ - + - + - + - + - + - + - + - + - + @@ -12585,26 +12539,26 @@ - + - + - + - + - + - + - + - + @@ -12615,12 +12569,12 @@ - + - + - + @@ -12635,117 +12589,117 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -12760,58 +12714,58 @@ - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -12819,267 +12773,267 @@ - + - + - + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - + + + + - - + + + - - - - + + + - - + + - - + + + - - - - + + + - - + + - - - + - - + + + + - - + + - - + + - - + + - - - + + + - - + + - + - + - - + + - - + + - + - - + + - + - - + + - + - - + + - - - + + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - + + + + + + + + + - - - + + + - - + + - - + + - - + + - - - + + + @@ -13098,37 +13052,37 @@ - + - - + + - + - + - + - + - + - + @@ -13152,13 +13106,13 @@ - + - + @@ -13166,24 +13120,24 @@ - + - + - + - - + + @@ -13192,20 +13146,20 @@ - - + + - - - - + + + + - + @@ -13221,7 +13175,7 @@ - + @@ -13241,7 +13195,7 @@ - + @@ -13249,7 +13203,7 @@ - + @@ -13265,18 +13219,18 @@ - + - + - - + + @@ -13284,8 +13238,8 @@ - - + + @@ -13298,71 +13252,71 @@ - - + + - + - + - - + + - + - + - - + + - + - + - - + + - + - + - - + + - + - + - - + + - + - + - + - - + + - + - - + + @@ -13370,8 +13324,8 @@ - - + + @@ -13379,1099 +13333,1099 @@ - - + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - + + + - - - + + + - - - - + + + + - + - - + + - + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - - - + + + + + - - + + - - - - - + + + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - - - + + + + - - + + - - - - + + + + - - + + - - + + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - + - - + + - + - - + + - + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - + + - - + + - - - - + + + + - - - - - + + + + + - + - - + + - + - - + + - + - - + + - - - + + + - - + + - - - - + + + + - - + + - + - - + + - - + + - - + + - + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - + - + - + - - + + - - + + - - + + - - + + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - + - + - - - + + + - + - + - + - + - + - - - - + + + + - + - + - + - - + + - - + + - + - - - + + + - - - - + + + + - + - + - - + + - - - + + + - - - - + + + + - - - + + + - - - + + + - - - - + + + + - - - - + + + + - - - + + + - - - + + + - - - - + + + + - - - + + + - - - + + + - - - - - + + + + + - - - + + + - - - - - + + + + + - - - - - - + + + + + + - - + + - - - - - + + + + + - - - - - + + + + + - - - - - - + + + + + + - - + + - - - - - - + + + + + + - - + + - - - + + + - - - - + + + + - - - - + + + + - + - + - - - + + + - - - + + + - - - + + + - - + + - - - + + + - - - + + + - - - + + + - - + + - - + + - - + + - - + + - - - + + + - - - + + + - - - - + + + + - - + + - - + + - - - - + + + + - - - + + + - - + + - - - + + + - - + + - - + + - - - + + + - - - + + + - - + + - - - + + + - - + + - - + + - + - - - + + + - - + + - + - - + + - - + + @@ -14479,8 +14433,8 @@ - - + + @@ -14497,150 +14451,150 @@ - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - + - - + + - - + + - - + + - - + + - - + + - + - - + + - + - - - + + + - - + + - - + + - - + + - - + + - + - - + + - - + + - + - - - - + + + + - + - + - + - + @@ -14652,11 +14606,11 @@ - + - - + + @@ -14670,16 +14624,16 @@ - - + + - + - + - + @@ -14703,178 +14657,181 @@ - + - - - + + + - - + + - - + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - + + + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - + @@ -14882,11 +14839,11 @@ - + - - + + @@ -14900,8 +14857,8 @@ - - + + @@ -14909,11 +14866,11 @@ - + - - + + @@ -14960,7 +14917,7 @@ - + @@ -14984,16 +14941,16 @@ - + - + - + - + @@ -15005,17 +14962,17 @@ - + - + - - - + + + @@ -15023,15 +14980,15 @@ - - + + - - - - + + + + @@ -15042,7 +14999,7 @@ - + @@ -15054,7 +15011,7 @@ - + @@ -15063,12 +15020,12 @@ - - - - + + + + - + @@ -15116,14 +15073,14 @@ - + - + - - + + @@ -15131,17 +15088,17 @@ - - - + + + - + - + @@ -15149,8 +15106,8 @@ - - + + @@ -15182,8 +15139,8 @@ - - + + @@ -15248,16 +15205,16 @@ - + - + - + - + @@ -15344,7 +15301,7 @@ - + @@ -15392,43 +15349,43 @@ - - + + - + - - - + + + - + - + - - + + - + - + - + - + - - + + - + @@ -15437,7 +15394,7 @@ - + @@ -15455,191 +15412,191 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - - + + - + - + - + - - - + + + - + - + - + - + - - + + - + - + - - + + @@ -15653,49 +15610,49 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -15704,31 +15661,31 @@ - + - + - + - + - + - + - + @@ -15737,28 +15694,28 @@ - + - + - + - + - + - + - + @@ -15776,47 +15733,47 @@ - + - + - + - + - - + + - - - + + + - + - + - - + + - + @@ -15840,48 +15797,48 @@ - - - + + + - + - + - + - + - - - + + + - + - + - - + + - + - - + + - + @@ -15890,11 +15847,11 @@ - + - - + + @@ -15905,10 +15862,10 @@ - + - + @@ -15935,13 +15892,13 @@ - + - + - + @@ -15974,7 +15931,7 @@ - + @@ -15995,7 +15952,7 @@ - + @@ -16004,7 +15961,7 @@ - + @@ -16019,13 +15976,13 @@ - + - + - + @@ -16051,17 +16008,17 @@ - - + + - + - - + + @@ -16069,13 +16026,13 @@ - + - - + + - + @@ -16084,10 +16041,10 @@ - + - + @@ -16104,10 +16061,10 @@ - - + + - + @@ -16116,18 +16073,18 @@ - - - - + + + + - + - + @@ -16136,7 +16093,7 @@ - + @@ -16153,10 +16110,10 @@ - - - - + + + + @@ -16164,25 +16121,25 @@ - + - - - + + + - - + + - - + + @@ -16198,20 +16155,20 @@ - - + + - - - - - - + + + + + + @@ -16219,11 +16176,11 @@ - + - - + + @@ -16231,11 +16188,11 @@ - + - - + + @@ -16243,11 +16200,11 @@ - + - - + + @@ -16255,11 +16212,11 @@ - + - - + + @@ -16291,12 +16248,12 @@ - + - + - + @@ -16311,349 +16268,349 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -16668,12 +16625,12 @@ - + - + - + @@ -16688,9 +16645,9 @@ - + - + @@ -17434,16 +17391,16 @@ - + - + - - - - - + + + + + @@ -17451,43 +17408,43 @@ - + - + - + - - - - + + + + - + - + - - - + + + - + - - + + - + - + - + @@ -17498,13 +17455,13 @@ - - + + - + - + @@ -17516,11 +17473,11 @@ - + - - + + @@ -17528,21 +17485,21 @@ - + - + - - + + - + - + - + @@ -17551,64 +17508,64 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -17619,7 +17576,7 @@ - + @@ -17669,7 +17626,7 @@ - + @@ -17698,7 +17655,7 @@ - + @@ -17724,7 +17681,7 @@ - + @@ -17759,7 +17716,7 @@ - + @@ -17767,7 +17724,7 @@ - + @@ -17781,7 +17738,7 @@ - + @@ -17792,7 +17749,7 @@ - + @@ -17803,7 +17760,7 @@ - + @@ -17817,7 +17774,7 @@ - + @@ -17828,7 +17785,7 @@ - + @@ -17836,7 +17793,7 @@ - + @@ -17847,7 +17804,7 @@ - + @@ -17858,7 +17815,7 @@ - + @@ -17872,7 +17829,7 @@ - + @@ -17880,7 +17837,7 @@ - + @@ -17894,7 +17851,7 @@ - + @@ -17902,7 +17859,7 @@ - + @@ -17922,21 +17879,21 @@ - - + + - + - + - + - + @@ -17945,8 +17902,8 @@ - - + + @@ -17963,21 +17920,21 @@ - + - + - + - + - + @@ -17986,7 +17943,7 @@ - + @@ -18006,7 +17963,7 @@ - + @@ -18014,7 +17971,7 @@ - + @@ -18038,7 +17995,7 @@ - + @@ -18046,69 +18003,69 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -18122,2628 +18079,2628 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - - + + - + - + @@ -20752,22 +20709,22 @@ - - - - - - - + + + + + + + - + - + - - + + @@ -20775,78 +20732,78 @@ - + - + - + - + - + - + - + - + - + - - + + - + - - + + - + - + - - + + - + - + - + - + - + - + - + - + - - + + - + @@ -20855,24 +20812,24 @@ - + - + - + - + - + - + @@ -20881,19 +20838,19 @@ - + - - + + - + - + @@ -20905,20 +20862,20 @@ - + - + - + - + - + - + @@ -20927,10 +20884,10 @@ - + - + @@ -20939,7 +20896,7 @@ - + @@ -20948,36 +20905,36 @@ - + - + - + - + - - + + - + - + - + @@ -20985,30 +20942,30 @@ - + - - + + - + - + - + - + - - + + - + @@ -21017,376 +20974,406 @@ - + - + - + - + + + + + + + + + + + + + - + - + - + - + - + - - - - + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -21398,396 +21385,396 @@ - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - + - + - + - + - + - + - + - + - + - + @@ -21799,160 +21786,160 @@ - + - + - + - + - + - + - + - + - + - + - - + + - + - + - - + + - + - - + + - + - + - + - - - + + + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -21967,34 +21954,34 @@ - + - + - + - - + + - + - + - + - + - + - - + + - + @@ -22003,7 +21990,7 @@ - + @@ -22011,43 +21998,43 @@ - + - + - + - + - - + + - + - + - + - + - + - + @@ -22062,21 +22049,21 @@ - + - + - + - + - + - + @@ -22087,7 +22074,7 @@ - + @@ -22096,158 +22083,158 @@ - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -22271,7 +22258,7 @@ - + @@ -22280,85 +22267,85 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -22367,21 +22354,21 @@ - + - + - + - + - + - + @@ -22390,10 +22377,10 @@ - + - + @@ -22401,25 +22388,25 @@ - + - + - + - + - + - + - + - + @@ -22434,7 +22421,7 @@ - + @@ -22442,52 +22429,52 @@ - + - + - + - + - - + + - + - + - + - + - + - + - - + + - + - - + + - + - + @@ -22496,28 +22483,28 @@ - - + + - + - + - - + + - + - - + + - + @@ -22526,13 +22513,13 @@ - + - + @@ -22540,29 +22527,29 @@ - + - + - + - + - + - + - + - + - + @@ -22571,53 +22558,53 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -22629,36 +22616,37 @@ - + - + - + - - - + + + + - + - + - - - + + + - + @@ -22667,78 +22655,78 @@ - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + @@ -22773,36 +22761,36 @@ - - - + + + - + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - + - + - + @@ -22810,44 +22798,44 @@ - - + + - + - - - + + + - + - + - - + + - + - + - + - - + + - + @@ -22859,21 +22847,21 @@ - + - + - + - - - - - - - + + + + + + + @@ -22896,30 +22884,30 @@ - + - + - + - + - - + + - + - + - + - + @@ -22928,32 +22916,32 @@ - + - - - - - - + + + + + + - + - - - + + + - + - - + + @@ -22961,9 +22949,9 @@ - - - + + + @@ -22977,36 +22965,36 @@ - + - + - + - + - + - + - + - + - + - + - + @@ -23014,7 +23002,7 @@ - + @@ -23022,10 +23010,10 @@ - + - + @@ -23033,10 +23021,10 @@ - + - + @@ -23053,7 +23041,7 @@ - + @@ -23061,18 +23049,18 @@ - + - + - + - + - + @@ -23080,7 +23068,7 @@ - + @@ -23088,7 +23076,7 @@ - + @@ -23099,8 +23087,8 @@ - - + + @@ -23111,9 +23099,9 @@ - - - + + + @@ -23151,10 +23139,10 @@ - + - + @@ -23163,31 +23151,31 @@ - + - + - + - + - + - + - + @@ -23199,25 +23187,25 @@ - + - - + + - - + + - + - + @@ -23243,19 +23231,19 @@ - + - + - + - + @@ -23303,11 +23291,11 @@ - + - - + + @@ -23345,7 +23333,7 @@ - + @@ -23375,22 +23363,22 @@ - + - + - + - + - + @@ -23429,16 +23417,16 @@ - + - + - + @@ -23453,7 +23441,7 @@ - + @@ -23464,11 +23452,11 @@ - + - - + + @@ -23491,14 +23479,14 @@ - + - - + + @@ -23506,10 +23494,10 @@ - - + + - + @@ -23518,7 +23506,7 @@ - + @@ -23526,8 +23514,8 @@ - - + + @@ -23541,13 +23529,13 @@ - - + + - + - + @@ -23559,10 +23547,10 @@ - + - + @@ -23598,10 +23586,10 @@ - + - + @@ -23613,179 +23601,180 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - + + + + - - + + - + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - + - + + + - - - - - - - - - - - - + + + + + + + + + + - - - - - - - - + + + + + + + + - - - - - + + + + + - - + + - - - - - - + + + + + + @@ -23809,7 +23798,7 @@ - + @@ -23830,32 +23819,32 @@ - - + + - - + + - - + + - - + + - + - + @@ -24011,24 +24000,24 @@ - - + + - + - + - + - + @@ -24036,11 +24025,11 @@ - + - + @@ -24048,317 +24037,317 @@ - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - - - - - - + - - + + - - - + - - + + + - + + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -24412,11 +24401,11 @@ - + - + @@ -24425,7 +24414,7 @@ - + @@ -24476,15 +24465,15 @@ - - - + + + - + - - + + @@ -24501,10 +24490,10 @@ - - + + - + @@ -24606,41 +24595,41 @@ - + - + - + - + - + - + - + - + @@ -24649,47 +24638,47 @@ - - - - + + + + - - + + - + - - + + - + - + - + - - - - + + + + @@ -24698,8 +24687,8 @@ - + @@ -24713,7 +24702,7 @@ - + @@ -24736,13 +24725,13 @@ - + - + @@ -24755,7 +24744,7 @@ - + @@ -24811,7 +24800,7 @@ - + @@ -24843,8 +24832,8 @@ - - + + @@ -24894,7 +24883,7 @@ - + @@ -24905,8 +24894,8 @@ - - + + @@ -24914,10 +24903,10 @@ - + - + @@ -24926,13 +24915,13 @@ - - + + - + - + @@ -24953,11 +24942,11 @@ - + - - + + @@ -24971,18 +24960,18 @@ - - - - - + + + + + - + - - + + @@ -24990,7 +24979,7 @@ - + @@ -25002,8 +24991,8 @@ - - + + @@ -25017,24 +25006,24 @@ - - + + - + - + - + - + @@ -25048,8 +25037,8 @@ - - + + @@ -25102,21 +25091,21 @@ - + - + - + - + @@ -25126,19 +25115,19 @@ - + - - + + - + - + @@ -25147,7 +25136,7 @@ - + @@ -25155,7 +25144,7 @@ - + @@ -25165,10 +25154,10 @@ - + - + @@ -25193,7 +25182,7 @@ - + @@ -25237,36 +25226,36 @@ - - - + + + - + - + - - + + - + - + - + - - + + - + - - + + @@ -25277,27 +25266,27 @@ - + - + - + - - - - - - - - - - - + + + + + + + + + + + @@ -25338,25 +25327,25 @@ - + - + - + - + @@ -25364,104 +25353,104 @@ - - - - + + + + - - + + - + - - - + + + - - + + - - + + - + - - - + + + - - + + - - + + - - - + + + - + - - - + + + - - + + - - + + - - + + - - + + - - + + - + @@ -25473,45 +25462,45 @@ - - + + - - - + + + - - + + - + - - - - + + + + - - + + - - + + @@ -25519,24 +25508,24 @@ - - + + - - + + - + - + - + - + @@ -25598,7 +25587,7 @@ - + @@ -25606,30 +25595,30 @@ - + - + - + - + + - - + @@ -25668,23 +25657,23 @@ - + - + - + - - + + - + @@ -25696,21 +25685,21 @@ - + - + - + - - + + - + @@ -25722,13 +25711,13 @@ - + - - + + @@ -25740,27 +25729,27 @@ - + - + - + - + - + @@ -25784,7 +25773,7 @@ - + @@ -25827,7 +25816,7 @@ - + @@ -25838,7 +25827,7 @@ - + @@ -25849,7 +25838,7 @@ - + @@ -25859,7 +25848,7 @@ - + @@ -25869,26 +25858,26 @@ - - + + - + - - - - - + + + + + - - - - - + + + + + @@ -25932,38 +25921,38 @@ - - + + - + - + - + - + - + - - + + - - + + @@ -25988,7 +25977,7 @@ - + @@ -26000,16 +25989,16 @@ - + - + - + @@ -26108,11 +26097,11 @@ - - - + + + - + @@ -26120,10 +26109,10 @@ - + - + @@ -26135,34 +26124,34 @@ - + - + - - + + - + - - + + - - + + - + - + @@ -26200,16 +26189,16 @@ - - + + - - + + @@ -26225,25 +26214,25 @@ - + - + - + - + - + - - + + @@ -26254,55 +26243,55 @@ - + - - + + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -26312,8 +26301,8 @@ - - + + @@ -26321,25 +26310,25 @@ - - + + - + - - + + - + - + @@ -26357,12 +26346,12 @@ - + - + - + @@ -26386,7 +26375,7 @@ - + @@ -26404,7 +26393,7 @@ - + @@ -26449,201 +26438,201 @@ - + - + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - + - - - - - + + + + + - - - + + + - - + + - - + + - + - + - + - - + + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - - + + - + - - + + - + - + - - + + - - - - + + + + - - - + + + - - + + - + - + - + - + @@ -26651,15 +26640,15 @@ - - + + - - + + @@ -26669,71 +26658,71 @@ - + - + - + - - + + - + - + - - - - - - + + + + + + - + - + - + - + - + - - - + + + - - - - + + + + - + @@ -26754,10 +26743,10 @@ - + - + @@ -26769,24 +26758,24 @@ - + - + - - - - - + + + + + - + - - + + @@ -26795,33 +26784,33 @@ - + - - + + - - + + - - - - + + + + - + - + @@ -26830,44 +26819,44 @@ - - + + - - + + - + - + - + - + - - + + - - + + - + @@ -26880,9 +26869,9 @@ - + - + @@ -26891,14 +26880,14 @@ - + - + - + @@ -26910,14 +26899,14 @@ - + - + - + @@ -26927,51 +26916,51 @@ - - + + - + - + - + - + - + - + - - + + - + - + - + - + @@ -26985,7 +26974,7 @@ - + @@ -27011,22 +27000,22 @@ - - - - - - + + + + + - - + + + - + - + @@ -27041,38 +27030,38 @@ - + - + - + - + - + - + - - + + @@ -27095,9 +27084,9 @@ - - - + + + @@ -27105,10 +27094,10 @@ - - + + - + @@ -27117,7 +27106,7 @@ - + @@ -27131,8 +27120,8 @@ - - + + @@ -27145,10 +27134,10 @@ - - - - + + + + @@ -27156,13 +27145,13 @@ - + - - + + - + @@ -27171,7 +27160,7 @@ - + @@ -27197,23 +27186,23 @@ - - - - - - + + + + + - - + + + - - + + @@ -27226,10 +27215,10 @@ - + - + @@ -27247,7 +27236,7 @@ - + @@ -27314,7 +27303,7 @@ - + @@ -27337,31 +27326,31 @@ - + - + - + - + - + - + - + - + @@ -27372,151 +27361,151 @@ - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - + - + - + - + - - - + + + - - + + - + - + - + - + - + - - + + - + @@ -27536,18 +27525,18 @@ - + - + - + @@ -27557,53 +27546,53 @@ - + - + - + - + - - - + + + - + - + - + - + - + @@ -27674,13 +27663,13 @@ - + - + @@ -27729,28 +27718,28 @@ - - + + - + - + - + - - - - - + + + + + @@ -27794,7 +27783,7 @@ - + @@ -27840,7 +27829,7 @@ - + @@ -27864,8 +27853,8 @@ - - + + @@ -27939,9 +27928,9 @@ - + - + @@ -27950,7 +27939,7 @@ - + @@ -27965,10 +27954,10 @@ - + - + @@ -27976,44 +27965,44 @@ - + - + - - - + + + - - - - + + + + - - + + - - - + + + - + - - + + - + @@ -28022,7 +28011,7 @@ - + @@ -28030,18 +28019,18 @@ - - - - - - - - - + + + + + + + + + - - + + @@ -28097,17 +28086,9 @@ - - + + - - - - - - - - @@ -28116,7 +28097,7 @@ - + @@ -28124,46 +28105,46 @@ - + - + - + - + - - - + + + - - + + - - + + - - - + + + - - + + - + - + @@ -28196,20 +28177,20 @@ - + - + - + @@ -28217,41 +28198,41 @@ - - + + - - + + - + - + - + - - - + + + - + @@ -28260,12 +28241,12 @@ - - - + + + - + @@ -28273,54 +28254,54 @@ - + - + - - + + - - - + + + - + - - + + - - + + - - + + - - + + @@ -28337,7 +28318,7 @@ - + @@ -28346,32 +28327,32 @@ - + - + - + - + - + - + - + - + @@ -28381,9 +28362,9 @@ - - - + + + @@ -28391,10 +28372,10 @@ - - + - + + @@ -28403,37 +28384,37 @@ - + - + - + - + - + - + - + - - + + @@ -28464,7 +28445,7 @@ - + @@ -28499,22 +28480,22 @@ - + - + - + - + @@ -28528,7 +28509,7 @@ - + @@ -28587,8 +28568,8 @@ - - + + @@ -28600,16 +28581,16 @@ - + - - + + - + - + @@ -28624,13 +28605,13 @@ - - + + - + - + @@ -28645,27 +28626,27 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + - - + + - - + + - + @@ -28701,7 +28682,7 @@ - + @@ -28710,7 +28691,7 @@ - + @@ -28733,13 +28714,13 @@ - + - - + + @@ -28764,21 +28745,21 @@ - + - + - + - + @@ -28787,7 +28768,7 @@ - + @@ -28801,7 +28782,7 @@ - + @@ -28810,7 +28791,7 @@ - + @@ -28841,7 +28822,7 @@ - + @@ -28872,18 +28853,18 @@ - + - + - + @@ -28900,19 +28881,19 @@ - + - + - + @@ -28947,12 +28928,12 @@ - + - + @@ -28965,44 +28946,44 @@ - + - + - + - + - + - + - + - + - + - + @@ -29011,32 +28992,32 @@ - + - + - + - + - + - + - + @@ -29057,210 +29038,210 @@ - + - - + + - + - + - - - - - - - - - + + + + + + + + + - + - - - - + + + + - - - - + + + + - - - - - + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - + + + + + - - - - - + + + + + - - - - - + + + + + - - - - - + + + + + - - - - - + + + + + - - - - - + + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - + + + + - - - - - + + + + + - + - + - + - + - + - - - + + + - + - - + + - + - - - + + + - + - - + + - + - - - - - + + + + + @@ -29272,8 +29253,8 @@ - - + + @@ -29319,21 +29300,21 @@ - + - + - - - + + + @@ -29342,53 +29323,53 @@ - + - + - + - + - + - - + + - + - + - - - + + + - + - - + + - + - - - - - - + + + + + + @@ -29404,7 +29385,7 @@ - + @@ -29413,21 +29394,21 @@ - + - + - + - + @@ -29443,88 +29424,88 @@ - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - + - + - - - + + + - + - + - + @@ -29542,13 +29523,13 @@ - + - + - + @@ -29560,16 +29541,16 @@ - + - + - - - - + + + + @@ -29579,7 +29560,7 @@ - + @@ -29614,7 +29595,7 @@ - + @@ -29627,12 +29608,12 @@ - + - + @@ -29653,39 +29634,39 @@ - + - + - + - + - - + + - + - - - - + + + + - + - + @@ -29694,34 +29675,34 @@ - + - + - + - + - + - + @@ -29779,22 +29760,22 @@ - - - + + + - + - - + + - + @@ -29802,8 +29783,8 @@ - - + + @@ -29815,23 +29796,23 @@ - - + + - + - + - + @@ -29849,11 +29830,11 @@ - + - + @@ -29863,66 +29844,66 @@ - + - + - - + + - + - - - - + + + + - - + + - - + + - + - + - + - + - - + + - + - + - + - + - + - + @@ -29930,7 +29911,7 @@ - + @@ -29938,59 +29919,59 @@ - + - + - + - + - - - - - + + + + + - + - + - - + + - + - + - - - + + + - - - - - - - + + + + + + + @@ -30104,7 +30085,7 @@ - + @@ -30157,13 +30138,13 @@ - + - - + + @@ -30195,7 +30176,7 @@ - + @@ -30439,7 +30420,7 @@ - + @@ -30471,7 +30452,7 @@ - + @@ -30547,7 +30528,7 @@ - + @@ -30556,14 +30537,14 @@ - - + + - + - + @@ -30622,18 +30603,19 @@ - + - - + + - - - - + + + + + @@ -30652,8 +30634,8 @@ - - + + @@ -30687,7 +30669,7 @@ - + @@ -30715,7 +30697,7 @@ - + @@ -30726,7 +30708,7 @@ - + @@ -30739,24 +30721,24 @@ - + - + - + - + - + @@ -30775,7 +30757,7 @@ - + @@ -30832,6 +30814,14 @@ + + + + + + + + @@ -30840,22 +30830,22 @@ - + - + - + - + @@ -30865,11 +30855,11 @@ - + - + @@ -30880,8 +30870,8 @@ - - + + @@ -30892,8 +30882,8 @@ - - + + @@ -30905,8 +30895,8 @@ - - + + @@ -31058,8 +31048,8 @@ - - + + @@ -31073,7 +31063,7 @@ - + @@ -31109,23 +31099,23 @@ - - - - - + + + + + - + - - + + - - + + @@ -31251,8 +31241,8 @@ - - + + @@ -31261,8 +31251,8 @@ - - + + @@ -31285,8 +31275,8 @@ - - + + @@ -31333,7 +31323,7 @@ - + @@ -31357,16 +31347,16 @@ - + - + - + - + @@ -31378,17 +31368,17 @@ - + - - - - + + + + - + @@ -31396,29 +31386,29 @@ - + - + - + - + - + - + @@ -31435,18 +31425,18 @@ - + - + - + - + @@ -31454,30 +31444,30 @@ - + - - - - - - - - - - - - - + + + + + + + + + + + + + - - + + @@ -31495,43 +31485,43 @@ - + - + - + - - + + - + - + - + - + - + @@ -31561,39 +31551,39 @@ - + - + - + - + - + - - + + - + - + - + @@ -31601,7 +31591,7 @@ - + @@ -31628,13 +31618,13 @@ - + - - + + @@ -31648,8 +31638,8 @@ - - + + @@ -31658,7 +31648,7 @@ - + @@ -31698,21 +31688,21 @@ - + - + - + - + diff --git a/Doc/library/gc.rst b/Doc/library/gc.rst index 63dc8bce46d472..2c160426203f89 100644 --- a/Doc/library/gc.rst +++ b/Doc/library/gc.rst @@ -40,18 +40,11 @@ The :mod:`!gc` module provides the following functions: .. function:: collect(generation=2) - Perform a collection. The optional argument *generation* + With no arguments, run a full collection. The optional argument *generation* may be an integer specifying which generation to collect (from 0 to 2). A :exc:`ValueError` is raised if the generation number is invalid. The sum of collected objects and uncollectable objects is returned. - Calling ``gc.collect(0)`` will perform a GC collection on the young generation. - - Calling ``gc.collect(1)`` will perform a GC collection on the young generation - and an increment of the old generation. - - Calling ``gc.collect(2)`` or ``gc.collect()`` performs a full collection - The free lists maintained for a number of built-in types are cleared whenever a full collection or collection of the highest generation (2) is run. Not all items in some free lists may be freed due to the @@ -60,9 +53,6 @@ The :mod:`!gc` module provides the following functions: The effect of calling ``gc.collect()`` while the interpreter is already performing a collection is undefined. - .. versionchanged:: 3.14 - ``generation=1`` performs an increment of collection. - .. function:: set_debug(flags) @@ -80,18 +70,12 @@ The :mod:`!gc` module provides the following functions: Returns a list of all objects tracked by the collector, excluding the list - returned. If *generation* is not ``None``, return only the objects as follows: - - * 0: All objects in the young generation - * 1: No objects, as there is no generation 1 (as of Python 3.14) - * 2: All objects in the old generation + returned. If *generation* is not ``None``, return only the objects tracked by + the collector that are in that generation. .. versionchanged:: 3.8 New *generation* parameter. - .. versionchanged:: 3.14 - Generation 1 is removed - .. audit-event:: gc.get_objects generation gc.get_objects .. function:: get_stats() @@ -108,43 +92,49 @@ The :mod:`!gc` module provides the following functions: * ``uncollectable`` is the total number of objects which were found to be uncollectable (and were therefore moved to the :data:`garbage` - list) inside this generation. + list) inside this generation; + + * ``candidates`` is the total number of objects in this generation which were + considered for collection and traversed; + + * ``duration`` is the total time in seconds spent in collections for this + generation. .. versionadded:: 3.4 + .. versionchanged:: 3.14 + Add ``duration`` and ``candidates``. + .. function:: set_threshold(threshold0, [threshold1, [threshold2]]) Set the garbage collection thresholds (the collection frequency). Setting *threshold0* to zero disables collection. - The GC classifies objects into two generations depending on whether they have - survived a collection. New objects are placed in the young generation. If an - object survives a collection it is moved into the old generation. - - In order to decide when to run, the collector keeps track of the number of object + The GC classifies objects into three generations depending on how many + collection sweeps they have survived. New objects are placed in the youngest + generation (generation ``0``). If an object survives a collection it is moved + into the next older generation. Since generation ``2`` is the oldest + generation, objects in that generation remain there after a collection. In + order to decide when to run, the collector keeps track of the number object allocations and deallocations since the last collection. When the number of allocations minus the number of deallocations exceeds *threshold0*, collection - starts. For each collection, all the objects in the young generation and some - fraction of the old generation is collected. + starts. Initially only generation ``0`` is examined. If generation ``0`` has + been examined more than *threshold1* times since generation ``1`` has been + examined, then generation ``1`` is examined as well. + With the third generation, things are a bit more complicated, + see `Garbage collector design `_ + for more information. + + .. note:: + In the free-threaded build, the cycle collector is not generational. + Collections operate over the entire tracked heap. In the free-threaded build, the increase in process memory usage is also - checked before running the collector. If the memory usage has not increased + checked before running the collector. If the memory usage has not increased by 10% since the last collection and the net number of object allocations has not exceeded 40 times *threshold0*, the collection is not run. - The fraction of the old generation that is collected is **inversely** proportional - to *threshold1*. The larger *threshold1* is, the slower objects in the old generation - are collected. - For the default value of 10, 1% of the old generation is scanned during each collection. - - *threshold2* is ignored. - - See `Garbage collector design `_ for more information. - - .. versionchanged:: 3.14 - *threshold2* is ignored - .. function:: get_count() diff --git a/Include/internal/pycore_gc.h b/Include/internal/pycore_gc.h index fd284d0e4ecc2f..f67eeb7263a0b4 100644 --- a/Include/internal/pycore_gc.h +++ b/Include/internal/pycore_gc.h @@ -211,7 +211,6 @@ extern void _Py_ScheduleGC(PyThreadState *tstate); extern void _Py_TriggerGC(struct _gc_runtime_state *gcstate); #endif - /* Tell the GC to track this object. * * The object must not be tracked by the GC. @@ -220,7 +219,7 @@ extern void _Py_TriggerGC(struct _gc_runtime_state *gcstate); * ob_traverse method. * * Internal note: interp->gc.generation0->_gc_prev doesn't have any bit flags - * because it's not object header. So we don't use _PyGCHead_PREV() and + * because it's not an object header. So we don't use _PyGCHead_PREV() and * _PyGCHead_SET_PREV() for it to avoid unnecessary bitwise operations. * * See also the public PyObject_GC_Track() function. @@ -245,16 +244,15 @@ static inline void _PyObject_GC_TRACK( filename, lineno, __func__); struct _gc_runtime_state *gcstate = &_PyInterpreterState_GET()->gc; - PyGC_Head *generation0 = &gcstate->young.head; - PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev); + PyGC_Head *generation0 = gcstate->generation0; + PyGC_Head *last = (PyGC_Head *)(generation0->_gc_prev); _PyGCHead_SET_NEXT(last, gc); _PyGCHead_SET_PREV(gc, last); - uintptr_t not_visited = 1 ^ gcstate->visited_space; - gc->_gc_next = ((uintptr_t)generation0) | not_visited; + _PyGCHead_SET_NEXT(gc, generation0); generation0->_gc_prev = (uintptr_t)gc; - gcstate->young.count++; /* number of tracked GC objects */ - gcstate->heap_size++; - if (gcstate->young.count > gcstate->young.threshold) { + /* gh-139951: count tracked GC objects, not all GC-capable allocations. */ + gcstate->generations[0].count++; /* number of tracked GC objects */ + if (gcstate->generations[0].count > gcstate->generations[0].threshold) { _Py_TriggerGC(gcstate); } #endif @@ -292,10 +290,9 @@ static inline void _PyObject_GC_UNTRACK( gc->_gc_next = 0; gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED; struct _gc_runtime_state *gcstate = &_PyInterpreterState_GET()->gc; - if (gcstate->young.count > 0) { - gcstate->young.count--; + if (gcstate->generations[0].count > 0) { + gcstate->generations[0].count--; } - gcstate->heap_size--; #endif } diff --git a/Include/internal/pycore_interp_structs.h b/Include/internal/pycore_interp_structs.h index c3d9533e90fdec..6ed4dcf710c17a 100644 --- a/Include/internal/pycore_interp_structs.h +++ b/Include/internal/pycore_interp_structs.h @@ -187,12 +187,19 @@ struct gc_collection_stats { /* Running stats per generation */ struct gc_generation_stats { + PyTime_t ts_start; + PyTime_t ts_stop; + /* total number of collections */ Py_ssize_t collections; /* total number of collected objects */ Py_ssize_t collected; /* total number of uncollectable objects (put into gc.garbage) */ Py_ssize_t uncollectable; + /* total number of objects considered for collection */ + Py_ssize_t candidates; + /* total duration of the collection in seconds */ + double duration; }; enum _GCPhase { @@ -214,14 +221,23 @@ struct _gc_runtime_state { /* Is automatic collection enabled? */ int enabled; int debug; - /* linked lists of container objects */ + + /* Generational GC state used in GIL builds. */ + struct gc_generation generations[NUM_GENERATIONS]; + PyGC_Head *generation0; + struct gc_generation_stats generation_stats_gen[NUM_GENERATIONS]; + + /* Incremental/free-threaded GC state. */ struct gc_generation young; struct gc_generation old[2]; + /* a permanent generation which won't be collected */ struct gc_generation permanent_generation; struct gc_generation_stats generation_stats[NUM_GENERATIONS]; /* true if we are currently running the collector */ int collecting; + /* The frame that started the current collection, or NULL. */ + _PyInterpreterFrame *frame; /* list of uncollectable objects */ PyObject *garbage; /* a list of callbacks to be invoked when collection is performed */ @@ -233,7 +249,6 @@ struct _gc_runtime_state { int visited_space; int phase; -#ifdef Py_GIL_DISABLED /* This is the number of objects that survived the last full collection. It approximates the number of long lived objects tracked by the GC. @@ -246,6 +261,7 @@ struct _gc_runtime_state { the first time. */ Py_ssize_t long_lived_pending; +#ifdef Py_GIL_DISABLED /* True if gc.freeze() has been used. */ int freeze_active; diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index 4200d91a2fcd9d..64c82597c750a4 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -137,6 +137,11 @@ extern PyTypeObject _PyExc_MemoryError; }, \ .gc = { \ .enabled = 1, \ + .generations = { \ + { .threshold = 2000, }, \ + { .threshold = 10, }, \ + { .threshold = 10, }, \ + }, \ .young = { .threshold = 2000, }, \ .old = { \ { .threshold = 10, }, \ diff --git a/InternalDocs/garbage_collector.md b/InternalDocs/garbage_collector.md index 9c35684c945b3e..f1ff2b15db2147 100644 --- a/InternalDocs/garbage_collector.md +++ b/InternalDocs/garbage_collector.md @@ -107,7 +107,7 @@ As is explained later in the [Optimization: reusing fields to save memory](#optimization-reusing-fields-to-save-memory) section, these two extra fields are normally used to keep doubly linked lists of all the objects tracked by the garbage collector (these lists are the GC generations, more on -that in the [Optimization: incremental collection](#Optimization-incremental-collection) section), but +that in the [Optimization: generations](#Optimization-generations) section), but they are also reused to fulfill other purposes when the full doubly linked list structure is not needed as a memory optimization. @@ -350,97 +350,44 @@ follows these steps in order: the reference counts fall to 0, triggering the destruction of all unreachable objects. -Optimization: incremental collection -==================================== +Optimization: generations +========================= -In order to bound the length of each garbage collection pause, the GC implementation -for the default build uses incremental collection with two generations. - -Generational garbage collection takes advantage of what is known as the weak -generational hypothesis: Most objects die young. -This has proven to be very close to the reality of many Python +In order to limit the time each garbage collection takes, the GC +implementation for the default build uses a popular optimization: +generations. The main idea behind this concept is the assumption that most +objects have a very short lifespan and can thus be collected soon after their +creation. This has proven to be very close to the reality of many Python programs as many temporary objects are created and destroyed very quickly. To take advantage of this fact, all container objects are segregated into -two generations: young and old. Every new object starts in the young generation. -Each garbage collection scans the entire young generation and part of the old generation. - -The time taken to scan the young generation can be controlled by controlling its -size, but the size of the old generation cannot be controlled. -In order to keep pause times down, scanning of the old generation of the heap -occurs in increments. - -To keep track of what has been scanned, the old generation contains two lists: - -* Those objects that have not yet been scanned, referred to as the `pending` list. -* Those objects that have been scanned, referred to as the `visited` list. - -To detect and collect all unreachable objects in the heap, the garbage collector -must scan the whole heap. This whole heap scan is called a full scavenge. - -Increments ----------- - -Each full scavenge is performed in a series of increments. -For each full scavenge, the combined increments will cover the whole heap. - -Each increment is made up of: - -* The young generation -* The old generation's least recently scanned objects -* All objects reachable from those objects that have not yet been scanned this full scavenge - -The surviving objects (those that are not collected) are moved to the back of the -`visited` list in the old generation. - -When a full scavenge starts, no objects in the heap are considered to have been scanned, -so all objects in the old generation must be in the `pending` space. -When all objects in the heap have been scanned a cycle ends, and all objects are moved -to the `pending` list again. To avoid having to traverse the entire list, which list is -`pending` and which is `visited` is determined by a field in the `GCState` struct. -The `visited` and `pending` lists can be swapped by toggling this bit. - -Correctness ------------ - -The [algorithm for identifying cycles](#Identifying-reference-cycles) will find all -unreachable cycles in a list of objects, but will not find any cycles that are -even partly outside of that list. -Therefore, to be guaranteed that a full scavenge will find all unreachable cycles, -each cycle must be fully contained within a single increment. - -To make sure that no partial cycles are included in the increment we perform a -[transitive closure](https://en.wikipedia.org/wiki/Transitive_closure) -over reachable, unscanned objects from the initial increment. -Since the transitive closure of objects reachable from an object must be a (non-strict) -superset of any unreachable cycle including that object, we are guaranteed that a -transitive closure cannot contain any partial cycles. -We can exclude scanned objects, as they must have been reachable when scanned. -If a scanned object becomes part of an unreachable cycle after being scanned, it will -not be collected at this time, but it will be collected in the next full scavenge. - -> [!NOTE] -> The GC implementation for the free-threaded build does not use incremental collection. -> Every collection operates on the entire heap. - -In order to decide when to run, the collector keeps track of the number of object -allocations and deallocations since the last collection. When the number of -allocations minus the number of deallocations exceeds `threshold0`, -collection starts. `threshold1` determines the fraction of the old -collection that is included in the increment. -The fraction is inversely proportional to `threshold1`, -as historically a larger `threshold1` meant that old generation -collections were performed less frequently. -`threshold2` is ignored. - -These thresholds can be examined using the +three spaces/generations. Every new object starts in the first generation +(generation 0). The previous algorithm is executed only over the objects of +a particular generation and if an object survives a collection of its +generation it will be moved to the next one (generation 1), where it will be +surveyed for collection less often. If the same object survives another GC +round in this new generation (generation 1) it will be moved to the last +generation (generation 2) where it will be surveyed the least often. + +The GC implementation for the free-threaded build does not use multiple +generations. Every collection operates on the entire heap. + +In order to decide when to run, the collector keeps track of the number of +object allocations and deallocations since the last collection. When the +number of allocations minus the number of deallocations exceeds `threshold0`, +collection starts. Initially only generation 0 is examined. If generation 0 +has been examined more than `threshold1` times since generation 1 has been +examined, then generation 1 is examined as well. With generation 2, things +are a bit more complicated; see +[Collecting the oldest generation](#Collecting-the-oldest-generation) for +more information. These thresholds can be examined using the [`gc.get_threshold()`](https://docs.python.org/3/library/gc.html#gc.get_threshold) function: ```pycon >>> import gc >>> gc.get_threshold() -(700, 10, 10) +(2000, 10, 10) ``` The content of these generations can be examined using the @@ -452,85 +399,63 @@ specifically in a generation by calling `gc.collect(generation=NUM)`. >>> class MyObj: ... pass ... ->>> # Move everything to the old generation so it's easier to inspect ->>> # the young generation. +>>> # Move everything to the last generation so it's easier to inspect +>>> # the younger generations. >>> gc.collect() 0 >>> # Create a reference cycle. >>> x = MyObj() >>> x.self = x ->>> ->>> # Initially the object is in the young generation. +>>> +>>> # Initially the object is in the youngest generation. >>> gc.get_objects(generation=0) [..., <__main__.MyObj object at 0x7fbcc12a3400>, ...] ->>> +>>> >>> # After a collection of the youngest generation the object ->>> # moves to the old generation. +>>> # moves to the next generation. >>> gc.collect(generation=0) 0 >>> gc.get_objects(generation=0) [] >>> gc.get_objects(generation=1) -[] ->>> gc.get_objects(generation=2) [..., <__main__.MyObj object at 0x7fbcc12a3400>, ...] ``` +Collecting the oldest generation +-------------------------------- + +In addition to the various configurable thresholds, the GC only triggers a full +collection of the oldest generation if the ratio `long_lived_pending / +long_lived_total` is above a given value (hardwired to 25%). The reason is +that, while "non-full" collections (that is, collections of the young and +middle generations) will always examine roughly the same number of objects +(determined by the aforementioned thresholds) the cost of a full collection is +proportional to the total number of long-lived objects, which is virtually +unbounded. Indeed, it has been remarked that doing a full collection every + of object creations entails a dramatic performance +degradation in workloads which consist of creating and storing lots of +long-lived objects (for example, building a large list of GC-tracked objects +would show quadratic performance, instead of linear as expected). Using the +above ratio, instead, yields amortized linear performance in the total number +of objects (the effect of which can be summarized thusly: "each full garbage +collection is more and more costly as the number of objects grows, but we do +fewer and fewer of them"). + Optimization: excluding reachable objects ========================================= An object cannot be garbage if it can be reached. To avoid having to identify -reference cycles across the whole heap, we can reduce the amount of work done -considerably by first identifying objects reachable from objects known to be -alive. These objects are excluded from the normal cyclic detection process. - -The default and free-threaded build both implement this optimization but in -slightly different ways. - -Finding reachable objects for the default build GC --------------------------------------------------- - -This works by first moving most reachable objects to the `visited` space. -Empirically, most reachable objects can be reached from a small set of global -objects and local variables. This step does much less work per object, so -reduces the time spent performing garbage collection by at least half. - -> [!NOTE] -> Objects that are not determined to be reachable by this pass are not necessarily -> unreachable. We still need to perform the main algorithm to determine which objects -> are actually unreachable. -We use the same technique of forming a transitive closure as the incremental -collector does to find reachable objects, seeding the list with some global -objects and the currently executing frames. - -This phase moves objects to the `visited` space, as follows: - -1. All objects directly referred to by any builtin class, the `sys` module, the `builtins` -module and all objects directly referred to from stack frames are added to a working -set of reachable objects. -2. Until this working set is empty: - 1. Pop an object from the set and move it to the `visited` space - 2. For each object directly reachable from that object: - * If it is not already in `visited` space and it is a GC object, - add it to the working set - - -Before each increment of collection is performed, the stacks are scanned -to check for any new stack frames that have been created since the last -increment. All objects directly referred to from those stack frames are -added to the working set. -Then the above algorithm is repeated, starting from step 2. - +reference cycles across the whole heap, the free-threaded build first identifies +objects reachable from objects known to be alive. These objects are excluded +from the normal cyclic detection process. Finding reachable objects for the free-threaded GC -------------------------------------------------- Within the `gc_free_threading.c` implementation, this is known as the "mark -alive" pass or phase. It is similar in concept to what is done for the default -build GC. Rather than moving objects between double-linked lists, the -free-threaded GC uses a flag in `ob_gc_bits` to track if an object is -found to be definitely alive (not garbage). +alive" pass or phase. The free-threaded GC uses a flag in `ob_gc_bits` to track +if an object is found to be definitely alive (not garbage). To find objects reachable from known alive objects, known as the "roots", the `gc_mark_alive_from_roots()` function is used. Root objects include diff --git a/Lib/test/_test_gc_fast_cycles.py b/Lib/test/_test_gc_fast_cycles.py deleted file mode 100644 index 4e2c7d72a02713..00000000000000 --- a/Lib/test/_test_gc_fast_cycles.py +++ /dev/null @@ -1,48 +0,0 @@ -# Run by test_gc. -from test import support -import _testinternalcapi -import gc -import unittest - -class IncrementalGCTests(unittest.TestCase): - - # Use small increments to emulate longer running process in a shorter time - @support.gc_threshold(200, 10) - def test_incremental_gc_handles_fast_cycle_creation(self): - - class LinkedList: - - #Use slots to reduce number of implicit objects - __slots__ = "next", "prev", "surprise" - - def __init__(self, next=None, prev=None): - self.next = next - if next is not None: - next.prev = self - self.prev = prev - if prev is not None: - prev.next = self - - def make_ll(depth): - head = LinkedList() - for i in range(depth): - head = LinkedList(head, head.prev) - return head - - head = make_ll(1000) - - assert(gc.isenabled()) - olds = [] - initial_heap_size = _testinternalcapi.get_tracked_heap_size() - for i in range(20_000): - newhead = make_ll(20) - newhead.surprise = head - olds.append(newhead) - if len(olds) == 20: - new_objects = _testinternalcapi.get_tracked_heap_size() - initial_heap_size - self.assertLess(new_objects, 27_000, f"Heap growing. Reached limit after {i} iterations") - del olds[:] - - -if __name__ == "__main__": - unittest.main() diff --git a/Lib/test/test_finalization.py b/Lib/test/test_finalization.py index 42871f8a09b16b..03a41a7d4fad61 100644 --- a/Lib/test/test_finalization.py +++ b/Lib/test/test_finalization.py @@ -276,19 +276,28 @@ def test_simple_resurrect(self): s = SelfCycleResurrector() ids = [id(s)] wr = weakref.ref(s) + wrc = weakref.ref(s, lambda x: None) del s gc.collect() self.assert_del_calls(ids) self.assert_survivors(ids) - # XXX is this desirable? - self.assertIs(wr(), None) + # The backport keeps the 3.14 free-threaded collector unchanged, + # so weakrefs without callbacks are still cleared before + # finalizers there. GIL builds follow the newer behavior. + if support.Py_GIL_DISABLED: + self.assertIsNone(wr()) + else: + self.assertIsNotNone(wr()) + # A weakref with a callback is still cleared before calling + # finalizers. + self.assertIsNone(wrc()) # When trying to destroy the object a second time, __del__ # isn't called anymore (and the object isn't resurrected). self.clear_survivors() gc.collect() self.assert_del_calls(ids) self.assert_survivors([]) - self.assertIs(wr(), None) + self.assertIsNone(wr()) def test_simple_suicide(self): # Test the GC is able to deal with an object that kills its last @@ -378,18 +387,32 @@ def check_non_resurrecting_chain(self, classes): def check_resurrecting_chain(self, classes): N = len(classes) + def dummy_callback(ref): + pass with SimpleBase.test(): nodes = self.build_chain(classes) N = len(nodes) ids = [id(s) for s in nodes] survivor_ids = [id(s) for s in nodes if isinstance(s, SimpleResurrector)] wrs = [weakref.ref(s) for s in nodes] + wrcs = [weakref.ref(s, dummy_callback) for s in nodes] del nodes gc.collect() self.assert_del_calls(ids) self.assert_survivors(survivor_ids) - # XXX desirable? - self.assertEqual([wr() for wr in wrs], [None] * N) + for wr in wrs: + # The backport keeps the 3.14 free-threaded collector + # unchanged, so weakrefs without callbacks are still cleared + # before finalizers there. GIL builds follow the newer + # behavior. + if support.Py_GIL_DISABLED: + self.assertIsNone(wr()) + else: + self.assertIsNotNone(wr()) + for wr in wrcs: + # Weakrefs with callbacks are still cleared before calling + # finalizers. + self.assertIsNone(wr()) self.clear_survivors() gc.collect() self.assert_del_calls(ids) diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py index 3879f116731a22..3ee88d25fd33d0 100644 --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -7,7 +7,7 @@ Py_GIL_DISABLED) from test.support.import_helper import import_module from test.support.os_helper import temp_dir, TESTFN, unlink -from test.support.script_helper import assert_python_ok, make_script, run_test_script +from test.support.script_helper import assert_python_ok, make_script from test.support import threading_helper, gc_threshold import gc @@ -399,11 +399,19 @@ def test_collect_generations(self): # each call to collect(N) x = [] gc.collect(0) - # x is now in the old gen + # x is now in gen 1 a, b, c = gc.get_count() - # We don't check a since its exact values depends on + gc.collect(1) + # x is now in gen 2 + d, e, f = gc.get_count() + gc.collect(2) + # x is now in gen 3 + g, h, i = gc.get_count() + # We don't check a, d, g since their exact values depends on # internal implementation details of the interpreter. self.assertEqual((b, c), (1, 0)) + self.assertEqual((e, f), (0, 1)) + self.assertEqual((h, i), (0, 0)) def test_trashcan(self): class Ouch: @@ -827,11 +835,15 @@ def test_get_stats(self): self.assertEqual(len(stats), 3) for st in stats: self.assertIsInstance(st, dict) - self.assertEqual(set(st), - {"collected", "collections", "uncollectable"}) + self.assertEqual( + set(st), + {"collected", "collections", "uncollectable", "candidates", "duration"} + ) self.assertGreaterEqual(st["collected"], 0) self.assertGreaterEqual(st["collections"], 0) self.assertGreaterEqual(st["uncollectable"], 0) + self.assertGreaterEqual(st["candidates"], 0) + self.assertGreaterEqual(st["duration"], 0) # Check that collection counts are incremented correctly if gc.isenabled(): self.addCleanup(gc.enable) @@ -870,10 +882,42 @@ def test_get_objects_generations(self): self.assertTrue( any(l is element for element in gc.get_objects(generation=0)) ) - gc.collect() + self.assertFalse( + any(l is element for element in gc.get_objects(generation=1)) + ) + self.assertFalse( + any(l is element for element in gc.get_objects(generation=2)) + ) + gc.collect(generation=0) + self.assertFalse( + any(l is element for element in gc.get_objects(generation=0)) + ) + self.assertTrue( + any(l is element for element in gc.get_objects(generation=1)) + ) + self.assertFalse( + any(l is element for element in gc.get_objects(generation=2)) + ) + gc.collect(generation=1) + self.assertFalse( + any(l is element for element in gc.get_objects(generation=0)) + ) + self.assertFalse( + any(l is element for element in gc.get_objects(generation=1)) + ) + self.assertTrue( + any(l is element for element in gc.get_objects(generation=2)) + ) + gc.collect(generation=2) self.assertFalse( any(l is element for element in gc.get_objects(generation=0)) ) + self.assertFalse( + any(l is element for element in gc.get_objects(generation=1)) + ) + self.assertTrue( + any(l is element for element in gc.get_objects(generation=2)) + ) del l gc.collect() @@ -1181,17 +1225,6 @@ def test_tuple_untrack_counts(self): self.assertTrue(new_count - count > (n // 2)) -class IncrementalGCTests(unittest.TestCase): - @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") - @requires_gil_enabled("Free threading does not support incremental GC") - def test_incremental_gc_handles_fast_cycle_creation(self): - # Run this test in a fresh process. The number of alive objects (which can - # be from unit tests run before this one) can influence how quickly cyclic - # garbage is found. - script = support.findfile("_test_gc_fast_cycles.py") - run_test_script(script) - - class GCCallbackTests(unittest.TestCase): def setUp(self): # Save gc state and disable it. @@ -1354,7 +1387,7 @@ def test_refcount_errors(self): p.stderr.close() # Verify that stderr has a useful error message: self.assertRegex(stderr, - br'gc.*\.c:[0-9]+: .*: Assertion "gc_get_refs\(.+\) .*" failed.') + br'gc.*\.[ch]:[0-9]+: .*: Assertion "gc_get_refs\(.+\) .*" failed.') self.assertRegex(stderr, br'refcount is too small') # "address : 0x7fb5062efc18" @@ -1567,6 +1600,17 @@ def test_threshold_zero(self): self.assertEqual(i, 50001) + @gc_threshold(50, 1000, 1000) + @requires_gil_enabled("requires tracked-object counting") + def test_untracked_tuples_do_not_trigger_gc(self): + # gh-139951: gen0 counts should reflect tracked GC objects, not + # merely GC-capable allocations like untracked tuples. + gc.collect() + collections = gc.get_stats()[0]["collections"] + items = [(i, i + 1) for i in range(5000)] + self.assertFalse(gc.is_tracked(items[0])) + self.assertEqual(gc.get_stats()[0]["collections"], collections) + class PythonFinalizationTests(unittest.TestCase): def test_ast_fini(self): diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 57b42fbb65fe10..ec25b3969b2d97 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -807,7 +807,12 @@ def test_closefd_attr(self): def test_garbage_collection(self): # FileIO objects are collected, and collecting them flushes # all data to disk. - with warnings_helper.check_warnings(('', ResourceWarning)): + # + # Note that using warnings_helper.check_warnings() will keep the + # file alive due to the `source` argument to warn(). So, use + # catch_warnings() instead. + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ResourceWarning) f = self.FileIO(os_helper.TESTFN, "wb") f.write(b"abcxxx") f.f = f @@ -1824,7 +1829,11 @@ def test_garbage_collection(self): # C BufferedReader objects are collected. # The Python version has __del__, so it ends into gc.garbage instead self.addCleanup(os_helper.unlink, os_helper.TESTFN) - with warnings_helper.check_warnings(('', ResourceWarning)): + # Note that using warnings_helper.check_warnings() will keep the + # file alive due to the `source` argument to warn(). So, use + # catch_warnings() instead. + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ResourceWarning) rawio = self.FileIO(os_helper.TESTFN, "w+b") f = self.tp(rawio) f.f = f @@ -2173,7 +2182,11 @@ def test_garbage_collection(self): # all data to disk. # The Python version has __del__, so it ends into gc.garbage instead self.addCleanup(os_helper.unlink, os_helper.TESTFN) - with warnings_helper.check_warnings(('', ResourceWarning)): + # Note that using warnings_helper.check_warnings() will keep the + # file alive due to the `source` argument to warn(). So, use + # catch_warnings() instead. + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ResourceWarning) rawio = self.FileIO(os_helper.TESTFN, "w+b") f = self.tp(rawio) f.write(b"123xxx") @@ -4113,7 +4126,8 @@ def test_garbage_collection(self): # C TextIOWrapper objects are collected, and collecting them flushes # all data to disk. # The Python version has __del__, so it ends in gc.garbage instead. - with warnings_helper.check_warnings(('', ResourceWarning)): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ResourceWarning) rawio = self.FileIO(os_helper.TESTFN, "wb") b = self.BufferedWriter(rawio) t = self.TextIOWrapper(b, encoding="ascii") diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-17-11-30-00.gh-issue-142516.GcGen314.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-17-11-30-00.gh-issue-142516.GcGen314.rst new file mode 100644 index 00000000000000..d52a3f26683937 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-17-11-30-00.gh-issue-142516.GcGen314.rst @@ -0,0 +1 @@ +Backport the generational cycle garbage collector to the default 3.14 build, replacing the incremental collector while leaving the free-threaded collector unchanged. diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index a09c4856f1166d..239aa2826922ca 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -8,7 +8,6 @@ #include "pycore_gc.h" #include "pycore_object.h" // _PyObject_IS_GC() #include "pycore_pystate.h" // _PyInterpreterState_GET() -#include "pycore_tuple.h" // _PyTuple_FromArray() typedef struct _gc_runtime_state GCState; @@ -159,6 +158,7 @@ gc_set_threshold_impl(PyObject *module, int threshold0, int group_right_1, { GCState *gcstate = get_gc_state(); +#ifdef Py_GIL_DISABLED gcstate->young.threshold = threshold0; if (group_right_1) { gcstate->old[0].threshold = threshold1; @@ -166,6 +166,15 @@ gc_set_threshold_impl(PyObject *module, int threshold0, int group_right_1, if (group_right_2) { gcstate->old[1].threshold = threshold2; } +#else + gcstate->generations[0].threshold = threshold0; + if (group_right_1) { + gcstate->generations[1].threshold = threshold1; + } + if (group_right_2) { + gcstate->generations[2].threshold = threshold2; + } +#endif Py_RETURN_NONE; } @@ -180,10 +189,17 @@ gc_get_threshold_impl(PyObject *module) /*[clinic end generated code: output=7902bc9f41ecbbd8 input=286d79918034d6e6]*/ { GCState *gcstate = get_gc_state(); +#ifdef Py_GIL_DISABLED return Py_BuildValue("(iii)", gcstate->young.threshold, gcstate->old[0].threshold, 0); +#else + return Py_BuildValue("(iii)", + gcstate->generations[0].threshold, + gcstate->generations[1].threshold, + gcstate->generations[2].threshold); +#endif } /*[clinic input] @@ -205,12 +221,17 @@ gc_get_count_impl(PyObject *module) // Flush the local allocation count to the global count _Py_atomic_add_int(&gcstate->young.count, (int)gc->alloc_count); gc->alloc_count = 0; -#endif return Py_BuildValue("(iii)", gcstate->young.count, gcstate->old[gcstate->visited_space].count, gcstate->old[gcstate->visited_space^1].count); +#else + return Py_BuildValue("(iii)", + gcstate->generations[0].count, + gcstate->generations[1].count, + gcstate->generations[2].count); +#endif } /*[clinic input] @@ -347,9 +368,15 @@ gc_get_stats_impl(PyObject *module) /* To get consistent values despite allocations while constructing the result list, we use a snapshot of the running stats. */ GCState *gcstate = get_gc_state(); +#ifdef Py_GIL_DISABLED for (i = 0; i < NUM_GENERATIONS; i++) { stats[i] = gcstate->generation_stats[i]; } +#else + for (i = 0; i < NUM_GENERATIONS; i++) { + stats[i] = gcstate->generation_stats_gen[i]; + } +#endif PyObject *result = PyList_New(0); if (result == NULL) @@ -358,10 +385,12 @@ gc_get_stats_impl(PyObject *module) for (i = 0; i < NUM_GENERATIONS; i++) { PyObject *dict; st = &stats[i]; - dict = Py_BuildValue("{snsnsn}", + dict = Py_BuildValue("{snsnsnsnsd}", "collections", st->collections, "collected", st->collected, - "uncollectable", st->uncollectable + "uncollectable", st->uncollectable, + "candidates", st->candidates, + "duration", st->duration ); if (dict == NULL) goto error; diff --git a/Python/gc.c b/Python/gc.c index c134dc57e28f6d..5bc346e947aa55 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -1,14 +1,18 @@ -// This implements the reference cycle garbage collector. -// The Python module interface to the collector is in gcmodule.c. -// See https://devguide.python.org/internals/garbage-collector/ +// Generational GC specific code, forward-ported from CPython 3.13. #include "Python.h" + +#if !defined(Py_GIL_DISABLED) + #include "pycore_ceval.h" // _Py_set_eval_breaker_bit() #include "pycore_dict.h" // _PyInlineValuesSize() #include "pycore_initconfig.h" // _PyStatus_OK() +#include "pycore_context.h" #include "pycore_interp.h" // PyInterpreterState.gc #include "pycore_interpframe.h" // _PyFrame_GetLocalsArray() +#include "pycore_object.h" #include "pycore_object_alloc.h" // _PyObject_MallocWithType() +#include "pycore_pyerrors.h" #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_tuple.h" // _PyTuple_MaybeUntrack() #include "pycore_weakref.h" // _PyWeakref_ClearRef() @@ -16,16 +20,12 @@ #include "pydtrace.h" -#ifndef Py_GIL_DISABLED - typedef struct _gc_runtime_state GCState; #ifdef Py_DEBUG # define GC_DEBUG #endif -// Define this when debugging the GC -// #define GC_EXTRA_DEBUG #define GC_NEXT _PyGCHead_NEXT @@ -50,7 +50,7 @@ typedef struct _gc_runtime_state GCState; // move_legacy_finalizers() removes this flag instead. // Between them, unreachable list is not normal list and we can not use // most gc_list_* functions for it. -#define NEXT_MASK_UNREACHABLE 2 +/* NEXT_MASK_UNREACHABLE is defined locally in each GC variant */ #define AS_GC(op) _Py_AS_GC(op) #define FROM_GC(gc) _Py_FROM_GC(gc) @@ -100,50 +100,8 @@ gc_decref(PyGC_Head *g) g->_gc_prev -= 1 << _PyGC_PREV_SHIFT; } -static inline int -gc_old_space(PyGC_Head *g) -{ - return g->_gc_next & _PyGC_NEXT_MASK_OLD_SPACE_1; -} - -static inline int -other_space(int space) -{ - assert(space == 0 || space == 1); - return space ^ _PyGC_NEXT_MASK_OLD_SPACE_1; -} - -static inline void -gc_flip_old_space(PyGC_Head *g) -{ - g->_gc_next ^= _PyGC_NEXT_MASK_OLD_SPACE_1; -} - -static inline void -gc_set_old_space(PyGC_Head *g, int space) -{ - assert(space == 0 || space == _PyGC_NEXT_MASK_OLD_SPACE_1); - g->_gc_next &= ~_PyGC_NEXT_MASK_OLD_SPACE_1; - g->_gc_next |= space; -} - -static PyGC_Head * -GEN_HEAD(GCState *gcstate, int n) -{ - assert((gcstate->visited_space & (~1)) == 0); - switch(n) { - case 0: - return &gcstate->young.head; - case 1: - return &gcstate->old[gcstate->visited_space].head; - case 2: - return &gcstate->old[gcstate->visited_space^1].head; - default: - Py_UNREACHABLE(); - } -} -static GCState * +static inline GCState * get_gc_state(void) { PyInterpreterState *interp = _PyInterpreterState_GET(); @@ -151,46 +109,6 @@ get_gc_state(void) } -void -_PyGC_InitState(GCState *gcstate) -{ -#define INIT_HEAD(GEN) \ - do { \ - GEN.head._gc_next = (uintptr_t)&GEN.head; \ - GEN.head._gc_prev = (uintptr_t)&GEN.head; \ - } while (0) - - assert(gcstate->young.count == 0); - assert(gcstate->old[0].count == 0); - assert(gcstate->old[1].count == 0); - INIT_HEAD(gcstate->young); - INIT_HEAD(gcstate->old[0]); - INIT_HEAD(gcstate->old[1]); - INIT_HEAD(gcstate->permanent_generation); - -#undef INIT_HEAD -} - - -PyStatus -_PyGC_Init(PyInterpreterState *interp) -{ - GCState *gcstate = &interp->gc; - - gcstate->garbage = PyList_New(0); - if (gcstate->garbage == NULL) { - return _PyStatus_NO_MEMORY(); - } - - gcstate->callbacks = PyList_New(0); - if (gcstate->callbacks == NULL) { - return _PyStatus_NO_MEMORY(); - } - gcstate->heap_size = 0; - - return _PyStatus_OK(); -} - /* _gc_prev values @@ -293,7 +211,7 @@ gc_list_remove(PyGC_Head *node) * named here) to the end of `list`. This is semantically the same as * gc_list_remove(node) followed by gc_list_append(node, list). */ -static void +static inline void gc_list_move(PyGC_Head *node, PyGC_Head *list) { /* Unlink from current list. */ @@ -312,7 +230,7 @@ gc_list_move(PyGC_Head *node, PyGC_Head *list) } /* append list `from` onto list `to`; `from` becomes an empty list */ -static void +static inline void gc_list_merge(PyGC_Head *from, PyGC_Head *to) { assert(from != to); @@ -322,8 +240,6 @@ gc_list_merge(PyGC_Head *from, PyGC_Head *to) PyGC_Head *from_tail = GC_PREV(from); assert(from_head != from); assert(from_tail != from); - assert(gc_list_is_empty(to) || - gc_old_space(to_tail) == gc_old_space(from_tail)); _PyGCHead_SET_NEXT(to_tail, from_head); _PyGCHead_SET_PREV(from_head, to_tail); @@ -334,7 +250,7 @@ gc_list_merge(PyGC_Head *from, PyGC_Head *to) gc_list_init(from); } -static Py_ssize_t +static inline Py_ssize_t gc_list_size(PyGC_Head *list) { PyGC_Head *gc; @@ -358,7 +274,7 @@ gc_list_clear_collecting(PyGC_Head *collectable) /* Append objects in a GC list to a Python list. * Return 0 if all OK, < 0 if error (out of memory for list) */ -static int +static inline int append_objects(PyObject *py_list, PyGC_Head *gc_list) { PyGC_Head *gc; @@ -379,216 +295,448 @@ enum flagstates {collecting_clear_unreachable_clear, collecting_set_unreachable_clear, collecting_set_unreachable_set}; -#ifdef GC_DEBUG -// validate_list checks list consistency. And it works as document -// describing when flags are expected to be set / unset. -// `head` must be a doubly-linked gc list, although it's fine (expected!) if -// the prev and next pointers are "polluted" with flags. -// What's checked: -// - The `head` pointers are not polluted. -// - The objects' PREV_MASK_COLLECTING and NEXT_MASK_UNREACHABLE flags are all -// `set or clear, as specified by the 'flags' argument. -// - The prev and next pointers are mutually consistent. -static void -validate_list(PyGC_Head *head, enum flagstates flags) -{ - assert((head->_gc_prev & ~_PyGC_PREV_MASK) == 0); - assert((head->_gc_next & ~_PyGC_PREV_MASK) == 0); - uintptr_t prev_value = 0, next_value = 0; - switch (flags) { - case collecting_clear_unreachable_clear: - break; - case collecting_set_unreachable_clear: - prev_value = PREV_MASK_COLLECTING; - break; - case collecting_clear_unreachable_set: - next_value = NEXT_MASK_UNREACHABLE; - break; - case collecting_set_unreachable_set: - prev_value = PREV_MASK_COLLECTING; - next_value = NEXT_MASK_UNREACHABLE; - break; - default: - assert(! "bad internal flags argument"); - } - PyGC_Head *prev = head; - PyGC_Head *gc = GC_NEXT(head); - while (gc != head) { - PyGC_Head *trueprev = GC_PREV(gc); - PyGC_Head *truenext = GC_NEXT(gc); - assert(truenext != NULL); - assert(trueprev == prev); - assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value); - assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value); - prev = gc; - gc = truenext; - } - assert(prev == GC_PREV(head)); -} - -#else -#define validate_list(x, y) do{}while(0) -#endif - -#ifdef GC_EXTRA_DEBUG +/*** end of list stuff ***/ -static void -gc_list_validate_space(PyGC_Head *head, int space) { - PyGC_Head *gc = GC_NEXT(head); - while (gc != head) { - assert(gc_old_space(gc) == space); - gc = GC_NEXT(gc); - } -} -static void -validate_spaces(GCState *gcstate) -{ - int visited = gcstate->visited_space; - int not_visited = other_space(visited); - gc_list_validate_space(&gcstate->young.head, not_visited); - for (int space = 0; space < 2; space++) { - gc_list_validate_space(&gcstate->old[space].head, space); - } - gc_list_validate_space(&gcstate->permanent_generation.head, visited); -} +/* A traversal callback for subtract_refs. + * + * This function must have external linkage (not static) because + * _PyGC_VisitStackRef compares function pointers against it. + * If it were static in a shared header, each compilation unit would + * get a different copy with a different address, breaking the comparison. + */ +int +_PyGC_VisitDecref(PyObject *op, void *parent); -static void -validate_consistent_old_space(PyGC_Head *head) +/* Subtract internal references from gc_refs. After this, gc_refs is >= 0 + * for all objects in containers, and is GC_REACHABLE for all tracked gc + * objects not in containers. The ones with gc_refs > 0 are directly + * reachable from outside containers, and so can't be collected. + */ +static inline void +subtract_refs(PyGC_Head *containers) { - PyGC_Head *gc = GC_NEXT(head); - if (gc == head) { - return; - } - int old_space = gc_old_space(gc); - while (gc != head) { - PyGC_Head *truenext = GC_NEXT(gc); - assert(truenext != NULL); - assert(gc_old_space(gc) == old_space); - gc = truenext; + traverseproc traverse; + PyGC_Head *gc = GC_NEXT(containers); + for (; gc != containers; gc = GC_NEXT(gc)) { + PyObject *op = FROM_GC(gc); + traverse = Py_TYPE(op)->tp_traverse; + (void) traverse(op, + _PyGC_VisitDecref, + op); } } -#else -#define validate_spaces(g) do{}while(0) -#define validate_consistent_old_space(l) do{}while(0) -#define gc_list_validate_space(l, s) do{}while(0) -#endif - -/*** end of list stuff ***/ - -/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and - * PREV_MASK_COLLECTING bit is set for all objects in containers. - */ -static void -update_refs(PyGC_Head *containers) +/* In theory, all tuples should be younger than the +* objects they refer to, as tuples are immortal. +* Therefore, untracking tuples in oldest-first order in the +* young generation before promoting them should have tracked +* all the tuples that can be untracked. +* +* Unfortunately, the C API allows tuples to be created +* and then filled in. So this won't untrack all tuples +* that can be untracked. It should untrack most of them +* and is much faster than a more complex approach that +* would untrack all relevant tuples. +*/ +static inline void +untrack_tuples(PyGC_Head *head) { - PyGC_Head *next; - PyGC_Head *gc = GC_NEXT(containers); - - while (gc != containers) { - next = GC_NEXT(gc); + PyGC_Head *gc = GC_NEXT(head); + while (gc != head) { PyObject *op = FROM_GC(gc); - if (_Py_IsImmortal(op)) { - assert(!_Py_IsStaticImmortal(op)); - _PyObject_GC_UNTRACK(op); - gc = next; - continue; + PyGC_Head *next = GC_NEXT(gc); + if (PyTuple_CheckExact(op)) { + _PyTuple_MaybeUntrack(op); } - gc_reset_refs(gc, Py_REFCNT(op)); - /* Python's cyclic gc should never see an incoming refcount - * of 0: if something decref'ed to 0, it should have been - * deallocated immediately at that time. - * Possible cause (if the assert triggers): a tp_dealloc - * routine left a gc-aware object tracked during its teardown - * phase, and did something-- or allowed something to happen -- - * that called back into Python. gc can trigger then, and may - * see the still-tracked dying object. Before this assert - * was added, such mistakes went on to allow gc to try to - * delete the object again. In a debug build, that caused - * a mysterious segfault, when _Py_ForgetReference tried - * to remove the object from the doubly-linked list of all - * objects a second time. In a release build, an actual - * double deallocation occurred, which leads to corruption - * of the allocator's internal bookkeeping pointers. That's - * so serious that maybe this should be a release-build - * check instead of an assert? - */ - _PyObject_ASSERT(op, gc_get_refs(gc) != 0); gc = next; } } -/* A traversal callback for subtract_refs. */ -static int -visit_decref(PyObject *op, void *parent) +/* Return true if object has a pre-PEP 442 finalization method. */ +static inline int +has_legacy_finalizer(PyObject *op) { - OBJECT_STAT_INC(object_visits); - _PyObject_ASSERT(_PyObject_CAST(parent), !_PyObject_IsFreed(op)); + return Py_TYPE(op)->tp_del != NULL; +} + +/* A traversal callback for move_legacy_finalizer_reachable. */ +static inline int +visit_move(PyObject *op, void *arg) +{ + PyGC_Head *tolist = arg; + OBJECT_STAT_INC(object_visits); if (_PyObject_IS_GC(op)) { PyGC_Head *gc = AS_GC(op); - /* We're only interested in gc_refs for objects in the - * generation being collected, which can be recognized - * because only they have positive gc_refs. - */ if (gc_is_collecting(gc)) { - gc_decref(gc); + gc_list_move(gc, tolist); + gc_clear_collecting(gc); } } return 0; } -int -_PyGC_VisitStackRef(_PyStackRef *ref, visitproc visit, void *arg) +/* Move objects that are reachable from finalizers, from the unreachable set + * into finalizers set. + */ +static inline void +move_legacy_finalizer_reachable(PyGC_Head *finalizers) { - // This is a bit tricky! We want to ignore stackrefs with embedded - // refcounts when computing the incoming references, but otherwise treat - // them like normal. - assert(!PyStackRef_IsTaggedInt(*ref)); - if (!PyStackRef_RefcountOnObject(*ref) && (visit == visit_decref)) { - return 0; + traverseproc traverse; + PyGC_Head *gc = GC_NEXT(finalizers); + for (; gc != finalizers; gc = GC_NEXT(gc)) { + /* Note that the finalizers list may grow during this. */ + traverse = Py_TYPE(FROM_GC(gc))->tp_traverse; + (void) traverse(FROM_GC(gc), + visit_move, + (void *)finalizers); } - Py_VISIT(PyStackRef_AsPyObjectBorrow(*ref)); - return 0; } -int -_PyGC_VisitFrameStack(_PyInterpreterFrame *frame, visitproc visit, void *arg) +static inline void +debug_cycle(const char *msg, PyObject *op) { - _PyStackRef *ref = _PyFrame_GetLocalsArray(frame); - /* locals and stack */ - for (; ref < frame->stackpointer; ref++) { - if (!PyStackRef_IsTaggedInt(*ref)) { - _Py_VISIT_STACKREF(*ref); - } - } - return 0; + PySys_FormatStderr("gc: %s <%s %p>\n", + msg, Py_TYPE(op)->tp_name, op); } -/* Subtract internal references from gc_refs. After this, gc_refs is >= 0 - * for all objects in containers, and is GC_REACHABLE for all tracked gc - * objects not in containers. The ones with gc_refs > 0 are directly - * reachable from outside containers, and so can't be collected. +/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable + * only from such cycles). + * If _PyGC_DEBUG_SAVEALL, all objects in finalizers are appended to the module + * garbage list (a Python list), else only the objects in finalizers with + * __del__ methods are appended to garbage. All objects in finalizers are + * merged into the old list regardless. */ -static void -subtract_refs(PyGC_Head *containers) +static inline void +handle_legacy_finalizers(PyThreadState *tstate, + GCState *gcstate, + PyGC_Head *finalizers, PyGC_Head *old) { - traverseproc traverse; - PyGC_Head *gc = GC_NEXT(containers); - for (; gc != containers; gc = GC_NEXT(gc)) { + assert(!_PyErr_Occurred(tstate)); + assert(gcstate->garbage != NULL); + + PyGC_Head *gc = GC_NEXT(finalizers); + for (; gc != finalizers; gc = GC_NEXT(gc)) { PyObject *op = FROM_GC(gc); - traverse = Py_TYPE(op)->tp_traverse; - (void) traverse(op, - visit_decref, - op); + + if ((gcstate->debug & _PyGC_DEBUG_SAVEALL) || has_legacy_finalizer(op)) { + if (PyList_Append(gcstate->garbage, op) < 0) { + _PyErr_Clear(tstate); + break; + } + } + } + + gc_list_merge(finalizers, old); +} + +/* Run first-time finalizers (if any) on all the objects in collectable. + * Note that this may remove some (or even all) of the objects from the + * list, due to refcounts falling to 0. + */ +static inline void +finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable) +{ + destructor finalize; + PyGC_Head seen; + + /* While we're going through the loop, `finalize(op)` may cause op, or + * other objects, to be reclaimed via refcounts falling to zero. So + * there's little we can rely on about the structure of the input + * `collectable` list across iterations. For safety, we always take the + * first object in that list and move it to a temporary `seen` list. + * If objects vanish from the `collectable` and `seen` lists we don't + * care. + */ + gc_list_init(&seen); + + while (!gc_list_is_empty(collectable)) { + PyGC_Head *gc = GC_NEXT(collectable); + PyObject *op = FROM_GC(gc); + gc_list_move(gc, &seen); + if (!_PyGC_FINALIZED(op) && + (finalize = Py_TYPE(op)->tp_finalize) != NULL) + { + _PyGC_SET_FINALIZED(op); + Py_INCREF(op); + finalize(op); + assert(!_PyErr_Occurred(tstate)); + Py_DECREF(op); + } + } + gc_list_merge(&seen, collectable); +} + +/* Break reference cycles by clearing the containers involved. This is + * tricky business as the lists can be changing and we don't know which + * objects may be freed. It is possible I screwed something up here. + */ +static inline void +delete_garbage(PyThreadState *tstate, GCState *gcstate, + PyGC_Head *collectable, PyGC_Head *old) +{ + assert(!_PyErr_Occurred(tstate)); + + while (!gc_list_is_empty(collectable)) { + PyGC_Head *gc = GC_NEXT(collectable); + PyObject *op = FROM_GC(gc); + + _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0, + "refcount is too small"); + + if (gcstate->debug & _PyGC_DEBUG_SAVEALL) { + assert(gcstate->garbage != NULL); + if (PyList_Append(gcstate->garbage, op) < 0) { + _PyErr_Clear(tstate); + } + } + else { + inquiry clear; + if ((clear = Py_TYPE(op)->tp_clear) != NULL) { + Py_INCREF(op); + (void) clear(op); + if (_PyErr_Occurred(tstate)) { + PyErr_FormatUnraisable("Exception ignored in tp_clear of %s", + Py_TYPE(op)->tp_name); + } + Py_DECREF(op); + } + } + if (GC_NEXT(collectable) == gc) { + /* object is still alive, move it, it may die later */ + gc_clear_collecting(gc); + gc_list_move(gc, old); + } + } +} + + + +static inline int +referrersvisit(PyObject* obj, void *arg) +{ + PyObject *objs = arg; + Py_ssize_t i; + for (i = 0; i < PyTuple_GET_SIZE(objs); i++) { + if (PyTuple_GET_ITEM(objs, i) == obj) { + return 1; + } + } + return 0; +} + +static inline int +gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist) +{ + PyGC_Head *gc; + PyObject *obj; + traverseproc traverse; + for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) { + obj = FROM_GC(gc); + traverse = Py_TYPE(obj)->tp_traverse; + if (obj == objs || obj == resultlist) { + continue; + } + if (traverse(obj, referrersvisit, objs)) { + if (PyList_Append(resultlist, obj) < 0) { + return 0; /* error */ + } + } + } + return 1; /* no error */ +} + + +static inline void +finalize_unlink_gc_head(PyGC_Head *gc) { + PyGC_Head *prev = GC_PREV(gc); + PyGC_Head *next = GC_NEXT(gc); + _PyGCHead_SET_NEXT(prev, next); + _PyGCHead_SET_PREV(next, prev); +} + + +static inline int +visit_generation(gcvisitobjects_t callback, void *arg, struct gc_generation *gen) +{ + PyGC_Head *gc_list, *gc; + gc_list = &gen->head; + for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) { + PyObject *op = FROM_GC(gc); + Py_INCREF(op); + int res = callback(op, arg); + Py_DECREF(op); + if (!res) { + return -1; + } + } + return 0; +} + +#define NEXT_MASK_UNREACHABLE (1) + +#define GEN_HEAD(gcstate, n) (&(gcstate)->generations[n].head) + + +void +_PyGC_InitState(GCState *gcstate) +{ +#define INIT_HEAD(GEN) \ + do { \ + GEN.head._gc_next = (uintptr_t)&GEN.head; \ + GEN.head._gc_prev = (uintptr_t)&GEN.head; \ + } while (0) + + for (int i = 0; i < NUM_GENERATIONS; i++) { + assert(gcstate->generations[i].count == 0); + INIT_HEAD(gcstate->generations[i]); + memset(&gcstate->generation_stats_gen[i], 0, + sizeof(gcstate->generation_stats_gen[i])); + }; + gcstate->generation0 = GEN_HEAD(gcstate, 0); + gcstate->long_lived_total = 0; + gcstate->long_lived_pending = 0; + gcstate->frame = NULL; + INIT_HEAD(gcstate->permanent_generation); + +#undef INIT_HEAD +} + + +PyStatus +_PyGC_Init(PyInterpreterState *interp) +{ + GCState *gcstate = &interp->gc; + + gcstate->garbage = PyList_New(0); + if (gcstate->garbage == NULL) { + return _PyStatus_NO_MEMORY(); + } + + gcstate->callbacks = PyList_New(0); + if (gcstate->callbacks == NULL) { + return _PyStatus_NO_MEMORY(); + } + + return _PyStatus_OK(); +} + + + +#ifdef GC_DEBUG +static void +validate_list(PyGC_Head *head, enum flagstates flags) +{ + assert((head->_gc_prev & ~_PyGC_PREV_MASK) == 0); + assert((head->_gc_next & ~_PyGC_PREV_MASK) == 0); + uintptr_t prev_value = 0, next_value = 0; + switch (flags) { + case collecting_clear_unreachable_clear: + break; + case collecting_set_unreachable_clear: + prev_value = PREV_MASK_COLLECTING; + break; + case collecting_clear_unreachable_set: + next_value = NEXT_MASK_UNREACHABLE; + break; + case collecting_set_unreachable_set: + prev_value = PREV_MASK_COLLECTING; + next_value = NEXT_MASK_UNREACHABLE; + break; + default: + assert(! "bad internal flags argument"); + } + PyGC_Head *prev = head; + PyGC_Head *gc = GC_NEXT(head); + while (gc != head) { + PyGC_Head *trueprev = GC_PREV(gc); + PyGC_Head *truenext = (PyGC_Head *)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE); + assert(truenext != NULL); + assert(trueprev == prev); + assert((gc->_gc_prev & PREV_MASK_COLLECTING) == prev_value); + assert((gc->_gc_next & NEXT_MASK_UNREACHABLE) == next_value); + prev = gc; + gc = truenext; } + assert(prev == GC_PREV(head)); +} +#else +#define validate_list(x, y) do{}while(0) +#endif + +/*** end of list stuff ***/ + + +/* Set all gc_refs = ob_refcnt. After this, gc_refs is > 0 and + * PREV_MASK_COLLECTING bit is set for all objects in containers. + */ +static Py_ssize_t +update_refs(PyGC_Head *containers) +{ + PyGC_Head *next; + PyGC_Head *gc = GC_NEXT(containers); + Py_ssize_t candidates = 0; + + while (gc != containers) { + next = GC_NEXT(gc); + PyObject *op = FROM_GC(gc); + if (_Py_IsImmortal(op)) { + assert(!_Py_IsStaticImmortal(op)); + _PyObject_GC_UNTRACK(op); + gc = next; + continue; + } + gc_reset_refs(gc, Py_REFCNT(op)); + _PyObject_ASSERT(op, gc_get_refs(gc) != 0); + gc = next; + candidates++; + } + return candidates; +} + +int +_PyGC_VisitDecref(PyObject *op, void *parent) +{ + OBJECT_STAT_INC(object_visits); + _PyObject_ASSERT(parent, !_PyObject_IsFreed(op)); + + if (_PyObject_IS_GC(op)) { + PyGC_Head *gc = AS_GC(op); + if (gc_is_collecting(gc)) { + gc_decref(gc); + } + } + return 0; } -/* A traversal callback for move_unreachable. */ +int +_PyGC_VisitStackRef(_PyStackRef *ref, visitproc visit, void *arg) +{ + assert(!PyStackRef_IsTaggedInt(*ref)); + if (!PyStackRef_RefcountOnObject(*ref) && (visit == _PyGC_VisitDecref)) { + return 0; + } + Py_VISIT(PyStackRef_AsPyObjectBorrow(*ref)); + return 0; +} + +int +_PyGC_VisitFrameStack(_PyInterpreterFrame *frame, visitproc visit, void *arg) +{ + _PyStackRef *ref = _PyFrame_GetLocalsArray(frame); + for (; ref < frame->stackpointer; ref++) { + if (!PyStackRef_IsTaggedInt(*ref)) { + _Py_VISIT_STACKREF(*ref); + } + } + return 0; +} + +/* A traversal callback for subtract_refs. */ static int visit_reachable(PyObject *op, void *arg) { @@ -601,33 +749,19 @@ visit_reachable(PyObject *op, void *arg) PyGC_Head *gc = AS_GC(op); const Py_ssize_t gc_refs = gc_get_refs(gc); - // Ignore objects in other generation. - // This also skips objects "to the left" of the current position in - // move_unreachable's scan of the 'young' list - they've already been - // traversed, and no longer have the PREV_MASK_COLLECTING flag. if (! gc_is_collecting(gc)) { return 0; } - // It would be a logic error elsewhere if the collecting flag were set on - // an untracked object. _PyObject_ASSERT(op, gc->_gc_next != 0); if (gc->_gc_next & NEXT_MASK_UNREACHABLE) { - /* This had gc_refs = 0 when move_unreachable got - * to it, but turns out it's reachable after all. - * Move it back to move_unreachable's 'young' list, - * and move_unreachable will eventually get to it - * again. - */ - // Manually unlink gc from unreachable list because the list functions - // don't work right in the presence of NEXT_MASK_UNREACHABLE flags. PyGC_Head *prev = GC_PREV(gc); - PyGC_Head *next = GC_NEXT(gc); + PyGC_Head *next = (PyGC_Head*)(gc->_gc_next & ~NEXT_MASK_UNREACHABLE); _PyObject_ASSERT(FROM_GC(prev), prev->_gc_next & NEXT_MASK_UNREACHABLE); _PyObject_ASSERT(FROM_GC(next), next->_gc_next & NEXT_MASK_UNREACHABLE); - prev->_gc_next = gc->_gc_next; // copy flag bits + prev->_gc_next = gc->_gc_next; // copy NEXT_MASK_UNREACHABLE gc->_gc_next &= ~NEXT_MASK_UNREACHABLE; _PyGCHead_SET_PREV(next, prev); @@ -635,151 +769,48 @@ visit_reachable(PyObject *op, void *arg) gc_set_refs(gc, 1); } else if (gc_refs == 0) { - /* This is in move_unreachable's 'young' list, but - * the traversal hasn't yet gotten to it. All - * we need to do is tell move_unreachable that it's - * reachable. - */ gc_set_refs(gc, 1); } - /* Else there's nothing to do. - * If gc_refs > 0, it must be in move_unreachable's 'young' - * list, and move_unreachable will eventually get to it. - */ else { _PyObject_ASSERT_WITH_MSG(op, gc_refs > 0, "refcount is too small"); } return 0; } -/* Move the unreachable objects from young to unreachable. After this, - * all objects in young don't have PREV_MASK_COLLECTING flag and - * unreachable have the flag. - * All objects in young after this are directly or indirectly reachable - * from outside the original young; and all objects in unreachable are - * not. - * - * This function restores _gc_prev pointer. young and unreachable are - * doubly linked list after this function. - * But _gc_next in unreachable list has NEXT_MASK_UNREACHABLE flag. - * So we can not gc_list_* functions for unreachable until we remove the flag. - */ static void move_unreachable(PyGC_Head *young, PyGC_Head *unreachable) { - // previous elem in the young list, used for restore gc_prev. PyGC_Head *prev = young; PyGC_Head *gc = GC_NEXT(young); - /* Invariants: all objects "to the left" of us in young are reachable - * (directly or indirectly) from outside the young list as it was at entry. - * - * All other objects from the original young "to the left" of us are in - * unreachable now, and have NEXT_MASK_UNREACHABLE. All objects to the - * left of us in 'young' now have been scanned, and no objects here - * or to the right have been scanned yet. - */ - - validate_consistent_old_space(young); - /* Record which old space we are in, and set NEXT_MASK_UNREACHABLE bit for convenience */ - uintptr_t flags = NEXT_MASK_UNREACHABLE | (gc->_gc_next & _PyGC_NEXT_MASK_OLD_SPACE_1); while (gc != young) { if (gc_get_refs(gc)) { - /* gc is definitely reachable from outside the - * original 'young'. Mark it as such, and traverse - * its pointers to find any other objects that may - * be directly reachable from it. Note that the - * call to tp_traverse may append objects to young, - * so we have to wait until it returns to determine - * the next object to visit. - */ PyObject *op = FROM_GC(gc); traverseproc traverse = Py_TYPE(op)->tp_traverse; _PyObject_ASSERT_WITH_MSG(op, gc_get_refs(gc) > 0, "refcount is too small"); - // NOTE: visit_reachable may change gc->_gc_next when - // young->_gc_prev == gc. Don't do gc = GC_NEXT(gc) before! (void) traverse(op, visit_reachable, (void *)young); - // relink gc_prev to prev element. _PyGCHead_SET_PREV(gc, prev); - // gc is not COLLECTING state after here. gc_clear_collecting(gc); prev = gc; } else { - /* This *may* be unreachable. To make progress, - * assume it is. gc isn't directly reachable from - * any object we've already traversed, but may be - * reachable from an object we haven't gotten to yet. - * visit_reachable will eventually move gc back into - * young if that's so, and we'll see it again. - */ - // Move gc to unreachable. - // No need to gc->next->prev = prev because it is single linked. prev->_gc_next = gc->_gc_next; - // We can't use gc_list_append() here because we use - // NEXT_MASK_UNREACHABLE here. PyGC_Head *last = GC_PREV(unreachable); - // NOTE: Since all objects in unreachable set has - // NEXT_MASK_UNREACHABLE flag, we set it unconditionally. - // But this may pollute the unreachable list head's 'next' pointer - // too. That's semantically senseless but expedient here - the - // damage is repaired when this function ends. - last->_gc_next = flags | (uintptr_t)gc; + last->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)gc); _PyGCHead_SET_PREV(gc, last); - gc->_gc_next = flags | (uintptr_t)unreachable; + gc->_gc_next = (NEXT_MASK_UNREACHABLE | (uintptr_t)unreachable); unreachable->_gc_prev = (uintptr_t)gc; } - gc = _PyGCHead_NEXT(prev); + gc = (PyGC_Head*)prev->_gc_next; } - // young->_gc_prev must be last element remained in the list. young->_gc_prev = (uintptr_t)prev; - young->_gc_next &= _PyGC_PREV_MASK; - // don't let the pollution of the list head's next pointer leak - unreachable->_gc_next &= _PyGC_PREV_MASK; + unreachable->_gc_next &= ~NEXT_MASK_UNREACHABLE; } -/* In theory, all tuples should be younger than the -* objects they refer to, as tuples are immortal. -* Therefore, untracking tuples in oldest-first order in the -* young generation before promoting them should have tracked -* all the tuples that can be untracked. -* -* Unfortunately, the C API allows tuples to be created -* and then filled in. So this won't untrack all tuples -* that can be untracked. It should untrack most of them -* and is much faster than a more complex approach that -* would untrack all relevant tuples. -*/ -static void -untrack_tuples(PyGC_Head *head) -{ - PyGC_Head *gc = GC_NEXT(head); - while (gc != head) { - PyObject *op = FROM_GC(gc); - PyGC_Head *next = GC_NEXT(gc); - if (PyTuple_CheckExact(op)) { - _PyTuple_MaybeUntrack(op); - } - gc = next; - } -} - -/* Return true if object has a pre-PEP 442 finalization method. */ -static int -has_legacy_finalizer(PyObject *op) -{ - return Py_TYPE(op)->tp_del != NULL; -} - -/* Move the objects in unreachable with tp_del slots into `finalizers`. - * - * This function also removes NEXT_MASK_UNREACHABLE flag - * from _gc_next in unreachable. - */ static void move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) { @@ -788,15 +819,12 @@ move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) FROM_GC(unreachable), (unreachable->_gc_next & NEXT_MASK_UNREACHABLE) == 0); - /* March over unreachable. Move objects with finalizers into - * `finalizers`. - */ for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) { PyObject *op = FROM_GC(gc); _PyObject_ASSERT(op, gc->_gc_next & NEXT_MASK_UNREACHABLE); - next = GC_NEXT(gc); gc->_gc_next &= ~NEXT_MASK_UNREACHABLE; + next = (PyGC_Head*)gc->_gc_next; if (has_legacy_finalizer(op)) { gc_clear_collecting(gc); @@ -808,7 +836,6 @@ move_legacy_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) static inline void clear_unreachable_mask(PyGC_Head *unreachable) { - /* Check that the list head does not have the unreachable bit set */ _PyObject_ASSERT( FROM_GC(unreachable), ((uintptr_t)unreachable & NEXT_MASK_UNREACHABLE) == 0); @@ -819,929 +846,353 @@ clear_unreachable_mask(PyGC_Head *unreachable) PyGC_Head *gc, *next; for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) { _PyObject_ASSERT((PyObject*)FROM_GC(gc), gc->_gc_next & NEXT_MASK_UNREACHABLE); - next = GC_NEXT(gc); gc->_gc_next &= ~NEXT_MASK_UNREACHABLE; + next = (PyGC_Head*)gc->_gc_next; } validate_list(unreachable, collecting_set_unreachable_clear); } -/* A traversal callback for move_legacy_finalizer_reachable. */ -static int -visit_move(PyObject *op, void *arg) -{ - PyGC_Head *tolist = arg; - OBJECT_STAT_INC(object_visits); - if (_PyObject_IS_GC(op)) { - PyGC_Head *gc = AS_GC(op); - if (gc_is_collecting(gc)) { - gc_list_move(gc, tolist); - gc_clear_collecting(gc); - } - } - return 0; -} - -/* Move objects that are reachable from finalizers, from the unreachable set - * into finalizers set. - */ -static void -move_legacy_finalizer_reachable(PyGC_Head *finalizers) -{ - traverseproc traverse; - PyGC_Head *gc = GC_NEXT(finalizers); - for (; gc != finalizers; gc = GC_NEXT(gc)) { - /* Note that the finalizers list may grow during this. */ - traverse = Py_TYPE(FROM_GC(gc))->tp_traverse; - (void) traverse(FROM_GC(gc), - visit_move, - (void *)finalizers); - } -} - -/* Clear all weakrefs to unreachable objects, and if such a weakref has a - * callback, invoke it if necessary. Note that it's possible for such - * weakrefs to be outside the unreachable set -- indeed, those are precisely - * the weakrefs whose callbacks must be invoked. See gc_weakref.txt for - * overview & some details. Some weakrefs with callbacks may be reclaimed - * directly by this routine; the number reclaimed is the return value. Other - * weakrefs with callbacks may be moved into the `old` generation. Objects - * moved into `old` have gc_refs set to GC_REACHABLE; the objects remaining in - * unreachable are left at GC_TENTATIVELY_UNREACHABLE. When this returns, - * no object in `unreachable` is weakly referenced anymore. +/* Handle weakref callbacks. Weakrefs without callbacks are cleared later, + * after finalizers have run but before tp_clear() executes on the remaining + * unreachable objects. */ static int -handle_weakrefs(PyGC_Head *unreachable, PyGC_Head *old, bool allow_callbacks) +handle_weakref_callbacks(PyGC_Head *unreachable, PyGC_Head *old) { PyGC_Head *gc; - PyObject *op; /* generally FROM_GC(gc) */ - PyWeakReference *wr; /* generally a cast of op */ - PyGC_Head wrcb_to_call; /* weakrefs with callbacks to call */ + PyGC_Head wrcb_to_call; PyGC_Head *next; int num_freed = 0; - if (allow_callbacks) { - gc_list_init(&wrcb_to_call); - } + gc_list_init(&wrcb_to_call); - /* Clear all weakrefs to the objects in unreachable. If such a weakref - * also has a callback, move it into `wrcb_to_call` if the callback - * needs to be invoked. Note that we cannot invoke any callbacks until - * all weakrefs to unreachable objects are cleared, lest the callback - * resurrect an unreachable object via a still-active weakref. We - * make another pass over wrcb_to_call, invoking callbacks, after this - * pass completes. - */ for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) { PyWeakReference **wrlist; - op = FROM_GC(gc); + PyObject *op = FROM_GC(gc); next = GC_NEXT(gc); - if (PyWeakref_Check(op)) { - /* A weakref inside the unreachable set must be cleared. If we - * allow its callback to execute inside delete_garbage(), it - * could expose objects that have tp_clear already called on - * them. Or, it could resurrect unreachable objects. One way - * this can happen is if some container objects do not implement - * tp_traverse. Then, wr_object can be outside the unreachable - * set but can be deallocated as a result of breaking the - * reference cycle. If we don't clear the weakref, the callback - * will run and potentially cause a crash. See bpo-38006 for - * one example. - */ - _PyWeakref_ClearRef((PyWeakReference *)op); - } - if (! _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) { continue; } - /* It supports weakrefs. Does it have any? - * - * This is never triggered for static types so we can avoid the - * (slightly) more costly _PyObject_GET_WEAKREFS_LISTPTR(). - */ wrlist = _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(op); - /* `op` may have some weakrefs. March over the list, clear - * all the weakrefs, and move the weakrefs with callbacks - * that must be called into wrcb_to_call. - */ - for (wr = *wrlist; wr != NULL; wr = *wrlist) { - PyGC_Head *wrasgc; /* AS_GC(wr) */ - - /* _PyWeakref_ClearRef clears the weakref but leaves - * the callback pointer intact. Obscure: it also - * changes *wrlist. - */ - _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op); - _PyWeakref_ClearRef(wr); - _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None); - - if (!allow_callbacks) { - continue; - } + PyWeakReference *next_wr; + for (PyWeakReference *wr = *wrlist; wr != NULL; wr = next_wr) { + next_wr = wr->wr_next; if (wr->wr_callback == NULL) { - /* no callback */ continue; } - /* Headache time. `op` is going away, and is weakly referenced by - * `wr`, which has a callback. Should the callback be invoked? If wr - * is also trash, no: - * - * 1. There's no need to call it. The object and the weakref are - * both going away, so it's legitimate to pretend the weakref is - * going away first. The user has to ensure a weakref outlives its - * referent if they want a guarantee that the wr callback will get - * invoked. - * - * 2. It may be catastrophic to call it. If the callback is also in - * cyclic trash (CT), then although the CT is unreachable from - * outside the current generation, CT may be reachable from the - * callback. Then the callback could resurrect insane objects. - * - * Since the callback is never needed and may be unsafe in this case, - * wr is simply left in the unreachable set. Note that because we - * already called _PyWeakref_ClearRef(wr), its callback will never - * trigger. - * - * OTOH, if wr isn't part of CT, we should invoke the callback: the - * weakref outlived the trash. Note that since wr isn't CT in this - * case, its callback can't be CT either -- wr acted as an external - * root to this generation, and therefore its callback did too. So - * nothing in CT is reachable from the callback either, so it's hard - * to imagine how calling it later could create a problem for us. wr - * is moved to wrcb_to_call in this case. - */ + _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op); + _PyWeakref_ClearRef(wr); + _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None); + if (gc_is_collecting(AS_GC((PyObject *)wr))) { - /* it should already have been cleared above */ - _PyObject_ASSERT((PyObject*)wr, wr->wr_object == Py_None); continue; } - /* Create a new reference so that wr can't go away - * before we can process it again. - */ Py_INCREF(wr); - /* Move wr to wrcb_to_call, for the next pass. */ - wrasgc = AS_GC((PyObject *)wr); - // wrasgc is reachable, but next isn't, so they can't be the same + PyGC_Head *wrasgc = AS_GC((PyObject *)wr); _PyObject_ASSERT((PyObject *)wr, wrasgc != next); - gc_list_move(wrasgc, &wrcb_to_call); - } - } - - if (!allow_callbacks) { - return 0; - } - - /* Invoke the callbacks we decided to honor. It's safe to invoke them - * because they can't reference unreachable objects. - */ - int visited_space = get_gc_state()->visited_space; - while (! gc_list_is_empty(&wrcb_to_call)) { - PyObject *temp; - PyObject *callback; - - gc = (PyGC_Head*)wrcb_to_call._gc_next; - op = FROM_GC(gc); - _PyObject_ASSERT(op, PyWeakref_Check(op)); - wr = (PyWeakReference *)op; - callback = wr->wr_callback; - _PyObject_ASSERT(op, callback != NULL); - - /* copy-paste of weakrefobject.c's handle_callback() */ - temp = PyObject_CallOneArg(callback, (PyObject *)wr); - if (temp == NULL) { - PyErr_FormatUnraisable("Exception ignored on " - "calling weakref callback %R", callback); - } - else { - Py_DECREF(temp); - } - - /* Give up the reference we created in the first pass. When - * op's refcount hits 0 (which it may or may not do right now), - * op's tp_dealloc will decref op->wr_callback too. Note - * that the refcount probably will hit 0 now, and because this - * weakref was reachable to begin with, gc didn't already - * add it to its count of freed objects. Example: a reachable - * weak value dict maps some key to this reachable weakref. - * The callback removes this key->weakref mapping from the - * dict, leaving no other references to the weakref (excepting - * ours). - */ - Py_DECREF(op); - if (wrcb_to_call._gc_next == (uintptr_t)gc) { - /* object is still alive -- move it */ - gc_set_old_space(gc, visited_space); - gc_list_move(gc, old); - } - else { - ++num_freed; - } - } - - return num_freed; -} - -static void -debug_cycle(const char *msg, PyObject *op) -{ - PySys_FormatStderr("gc: %s <%s %p>\n", - msg, Py_TYPE(op)->tp_name, op); -} - -/* Handle uncollectable garbage (cycles with tp_del slots, and stuff reachable - * only from such cycles). - * If _PyGC_DEBUG_SAVEALL, all objects in finalizers are appended to the module - * garbage list (a Python list), else only the objects in finalizers with - * __del__ methods are appended to garbage. All objects in finalizers are - * merged into the old list regardless. - */ -static void -handle_legacy_finalizers(PyThreadState *tstate, - GCState *gcstate, - PyGC_Head *finalizers, PyGC_Head *old) -{ - assert(!_PyErr_Occurred(tstate)); - assert(gcstate->garbage != NULL); - - PyGC_Head *gc = GC_NEXT(finalizers); - for (; gc != finalizers; gc = GC_NEXT(gc)) { - PyObject *op = FROM_GC(gc); - - if ((gcstate->debug & _PyGC_DEBUG_SAVEALL) || has_legacy_finalizer(op)) { - if (PyList_Append(gcstate->garbage, op) < 0) { - _PyErr_Clear(tstate); - break; - } + gc_list_move(wrasgc, &wrcb_to_call); } } - gc_list_merge(finalizers, old); -} + while (!gc_list_is_empty(&wrcb_to_call)) { + PyObject *temp; + PyObject *callback; -/* Run first-time finalizers (if any) on all the objects in collectable. - * Note that this may remove some (or even all) of the objects from the - * list, due to refcounts falling to 0. - */ -static void -finalize_garbage(PyThreadState *tstate, PyGC_Head *collectable) -{ - destructor finalize; - PyGC_Head seen; + gc = (PyGC_Head *)wrcb_to_call._gc_next; + PyObject *op = FROM_GC(gc); + _PyObject_ASSERT(op, PyWeakref_Check(op)); + PyWeakReference *wr = (PyWeakReference *)op; + callback = wr->wr_callback; + _PyObject_ASSERT(op, callback != NULL); - /* While we're going through the loop, `finalize(op)` may cause op, or - * other objects, to be reclaimed via refcounts falling to zero. So - * there's little we can rely on about the structure of the input - * `collectable` list across iterations. For safety, we always take the - * first object in that list and move it to a temporary `seen` list. - * If objects vanish from the `collectable` and `seen` lists we don't - * care. - */ - gc_list_init(&seen); + temp = PyObject_CallOneArg(callback, (PyObject *)wr); + if (temp == NULL) { + PyErr_FormatUnraisable("Exception ignored on " + "calling weakref callback %R", callback); + } + else { + Py_DECREF(temp); + } - while (!gc_list_is_empty(collectable)) { - PyGC_Head *gc = GC_NEXT(collectable); - PyObject *op = FROM_GC(gc); - gc_list_move(gc, &seen); - if (!_PyGC_FINALIZED(op) && - (finalize = Py_TYPE(op)->tp_finalize) != NULL) - { - _PyGC_SET_FINALIZED(op); - Py_INCREF(op); - finalize(op); - assert(!_PyErr_Occurred(tstate)); - Py_DECREF(op); + Py_DECREF(op); + if (wrcb_to_call._gc_next == (uintptr_t)gc) { + gc_list_move(gc, old); + } + else { + ++num_freed; } } - gc_list_merge(&seen, collectable); + + return num_freed; } -/* Break reference cycles by clearing the containers involved. This is - * tricky business as the lists can be changing and we don't know which - * objects may be freed. It is possible I screwed something up here. - */ +/* Clear all weakrefs to unreachable objects. */ static void -delete_garbage(PyThreadState *tstate, GCState *gcstate, - PyGC_Head *collectable, PyGC_Head *old) +clear_weakrefs(PyGC_Head *unreachable) { - assert(!_PyErr_Occurred(tstate)); + PyGC_Head *gc; + PyGC_Head *next; - while (!gc_list_is_empty(collectable)) { - PyGC_Head *gc = GC_NEXT(collectable); - PyObject *op = FROM_GC(gc); + for (gc = GC_NEXT(unreachable); gc != unreachable; gc = next) { + PyWeakReference **wrlist; - _PyObject_ASSERT_WITH_MSG(op, Py_REFCNT(op) > 0, - "refcount is too small"); + PyObject *op = FROM_GC(gc); + next = GC_NEXT(gc); - if (gcstate->debug & _PyGC_DEBUG_SAVEALL) { - assert(gcstate->garbage != NULL); - if (PyList_Append(gcstate->garbage, op) < 0) { - _PyErr_Clear(tstate); - } + if (PyWeakref_Check(op)) { + _PyWeakref_ClearRef((PyWeakReference *)op); } - else { - inquiry clear; - if ((clear = Py_TYPE(op)->tp_clear) != NULL) { - Py_INCREF(op); - (void) clear(op); - if (_PyErr_Occurred(tstate)) { - PyErr_FormatUnraisable("Exception ignored in tp_clear of %s", - Py_TYPE(op)->tp_name); - } - Py_DECREF(op); - } + + if (! _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))) { + continue; } - if (GC_NEXT(collectable) == gc) { - /* object is still alive, move it, it may die later */ - gc_clear_collecting(gc); - gc_list_move(gc, old); + + wrlist = _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(op); + + for (PyWeakReference *wr = *wrlist; wr != NULL; wr = *wrlist) { + _PyObject_ASSERT((PyObject *)wr, wr->wr_object == op); + _PyWeakref_ClearRef(wr); + _PyObject_ASSERT((PyObject *)wr, wr->wr_object == Py_None); } } } +static void +show_stats_each_generations(GCState *gcstate) +{ + char buf[100]; + size_t pos = 0; -/* Deduce which objects among "base" are unreachable from outside the list - and move them to 'unreachable'. The process consist in the following steps: - -1. Copy all reference counts to a different field (gc_prev is used to hold - this copy to save memory). -2. Traverse all objects in "base" and visit all referred objects using - "tp_traverse" and for every visited object, subtract 1 to the reference - count (the one that we copied in the previous step). After this step, all - objects that can be reached directly from outside must have strictly positive - reference count, while all unreachable objects must have a count of exactly 0. -3. Identify all unreachable objects (the ones with 0 reference count) and move - them to the "unreachable" list. This step also needs to move back to "base" all - objects that were initially marked as unreachable but are referred transitively - by the reachable objects (the ones with strictly positive reference count). - -Contracts: - - * The "base" has to be a valid list with no mask set. + for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) { + pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos, + " %zd", + gc_list_size(GEN_HEAD(gcstate, i))); + } - * The "unreachable" list must be uninitialized (this function calls - gc_list_init over 'unreachable'). + PySys_FormatStderr( + "gc: objects in each generation:%s\n" + "gc: objects in permanent generation: %zd\n", + buf, gc_list_size(&gcstate->permanent_generation.head)); +} -IMPORTANT: This function leaves 'unreachable' with the NEXT_MASK_UNREACHABLE -flag set but it does not clear it to skip unnecessary iteration. Before the -flag is cleared (for example, by using 'clear_unreachable_mask' function or -by a call to 'move_legacy_finalizers'), the 'unreachable' list is not a normal -list and we can not use most gc_list_* functions for it. */ -static inline void +static inline Py_ssize_t deduce_unreachable(PyGC_Head *base, PyGC_Head *unreachable) { validate_list(base, collecting_clear_unreachable_clear); - /* Using ob_refcnt and gc_refs, calculate which objects in the - * container set are reachable from outside the set (i.e., have a - * refcount greater than 0 when all the references within the - * set are taken into account). - */ - update_refs(base); // gc_prev is used for gc_refs + Py_ssize_t candidates = update_refs(base); // gc_prev is used for gc_refs subtract_refs(base); - - /* Leave everything reachable from outside base in base, and move - * everything else (in base) to unreachable. - * - * NOTE: This used to move the reachable objects into a reachable - * set instead. But most things usually turn out to be reachable, - * so it's more efficient to move the unreachable things. It "sounds slick" - * to move the unreachable objects, until you think about it - the reason it - * pays isn't actually obvious. - * - * Suppose we create objects A, B, C in that order. They appear in the young - * generation in the same order. If B points to A, and C to B, and C is - * reachable from outside, then the adjusted refcounts will be 0, 0, and 1 - * respectively. - * - * When move_unreachable finds A, A is moved to the unreachable list. The - * same for B when it's first encountered. Then C is traversed, B is moved - * _back_ to the reachable list. B is eventually traversed, and then A is - * moved back to the reachable list. - * - * So instead of not moving at all, the reachable objects B and A are moved - * twice each. Why is this a win? A straightforward algorithm to move the - * reachable objects instead would move A, B, and C once each. - * - * The key is that this dance leaves the objects in order C, B, A - it's - * reversed from the original order. On all _subsequent_ scans, none of - * them will move. Since most objects aren't in cycles, this can save an - * unbounded number of moves across an unbounded number of later collections. - * It can cost more only the first time the chain is scanned. - * - * Drawback: move_unreachable is also used to find out what's still trash - * after finalizers may resurrect objects. In _that_ case most unreachable - * objects will remain unreachable, so it would be more efficient to move - * the reachable objects instead. But this is a one-time cost, probably not - * worth complicating the code to speed just a little. - */ + gc_list_init(unreachable); move_unreachable(base, unreachable); // gc_prev is pointer again validate_list(base, collecting_clear_unreachable_clear); validate_list(unreachable, collecting_set_unreachable_set); + return candidates; } -/* Handle objects that may have resurrected after a call to 'finalize_garbage', moving - them to 'old_generation' and placing the rest on 'still_unreachable'. - - Contracts: - * After this function 'unreachable' must not be used anymore and 'still_unreachable' - will contain the objects that did not resurrect. - - * The "still_unreachable" list must be uninitialized (this function calls - gc_list_init over 'still_unreachable'). - -IMPORTANT: After a call to this function, the 'still_unreachable' set will have the -PREV_MARK_COLLECTING set, but the objects in this set are going to be removed so -we can skip the expense of clearing the flag to avoid extra iteration. */ static inline void handle_resurrected_objects(PyGC_Head *unreachable, PyGC_Head* still_unreachable, PyGC_Head *old_generation) { - // Remove the PREV_MASK_COLLECTING from unreachable - // to prepare it for a new call to 'deduce_unreachable' gc_list_clear_collecting(unreachable); - // After the call to deduce_unreachable, the 'still_unreachable' set will - // have the PREV_MARK_COLLECTING set, but the objects are going to be - // removed so we can skip the expense of clearing the flag. PyGC_Head* resurrected = unreachable; deduce_unreachable(resurrected, still_unreachable); clear_unreachable_mask(still_unreachable); - // Move the resurrected objects to the old generation for future collection. gc_list_merge(resurrected, old_generation); } -static void -gc_collect_region(PyThreadState *tstate, - PyGC_Head *from, - PyGC_Head *to, - struct gc_collection_stats *stats); - -static inline Py_ssize_t -gc_list_set_space(PyGC_Head *list, int space) -{ - Py_ssize_t size = 0; - PyGC_Head *gc; - for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) { - gc_set_old_space(gc, space); - size++; - } - return size; -} - -/* Making progress in the incremental collector - * In order to eventually collect all cycles - * the incremental collector must progress through the old - * space faster than objects are added to the old space. - * - * Each young or incremental collection adds a number of - * objects, S (for survivors) to the old space, and - * incremental collectors scan I objects from the old space. - * I > S must be true. We also want I > S * N to be where - * N > 1. Higher values of N mean that the old space is - * scanned more rapidly. - * The default incremental threshold of 10 translates to - * N == 1.4 (1 + 4/threshold) - */ - -/* Divide by 10, so that the default incremental threshold of 10 - * scans objects at 1% of the heap size */ -#define SCAN_RATE_DIVISOR 10 static void -add_stats(GCState *gcstate, int gen, struct gc_collection_stats *stats) +invoke_gc_callback(PyThreadState *tstate, const char *phase, + int generation, struct gc_generation_stats *stats) { - gcstate->generation_stats[gen].collected += stats->collected; - gcstate->generation_stats[gen].uncollectable += stats->uncollectable; - gcstate->generation_stats[gen].collections += 1; -} + assert(!_PyErr_Occurred(tstate)); -static void -gc_collect_young(PyThreadState *tstate, - struct gc_collection_stats *stats) -{ GCState *gcstate = &tstate->interp->gc; - validate_spaces(gcstate); - PyGC_Head *young = &gcstate->young.head; - PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head; - untrack_tuples(young); - GC_STAT_ADD(0, collections, 1); -#ifdef Py_STATS - { - Py_ssize_t count = 0; - PyGC_Head *gc; - for (gc = GC_NEXT(young); gc != young; gc = GC_NEXT(gc)) { - count++; - } - } -#endif - - PyGC_Head survivors; - gc_list_init(&survivors); - gc_list_set_space(young, gcstate->visited_space); - gc_collect_region(tstate, young, &survivors, stats); - gc_list_merge(&survivors, visited); - validate_spaces(gcstate); - gcstate->young.count = 0; - gcstate->old[gcstate->visited_space].count++; - add_stats(gcstate, 0, stats); - validate_spaces(gcstate); -} - -#ifndef NDEBUG -static inline int -IS_IN_VISITED(PyGC_Head *gc, int visited_space) -{ - assert(visited_space == 0 || other_space(visited_space) == 0); - return gc_old_space(gc) == visited_space; -} -#endif - -struct container_and_flag { - PyGC_Head *container; - int visited_space; - intptr_t size; -}; - -/* A traversal callback for adding to container) */ -static int -visit_add_to_container(PyObject *op, void *arg) -{ - OBJECT_STAT_INC(object_visits); - struct container_and_flag *cf = (struct container_and_flag *)arg; - int visited = cf->visited_space; - assert(visited == get_gc_state()->visited_space); - if (!_Py_IsImmortal(op) && _PyObject_IS_GC(op)) { - PyGC_Head *gc = AS_GC(op); - if (_PyObject_GC_IS_TRACKED(op) && - gc_old_space(gc) != visited) { - gc_flip_old_space(gc); - gc_list_move(gc, cf->container); - cf->size++; - } + if (gcstate->callbacks == NULL) { + return; } - return 0; -} -static intptr_t -expand_region_transitively_reachable(PyGC_Head *container, PyGC_Head *gc, GCState *gcstate) -{ - struct container_and_flag arg = { - .container = container, - .visited_space = gcstate->visited_space, - .size = 0 - }; - assert(GC_NEXT(gc) == container); - while (gc != container) { - /* Survivors will be moved to visited space, so they should - * have been marked as visited */ - assert(IS_IN_VISITED(gc, gcstate->visited_space)); - PyObject *op = FROM_GC(gc); - assert(_PyObject_GC_IS_TRACKED(op)); - if (_Py_IsImmortal(op)) { - PyGC_Head *next = GC_NEXT(gc); - gc_list_move(gc, &get_gc_state()->permanent_generation.head); - gc = next; - continue; + assert(PyList_CheckExact(gcstate->callbacks)); + PyObject *info = NULL; + if (PyList_GET_SIZE(gcstate->callbacks) != 0) { + info = Py_BuildValue("{sisnsnsnsd}", + "generation", generation, + "collected", stats->collected, + "uncollectable", stats->uncollectable, + "candidates", stats->candidates, + "duration", stats->duration); + if (info == NULL) { + PyErr_FormatUnraisable("Exception ignored while invoking gc callbacks"); + return; } - traverseproc traverse = Py_TYPE(op)->tp_traverse; - (void) traverse(op, - visit_add_to_container, - &arg); - gc = GC_NEXT(gc); } - return arg.size; -} -/* Do bookkeeping for a completed GC cycle */ -static void -completed_scavenge(GCState *gcstate) -{ - /* We must observe two invariants: - * 1. Members of the permanent generation must be marked visited. - * 2. We cannot touch members of the permanent generation. */ - int visited; - if (gc_list_is_empty(&gcstate->permanent_generation.head)) { - /* Permanent generation is empty so we can flip spaces bit */ - int not_visited = gcstate->visited_space; - visited = other_space(not_visited); - gcstate->visited_space = visited; - /* Make sure all objects have visited bit set correctly */ - gc_list_set_space(&gcstate->young.head, not_visited); - } - else { - /* We must move the objects from visited to pending space. */ - visited = gcstate->visited_space; - int not_visited = other_space(visited); - assert(gc_list_is_empty(&gcstate->old[not_visited].head)); - gc_list_merge(&gcstate->old[visited].head, &gcstate->old[not_visited].head); - gc_list_set_space(&gcstate->old[not_visited].head, not_visited); + PyObject *phase_obj = PyUnicode_FromString(phase); + if (phase_obj == NULL) { + Py_XDECREF(info); + PyErr_FormatUnraisable("Exception ignored while invoking gc callbacks"); + return; } - assert(gc_list_is_empty(&gcstate->old[visited].head)); - gcstate->work_to_do = 0; - gcstate->phase = GC_PHASE_MARK; -} -static intptr_t -move_to_reachable(PyObject *op, PyGC_Head *reachable, int visited_space) -{ - if (op != NULL && !_Py_IsImmortal(op) && _PyObject_IS_GC(op)) { - PyGC_Head *gc = AS_GC(op); - if (_PyObject_GC_IS_TRACKED(op) && - gc_old_space(gc) != visited_space) { - gc_flip_old_space(gc); - gc_list_move(gc, reachable); - return 1; + PyObject *stack[] = {phase_obj, info}; + for (Py_ssize_t i=0; icallbacks); i++) { + PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i); + Py_INCREF(cb); + r = PyObject_Vectorcall(cb, stack, 2, NULL); + if (r == NULL) { + PyErr_FormatUnraisable("Exception ignored while " + "calling GC callback %R", cb); } - } - return 0; -} - -static intptr_t -mark_all_reachable(PyGC_Head *reachable, PyGC_Head *visited, int visited_space) -{ - // Transitively traverse all objects from reachable, until empty - struct container_and_flag arg = { - .container = reachable, - .visited_space = visited_space, - .size = 0 - }; - while (!gc_list_is_empty(reachable)) { - PyGC_Head *gc = _PyGCHead_NEXT(reachable); - assert(gc_old_space(gc) == visited_space); - gc_list_move(gc, visited); - PyObject *op = FROM_GC(gc); - traverseproc traverse = Py_TYPE(op)->tp_traverse; - (void) traverse(op, - visit_add_to_container, - &arg); - } - gc_list_validate_space(visited, visited_space); - return arg.size; -} - -static intptr_t -mark_stacks(PyInterpreterState *interp, PyGC_Head *visited, int visited_space, bool start) -{ - PyGC_Head reachable; - gc_list_init(&reachable); - Py_ssize_t objects_marked = 0; - // Move all objects on stacks to reachable - _PyRuntimeState *runtime = &_PyRuntime; - HEAD_LOCK(runtime); - PyThreadState* ts = PyInterpreterState_ThreadHead(interp); - HEAD_UNLOCK(runtime); - while (ts) { - _PyInterpreterFrame *frame = ts->current_frame; - while (frame) { - if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) { - frame = frame->previous; - continue; - } - _PyStackRef *locals = frame->localsplus; - _PyStackRef *sp = frame->stackpointer; - objects_marked += move_to_reachable(frame->f_locals, &reachable, visited_space); - PyObject *func = PyStackRef_AsPyObjectBorrow(frame->f_funcobj); - objects_marked += move_to_reachable(func, &reachable, visited_space); - while (sp > locals) { - sp--; - if (PyStackRef_IsNullOrInt(*sp)) { - continue; - } - PyObject *op = PyStackRef_AsPyObjectBorrow(*sp); - if (_Py_IsImmortal(op)) { - continue; - } - if (_PyObject_IS_GC(op)) { - PyGC_Head *gc = AS_GC(op); - if (_PyObject_GC_IS_TRACKED(op) && - gc_old_space(gc) != visited_space) { - gc_flip_old_space(gc); - objects_marked++; - gc_list_move(gc, &reachable); - } - } - } - if (!start && frame->visited) { - // If this frame has already been visited, then the lower frames - // will have already been visited and will not have changed - break; - } - frame->visited = 1; - frame = frame->previous; + else { + Py_DECREF(r); } - HEAD_LOCK(runtime); - ts = PyThreadState_Next(ts); - HEAD_UNLOCK(runtime); - } - objects_marked += mark_all_reachable(&reachable, visited, visited_space); - assert(gc_list_is_empty(&reachable)); - return objects_marked; -} - -static intptr_t -mark_global_roots(PyInterpreterState *interp, PyGC_Head *visited, int visited_space) -{ - PyGC_Head reachable; - gc_list_init(&reachable); - Py_ssize_t objects_marked = 0; - objects_marked += move_to_reachable(interp->sysdict, &reachable, visited_space); - objects_marked += move_to_reachable(interp->builtins, &reachable, visited_space); - objects_marked += move_to_reachable(interp->dict, &reachable, visited_space); - struct types_state *types = &interp->types; - for (int i = 0; i < _Py_MAX_MANAGED_STATIC_BUILTIN_TYPES; i++) { - objects_marked += move_to_reachable(types->builtins.initialized[i].tp_dict, &reachable, visited_space); - objects_marked += move_to_reachable(types->builtins.initialized[i].tp_subclasses, &reachable, visited_space); - } - for (int i = 0; i < _Py_MAX_MANAGED_STATIC_EXT_TYPES; i++) { - objects_marked += move_to_reachable(types->for_extensions.initialized[i].tp_dict, &reachable, visited_space); - objects_marked += move_to_reachable(types->for_extensions.initialized[i].tp_subclasses, &reachable, visited_space); + Py_DECREF(cb); } - objects_marked += mark_all_reachable(&reachable, visited, visited_space); - assert(gc_list_is_empty(&reachable)); - return objects_marked; + Py_DECREF(phase_obj); + Py_XDECREF(info); + assert(!_PyErr_Occurred(tstate)); } -static intptr_t -mark_at_start(PyThreadState *tstate) -{ - // TO DO -- Make this incremental - GCState *gcstate = &tstate->interp->gc; - PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head; - Py_ssize_t objects_marked = mark_global_roots(tstate->interp, visited, gcstate->visited_space); - objects_marked += mark_stacks(tstate->interp, visited, gcstate->visited_space, true); - gcstate->work_to_do -= objects_marked; - gcstate->phase = GC_PHASE_COLLECT; - validate_spaces(gcstate); - return objects_marked; -} - -static intptr_t -assess_work_to_do(GCState *gcstate) -{ - /* The amount of work we want to do depends on three things. - * 1. The number of new objects created - * 2. The growth in heap size since the last collection - * 3. The heap size (up to the number of new objects, to avoid quadratic effects) - * - * For a steady state heap, the amount of work to do is three times the number - * of new objects added to the heap. This ensures that we stay ahead in the - * worst case of all new objects being garbage. - * - * This could be improved by tracking survival rates, but it is still a - * large improvement on the non-marking approach. - */ - intptr_t scale_factor = gcstate->old[0].threshold; - if (scale_factor < 2) { - scale_factor = 2; - } - intptr_t new_objects = gcstate->young.count; - intptr_t max_heap_fraction = new_objects*2; - intptr_t heap_fraction = gcstate->heap_size / SCAN_RATE_DIVISOR / scale_factor; - if (heap_fraction > max_heap_fraction) { - heap_fraction = max_heap_fraction; - } - gcstate->young.count = 0; - return new_objects + heap_fraction; -} -static void -gc_collect_increment(PyThreadState *tstate, struct gc_collection_stats *stats) +/* Find the oldest generation where the count exceeds the threshold. */ +static int +gc_select_generation(GCState *gcstate) { - GC_STAT_ADD(1, collections, 1); - GCState *gcstate = &tstate->interp->gc; - gcstate->work_to_do += assess_work_to_do(gcstate); - if (gcstate->work_to_do < 0) { - return; - } - untrack_tuples(&gcstate->young.head); - if (gcstate->phase == GC_PHASE_MARK) { - Py_ssize_t objects_marked = mark_at_start(tstate); - GC_STAT_ADD(1, objects_transitively_reachable, objects_marked); - gcstate->work_to_do -= objects_marked; - validate_spaces(gcstate); - return; - } - PyGC_Head *not_visited = &gcstate->old[gcstate->visited_space^1].head; - PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head; - PyGC_Head increment; - gc_list_init(&increment); - int scale_factor = gcstate->old[0].threshold; - if (scale_factor < 2) { - scale_factor = 2; - } - intptr_t objects_marked = mark_stacks(tstate->interp, visited, gcstate->visited_space, false); - GC_STAT_ADD(1, objects_transitively_reachable, objects_marked); - gcstate->work_to_do -= objects_marked; - gc_list_set_space(&gcstate->young.head, gcstate->visited_space); - gc_list_merge(&gcstate->young.head, &increment); - gc_list_validate_space(&increment, gcstate->visited_space); - Py_ssize_t increment_size = gc_list_size(&increment); - while (increment_size < gcstate->work_to_do) { - if (gc_list_is_empty(not_visited)) { - break; + for (int i = NUM_GENERATIONS-1; i >= 0; i--) { + if (gcstate->generations[i].count > gcstate->generations[i].threshold) { + if (i == NUM_GENERATIONS - 1 + && gcstate->long_lived_pending < gcstate->long_lived_total / 4) + { + continue; + } + return i; } - PyGC_Head *gc = _PyGCHead_NEXT(not_visited); - gc_list_move(gc, &increment); - increment_size++; - assert(!_Py_IsImmortal(FROM_GC(gc))); - gc_set_old_space(gc, gcstate->visited_space); - increment_size += expand_region_transitively_reachable(&increment, gc, gcstate); - } - GC_STAT_ADD(1, objects_not_transitively_reachable, increment_size); - validate_list(&increment, collecting_clear_unreachable_clear); - gc_list_validate_space(&increment, gcstate->visited_space); - PyGC_Head survivors; - gc_list_init(&survivors); - gc_collect_region(tstate, &increment, &survivors, stats); - gc_list_merge(&survivors, visited); - assert(gc_list_is_empty(&increment)); - gcstate->work_to_do -= increment_size; - - add_stats(gcstate, 1, stats); - if (gc_list_is_empty(not_visited)) { - completed_scavenge(gcstate); - } - validate_spaces(gcstate); + } + return -1; } -static void -gc_collect_full(PyThreadState *tstate, - struct gc_collection_stats *stats) -{ - GC_STAT_ADD(2, collections, 1); - GCState *gcstate = &tstate->interp->gc; - validate_spaces(gcstate); - PyGC_Head *young = &gcstate->young.head; - PyGC_Head *pending = &gcstate->old[gcstate->visited_space^1].head; - PyGC_Head *visited = &gcstate->old[gcstate->visited_space].head; - untrack_tuples(young); - /* merge all generations into visited */ - gc_list_merge(young, pending); - gc_list_validate_space(pending, 1-gcstate->visited_space); - gc_list_set_space(pending, gcstate->visited_space); - gcstate->young.count = 0; - gc_list_merge(pending, visited); - validate_spaces(gcstate); - - gc_collect_region(tstate, visited, visited, - stats); - validate_spaces(gcstate); - gcstate->young.count = 0; - gcstate->old[0].count = 0; - gcstate->old[1].count = 0; - completed_scavenge(gcstate); - _PyGC_ClearAllFreeLists(tstate->interp); - validate_spaces(gcstate); - add_stats(gcstate, 2, stats); -} - -/* This is the main function. Read this to understand how the + +/* This is the main function. Read this to understand how the * collection process works. */ -static void -gc_collect_region(PyThreadState *tstate, - PyGC_Head *from, - PyGC_Head *to, - struct gc_collection_stats *stats) +static Py_ssize_t +gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason) { + int i; + Py_ssize_t m = 0; /* # objects collected */ + Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */ + PyGC_Head *young; /* the generation we are examining */ + PyGC_Head *old; /* next older generation */ PyGC_Head unreachable; /* non-problematic unreachable trash */ PyGC_Head finalizers; /* objects with, & reachable from, __del__ */ - PyGC_Head *gc; /* initialize to prevent a compiler warning */ + PyGC_Head *gc; + struct gc_generation_stats stats = {0}; GCState *gcstate = &tstate->interp->gc; + // gc_collect_main() must not be called before _PyGC_Init + // or after _PyGC_Fini() assert(gcstate->garbage != NULL); assert(!_PyErr_Occurred(tstate)); + assert(tstate->current_frame == NULL || tstate->current_frame->stackpointer != NULL); + + int expected = 0; + if (!_Py_atomic_compare_exchange_int(&gcstate->collecting, &expected, 1)) { + return 0; + } + gcstate->frame = tstate->current_frame; + + if (generation == GENERATION_AUTO) { + generation = gc_select_generation(gcstate); + if (generation < 0) { + gcstate->frame = NULL; + _Py_atomic_store_int(&gcstate->collecting, 0); + return 0; + } + } + + assert(generation >= 0 && generation < NUM_GENERATIONS); + +#ifdef Py_STATS + if (_Py_stats) { + _Py_stats->object_stats.object_visits = 0; + } +#endif + GC_STAT_ADD(generation, collections, 1); + + if (reason != _Py_GC_REASON_SHUTDOWN) { + invoke_gc_callback(tstate, "start", generation, &stats); + } + + if (gcstate->debug & _PyGC_DEBUG_STATS) { + PySys_WriteStderr("gc: collecting generation %d...\n", generation); + show_stats_each_generations(gcstate); + } + + if (PyDTrace_GC_START_ENABLED()) { + PyDTrace_GC_START(generation); + } + (void)PyTime_PerfCounterRaw(&stats.ts_start); + + /* update collection and allocation counters */ + if (generation+1 < NUM_GENERATIONS) { + gcstate->generations[generation+1].count += 1; + } + for (i = 0; i <= generation; i++) { + gcstate->generations[i].count = 0; + } - gc_list_init(&unreachable); - deduce_unreachable(from, &unreachable); - validate_consistent_old_space(from); - untrack_tuples(from); - validate_consistent_old_space(to); - if (from != to) { - gc_list_merge(from, to); + /* merge younger generations with one we are currently collecting */ + for (i = 0; i < generation; i++) { + gc_list_merge(GEN_HEAD(gcstate, i), GEN_HEAD(gcstate, generation)); } - validate_consistent_old_space(to); + + /* handy references */ + young = GEN_HEAD(gcstate, generation); + if (generation < NUM_GENERATIONS-1) { + old = GEN_HEAD(gcstate, generation+1); + } + else { + old = young; + } + validate_list(old, collecting_clear_unreachable_clear); + + stats.candidates = deduce_unreachable(young, &unreachable); + + untrack_tuples(young); /* Move reachable objects to next generation. */ + if (young != old) { + if (generation == NUM_GENERATIONS - 2) { + gcstate->long_lived_pending += gc_list_size(young); + } + gc_list_merge(young, old); + } + else { + /* We only un-track dicts in full collections, to avoid quadratic + dict build-up. See issue #14775. + Note: _PyDict_MaybeUntrack was removed in 3.14, so dict + untracking during GC is no longer done. */ + gcstate->long_lived_pending = 0; + gcstate->long_lived_total = gc_list_size(young); + } /* All objects in unreachable are trash, but objects reachable from * legacy finalizers (e.g. tp_del) can't safely be deleted. */ gc_list_init(&finalizers); // NEXT_MASK_UNREACHABLE is cleared here. - // After move_legacy_finalizers(), unreachable is normal list. move_legacy_finalizers(&unreachable, &finalizers); - /* finalizers contains the unreachable objects with a legacy finalizer; - * unreachable objects reachable *from* those are also uncollectable, - * and we move those into the finalizers list too. - */ move_legacy_finalizer_reachable(&finalizers); + validate_list(&finalizers, collecting_clear_unreachable_clear); validate_list(&unreachable, collecting_set_unreachable_clear); + /* Print debugging information. */ if (gcstate->debug & _PyGC_DEBUG_COLLECTABLE) { for (gc = GC_NEXT(&unreachable); gc != &unreachable; gc = GC_NEXT(gc)) { @@ -1749,145 +1200,93 @@ gc_collect_region(PyThreadState *tstate, } } - /* Clear weakrefs and invoke callbacks as necessary. */ - stats->collected += handle_weakrefs(&unreachable, to, true); - gc_list_validate_space(to, gcstate->visited_space); - validate_list(to, collecting_clear_unreachable_clear); + /* Invoke weakref callbacks as necessary. */ + m += handle_weakref_callbacks(&unreachable, old); + + validate_list(old, collecting_clear_unreachable_clear); validate_list(&unreachable, collecting_set_unreachable_clear); /* Call tp_finalize on objects which have one. */ finalize_garbage(tstate, &unreachable); + /* Handle any objects that may have resurrected after the call * to 'finalize_garbage' and continue the collection with the * objects that are still unreachable */ PyGC_Head final_unreachable; - gc_list_init(&final_unreachable); - handle_resurrected_objects(&unreachable, &final_unreachable, to); - - /* Clear weakrefs to objects in the unreachable set. No Python-level - * code must be allowed to access those unreachable objects. During - * delete_garbage(), finalizers outside the unreachable set might run - * and create new weakrefs. If those weakrefs were not cleared, they - * could reveal unreachable objects. Callbacks are not executed. + handle_resurrected_objects(&unreachable, &final_unreachable, old); + + /* Clear weakrefs to objects in the remaining unreachable set before + * tp_clear() can expose them. */ - handle_weakrefs(&final_unreachable, NULL, false); + clear_weakrefs(&final_unreachable); - /* Call tp_clear on objects in the final_unreachable set. This will cause - * the reference cycles to be broken. It may also cause some objects - * in finalizers to be freed. - */ - stats->collected += gc_list_size(&final_unreachable); - delete_garbage(tstate, gcstate, &final_unreachable, to); + m += gc_list_size(&final_unreachable); + delete_garbage(tstate, gcstate, &final_unreachable, old); /* Collect statistics on uncollectable objects found and print * debugging information. */ - Py_ssize_t n = 0; for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) { n++; if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) debug_cycle("uncollectable", FROM_GC(gc)); } - stats->uncollectable = n; - /* Append instances in the uncollectable set to a Python - * reachable list of garbage. The programmer has to deal with - * this if they insist on creating this type of structure. - */ - handle_legacy_finalizers(tstate, gcstate, &finalizers, to); - gc_list_validate_space(to, gcstate->visited_space); - validate_list(to, collecting_clear_unreachable_clear); -} - -/* Invoke progress callbacks to notify clients that garbage collection - * is starting or stopping - */ -static void -do_gc_callback(GCState *gcstate, const char *phase, - int generation, struct gc_collection_stats *stats) -{ - assert(!PyErr_Occurred()); - - /* The local variable cannot be rebound, check it for sanity */ - assert(PyList_CheckExact(gcstate->callbacks)); - PyObject *info = NULL; - if (PyList_GET_SIZE(gcstate->callbacks) != 0) { - info = Py_BuildValue("{sisnsn}", - "generation", generation, - "collected", stats->collected, - "uncollectable", stats->uncollectable); - if (info == NULL) { - PyErr_FormatUnraisable("Exception ignored while invoking gc callbacks"); - return; - } + (void)PyTime_PerfCounterRaw(&stats.ts_stop); + stats.collected = m; + stats.uncollectable = n; + stats.duration = PyTime_AsSecondsDouble(stats.ts_stop - stats.ts_start); + if (gcstate->debug & _PyGC_DEBUG_STATS) { + PySys_WriteStderr( + "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n", + n+m, n, stats.duration); } - PyObject *phase_obj = PyUnicode_FromString(phase); - if (phase_obj == NULL) { - Py_XDECREF(info); - PyErr_FormatUnraisable("Exception ignored while invoking gc callbacks"); - return; + handle_legacy_finalizers(tstate, gcstate, &finalizers, old); + validate_list(old, collecting_clear_unreachable_clear); + + /* Clear free list only during the collection of the highest + * generation */ + if (generation == NUM_GENERATIONS-1) { + _PyGC_ClearAllFreeLists(tstate->interp); } - PyObject *stack[] = {phase_obj, info}; - for (Py_ssize_t i=0; icallbacks); i++) { - PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i); - Py_INCREF(cb); /* make sure cb doesn't go away */ - r = PyObject_Vectorcall(cb, stack, 2, NULL); - if (r == NULL) { - PyErr_FormatUnraisable("Exception ignored while " - "calling GC callback %R", cb); + if (_PyErr_Occurred(tstate)) { + if (reason == _Py_GC_REASON_SHUTDOWN) { + _PyErr_Clear(tstate); } else { - Py_DECREF(r); + PyErr_FormatUnraisable("Exception ignored in garbage collection"); } - Py_DECREF(cb); } - Py_DECREF(phase_obj); - Py_XDECREF(info); - assert(!PyErr_Occurred()); -} -static void -invoke_gc_callback(GCState *gcstate, const char *phase, - int generation, struct gc_collection_stats *stats) -{ - if (gcstate->callbacks == NULL) { - return; + /* Update stats */ + struct gc_generation_stats *total_stats = &gcstate->generation_stats_gen[generation]; + total_stats->collections++; + total_stats->collected += m; + total_stats->uncollectable += n; + total_stats->candidates += stats.candidates; + total_stats->duration += stats.duration; + + GC_STAT_ADD(generation, objects_collected, m); +#ifdef Py_STATS + if (_Py_stats) { + GC_STAT_ADD(generation, object_visits, + _Py_stats->object_stats.object_visits); + _Py_stats->object_stats.object_visits = 0; } - do_gc_callback(gcstate, phase, generation, stats); -} +#endif -static int -referrersvisit(PyObject* obj, void *arg) -{ - PyObject *objs = arg; - Py_ssize_t i; - for (i = 0; i < PyTuple_GET_SIZE(objs); i++) { - if (PyTuple_GET_ITEM(objs, i) == obj) { - return 1; - } + if (PyDTrace_GC_DONE_ENABLED()) { + PyDTrace_GC_DONE(n + m); } - return 0; -} -static int -gc_referrers_for(PyObject *objs, PyGC_Head *list, PyObject *resultlist) -{ - PyGC_Head *gc; - PyObject *obj; - traverseproc traverse; - for (gc = GC_NEXT(list); gc != list; gc = GC_NEXT(gc)) { - obj = FROM_GC(gc); - traverse = Py_TYPE(obj)->tp_traverse; - if (obj == objs || obj == resultlist) { - continue; - } - if (traverse(obj, referrersvisit, objs)) { - if (PyList_Append(resultlist, obj) < 0) { - return 0; /* error */ - } - } + if (reason != _Py_GC_REASON_SHUTDOWN) { + invoke_gc_callback(tstate, "stop", generation, &stats); } - return 1; /* no error */ + + assert(!_PyErr_Occurred(tstate)); + gcstate->frame = NULL; + _Py_atomic_store_int(&gcstate->collecting, 0); + return n + m; } PyObject * @@ -1915,25 +1314,19 @@ _PyGC_GetObjects(PyInterpreterState *interp, int generation) GCState *gcstate = &interp->gc; PyObject *result = PyList_New(0); - /* Generation: - * -1: Return all objects - * 0: All young objects - * 1: No objects - * 2: All old objects - */ - if (result == NULL || generation == 1) { - return result; + if (result == NULL) { + return NULL; } - if (generation <= 0) { - if (append_objects(result, &gcstate->young.head)) { - goto error; + + if (generation == -1) { + for (int i = 0; i < NUM_GENERATIONS; i++) { + if (append_objects(result, GEN_HEAD(gcstate, i))) { + goto error; + } } } - if (generation != 0) { - if (append_objects(result, &gcstate->old[0].head)) { - goto error; - } - if (append_objects(result, &gcstate->old[1].head)) { + else { + if (append_objects(result, GEN_HEAD(gcstate, generation))) { goto error; } } @@ -1948,23 +1341,10 @@ void _PyGC_Freeze(PyInterpreterState *interp) { GCState *gcstate = &interp->gc; - /* The permanent_generation must be visited */ - gc_list_set_space(&gcstate->young.head, gcstate->visited_space); - gc_list_merge(&gcstate->young.head, &gcstate->permanent_generation.head); - gcstate->young.count = 0; - PyGC_Head*old0 = &gcstate->old[0].head; - PyGC_Head*old1 = &gcstate->old[1].head; - if (gcstate->visited_space) { - gc_list_set_space(old0, 1); - } - else { - gc_list_set_space(old1, 0); + for (int i = 0; i < NUM_GENERATIONS; ++i) { + gc_list_merge(GEN_HEAD(gcstate, i), &gcstate->permanent_generation.head); + gcstate->generations[i].count = 0; } - gc_list_merge(old0, &gcstate->permanent_generation.head); - gcstate->old[0].count = 0; - gc_list_merge(old1, &gcstate->permanent_generation.head); - gcstate->old[1].count = 0; - validate_spaces(gcstate); } void @@ -1972,8 +1352,7 @@ _PyGC_Unfreeze(PyInterpreterState *interp) { GCState *gcstate = &interp->gc; gc_list_merge(&gcstate->permanent_generation.head, - &gcstate->old[gcstate->visited_space].head); - validate_spaces(gcstate); + GEN_HEAD(gcstate, NUM_GENERATIONS-1)); } Py_ssize_t @@ -1983,7 +1362,6 @@ _PyGC_GetFreezeCount(PyInterpreterState *interp) return gc_list_size(&gcstate->permanent_generation.head); } -/* C API for controlling the state of the garbage collector */ int PyGC_Enable(void) { @@ -2009,111 +1387,27 @@ PyGC_IsEnabled(void) return gcstate->enabled; } -// Show stats for objects in each generations -static void -show_stats_each_generations(GCState *gcstate) +Py_ssize_t +PyGC_Collect(void) { - char buf[100]; - size_t pos = 0; - - for (int i = 0; i < NUM_GENERATIONS && pos < sizeof(buf); i++) { - pos += PyOS_snprintf(buf+pos, sizeof(buf)-pos, - " %zd", - gc_list_size(GEN_HEAD(gcstate, i))); - } - PySys_FormatStderr( - "gc: objects in each generation:%s\n" - "gc: objects in permanent generation: %zd\n", - buf, gc_list_size(&gcstate->permanent_generation.head)); + return _PyGC_Collect(_PyThreadState_GET(), NUM_GENERATIONS - 1, + _Py_GC_REASON_MANUAL); } Py_ssize_t _PyGC_Collect(PyThreadState *tstate, int generation, _PyGC_Reason reason) { - GCState *gcstate = &tstate->interp->gc; - assert(tstate->current_frame == NULL || tstate->current_frame->stackpointer != NULL); - - int expected = 0; - if (!_Py_atomic_compare_exchange_int(&gcstate->collecting, &expected, 1)) { - // Don't start a garbage collection if one is already in progress. - return 0; - } - - struct gc_collection_stats stats = { 0 }; - if (reason != _Py_GC_REASON_SHUTDOWN) { - invoke_gc_callback(gcstate, "start", generation, &stats); - } - PyTime_t t1; - if (gcstate->debug & _PyGC_DEBUG_STATS) { - PySys_WriteStderr("gc: collecting generation %d...\n", generation); - (void)PyTime_PerfCounterRaw(&t1); - show_stats_each_generations(gcstate); - } - if (PyDTrace_GC_START_ENABLED()) { - PyDTrace_GC_START(generation); - } + Py_ssize_t n; PyObject *exc = _PyErr_GetRaisedException(tstate); - switch(generation) { - case 0: - gc_collect_young(tstate, &stats); - break; - case 1: - gc_collect_increment(tstate, &stats); - break; - case 2: - gc_collect_full(tstate, &stats); - break; - default: - Py_UNREACHABLE(); - } - if (PyDTrace_GC_DONE_ENABLED()) { - PyDTrace_GC_DONE(stats.uncollectable + stats.collected); - } - if (reason != _Py_GC_REASON_SHUTDOWN) { - invoke_gc_callback(gcstate, "stop", generation, &stats); - } + n = gc_collect_main(tstate, generation, reason); _PyErr_SetRaisedException(tstate, exc); - GC_STAT_ADD(generation, objects_collected, stats.collected); -#ifdef Py_STATS - if (_Py_stats) { - GC_STAT_ADD(generation, object_visits, - _Py_stats->object_stats.object_visits); - _Py_stats->object_stats.object_visits = 0; - } -#endif - validate_spaces(gcstate); - _Py_atomic_store_int(&gcstate->collecting, 0); - - if (gcstate->debug & _PyGC_DEBUG_STATS) { - PyTime_t t2; - (void)PyTime_PerfCounterRaw(&t2); - double d = PyTime_AsSecondsDouble(t2 - t1); - PySys_WriteStderr( - "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n", - stats.collected + stats.uncollectable, stats.uncollectable, d - ); - } - - return stats.uncollectable + stats.collected; -} - -/* Public API to invoke gc.collect() from C */ -Py_ssize_t -PyGC_Collect(void) -{ - return _PyGC_Collect(_PyThreadState_GET(), 2, _Py_GC_REASON_MANUAL); + return n; } void _PyGC_CollectNoFail(PyThreadState *tstate) { - /* Ideally, this function is only called on interpreter shutdown, - and therefore not recursively. Unfortunately, when there are daemon - threads, a daemon thread can start a cyclic garbage collection - during interpreter shutdown (and then never finish it). - See http://bugs.python.org/issue8713#msg195178 for an example. - */ - _PyGC_Collect(_PyThreadState_GET(), 2, _Py_GC_REASON_SHUTDOWN); + gc_collect_main(tstate, NUM_GENERATIONS - 1, _Py_GC_REASON_SHUTDOWN); } void @@ -2130,9 +1424,6 @@ _PyGC_DumpShutdownStats(PyInterpreterState *interp) message = "gc: %zd uncollectable objects at shutdown; " \ "use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them"; } - /* PyErr_WarnFormat does too many things and we are at shutdown, - the warnings module's dependencies (e.g. linecache) may be gone - already. */ if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0, "gc", NULL, message, PyList_GET_SIZE(gcstate->garbage))) @@ -2158,14 +1449,6 @@ _PyGC_DumpShutdownStats(PyInterpreterState *interp) } } -static void -finalize_unlink_gc_head(PyGC_Head *gc) { - PyGC_Head *prev = GC_PREV(gc); - PyGC_Head *next = GC_NEXT(gc); - _PyGCHead_SET_NEXT(prev, next); - _PyGCHead_SET_PREV(next, prev); -} - void _PyGC_Fini(PyInterpreterState *interp) { @@ -2188,20 +1471,12 @@ _PyGC_Fini(PyInterpreterState *interp) * This bug was originally fixed when reported as gh-90228. The bug was * re-introduced in gh-94673. */ - finalize_unlink_gc_head(&gcstate->young.head); - finalize_unlink_gc_head(&gcstate->old[0].head); - finalize_unlink_gc_head(&gcstate->old[1].head); + for (int i = 0; i < NUM_GENERATIONS; i++) { + finalize_unlink_gc_head(&gcstate->generations[i].head); + } finalize_unlink_gc_head(&gcstate->permanent_generation.head); } -/* for debugging */ -void -_PyGC_Dump(PyGC_Head *g) -{ - _PyObject_Dump(FROM_GC(g)); -} - - #ifdef Py_DEBUG static int visit_validate(PyObject *op, void *parent_raw) @@ -2269,7 +1544,7 @@ _Py_TriggerGC(struct _gc_runtime_state *gcstate) { PyThreadState *tstate = _PyThreadState_GET(); if (gcstate->enabled && - gcstate->young.threshold != 0 && + gcstate->generations[0].threshold != 0 && !_Py_atomic_load_int_relaxed(&gcstate->collecting) && !_PyErr_Occurred(tstate)) { @@ -2277,23 +1552,26 @@ _Py_TriggerGC(struct _gc_runtime_state *gcstate) } } +/* for debugging */ void _PyObject_GC_Link(PyObject *op) { PyGC_Head *gc = AS_GC(op); // gc must be correctly aligned _PyObject_ASSERT(op, ((uintptr_t)gc & (sizeof(uintptr_t)-1)) == 0); + gc->_gc_next = 0; gc->_gc_prev = 0; - } void _Py_RunGC(PyThreadState *tstate) { - if (tstate->interp->gc.enabled) { - _PyGC_Collect(tstate, 1, _Py_GC_REASON_HEAP); + GCState *gcstate = &tstate->interp->gc; + if (!gcstate->enabled) { + return; } + gc_collect_main(tstate, GENERATION_AUTO, _Py_GC_REASON_HEAP); } static PyObject * @@ -2315,7 +1593,6 @@ gc_alloc(PyTypeObject *tp, size_t basicsize, size_t presize) return op; } - PyObject * _PyObject_GC_New(PyTypeObject *tp) { @@ -2395,10 +1672,9 @@ PyObject_GC_Del(void *op) if (_PyObject_GC_IS_TRACKED(op)) { gc_list_remove(g); GCState *gcstate = get_gc_state(); - if (gcstate->young.count > 0) { - gcstate->young.count--; + if (gcstate->generations[0].count > 0) { + gcstate->generations[0].count--; } - gcstate->heap_size--; #ifdef Py_DEBUG PyObject *exc = PyErr_GetRaisedException(); if (PyErr_WarnExplicitFormat(PyExc_ResourceWarning, "gc", 0, @@ -2416,7 +1692,7 @@ PyObject_GC_Del(void *op) } int -PyObject_GC_IsTracked(PyObject* obj) +PyObject_GC_IsTracked(PyObject *obj) { if (_PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) { return 1; @@ -2433,41 +1709,23 @@ PyObject_GC_IsFinalized(PyObject *obj) return 0; } -static int -visit_generation(gcvisitobjects_t callback, void *arg, struct gc_generation *gen) -{ - PyGC_Head *gc_list, *gc; - gc_list = &gen->head; - for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) { - PyObject *op = FROM_GC(gc); - Py_INCREF(op); - int res = callback(op, arg); - Py_DECREF(op); - if (!res) { - return -1; - } - } - return 0; -} - void PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg) { GCState *gcstate = get_gc_state(); int original_state = gcstate->enabled; gcstate->enabled = 0; - if (visit_generation(callback, arg, &gcstate->young) < 0) { - goto done; - } - if (visit_generation(callback, arg, &gcstate->old[0]) < 0) { - goto done; - } - if (visit_generation(callback, arg, &gcstate->old[1]) < 0) { - goto done; + for (size_t i = 0; i < NUM_GENERATIONS; i++) { + if (visit_generation(callback, arg, &gcstate->generations[i]) < 0) { + goto done; + } } visit_generation(callback, arg, &gcstate->permanent_generation); done: gcstate->enabled = original_state; } -#endif // Py_GIL_DISABLED +#undef NEXT_MASK_UNREACHABLE +#undef GEN_HEAD + +#endif // !Py_GIL_DISABLED diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c index d1b8d282415337..48283a3d18e609 100644 --- a/Python/gc_free_threading.c +++ b/Python/gc_free_threading.c @@ -1615,7 +1615,7 @@ get_gc_state(void) void _PyGC_InitState(GCState *gcstate) { - // TODO: move to pycore_runtime_init.h once the incremental GC lands. + // TODO: move to pycore_runtime_init.h. gcstate->young.threshold = 2000; }