From 4f71f1680de11baf7fb421ef500f36ffd5936b58 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 27 Nov 2023 16:51:12 -0700 Subject: [3.12] gh-106931: Intern Statically Allocated Strings Globally (gh-107272) (gh-110713) We tried this before with a dict and for all interned strings. That ran into problems due to interpreter isolation. However, exclusively using a per-interpreter cache caused some inconsistency that can eliminate the benefit of interning. Here we circle back to using a global cache, but only for statically allocated strings. We also use a more-basic _Py_hashtable_t for that global cache instead of a dict. Ideally we would only have the global cache, but the optional isolation of each interpreter's allocator means that a non-static string object must not outlive its interpreter. Thus we would have to store a copy of each such interned string in the global cache, tied to the main interpreter. (cherry-picked from commit b72947a8d26915156323ccfd04d273199ecb870c) --- Doc/data/python3.12.abi | 8239 ++++++++++---------- Include/cpython/unicodeobject.h | 4 +- Include/internal/pycore_global_objects.h | 6 + Include/internal/pycore_hashtable.h | 1 + Include/internal/pycore_runtime.h | 1 + Include/internal/pycore_runtime_init.h | 1 + Lib/test/test_sys.py | 28 + .../2023-07-25-15-29-26.gh-issue-106931.kKU1le.rst | 3 + Objects/unicodeobject.c | 72 +- Python/hashtable.c | 7 + Tools/build/deepfreeze.py | 2 + 11 files changed, 4251 insertions(+), 4113 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-07-25-15-29-26.gh-issue-106931.kKU1le.rst diff --git a/Doc/data/python3.12.abi b/Doc/data/python3.12.abi index e289947..ca82d5a 100644 --- a/Doc/data/python3.12.abi +++ b/Doc/data/python3.12.abi @@ -1019,6 +1019,7 @@ + @@ -1486,6 +1487,7 @@ + @@ -1705,7 +1707,7 @@ - + @@ -2539,7 +2541,7 @@ - + @@ -4002,7 +4004,7 @@ - + @@ -5165,7 +5167,7 @@ - + @@ -5358,7 +5360,7 @@ - + @@ -5806,11 +5808,11 @@ - + - + @@ -5981,7 +5983,7 @@ - + @@ -6878,7 +6880,7 @@ - + @@ -7193,7 +7195,7 @@ - + @@ -7390,7 +7392,7 @@ - + @@ -7635,7 +7637,7 @@ - + @@ -7696,7 +7698,7 @@ - + @@ -8139,12 +8141,12 @@ - + - + @@ -8603,7 +8605,7 @@ - + @@ -9017,7 +9019,10 @@ - + + + + @@ -9058,7 +9063,7 @@ - + @@ -9066,101 +9071,128 @@ - + - + - + - + - + - - + + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -9194,484 +9226,484 @@ - - - + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - + + + - - - + + + - - - - + + + + - - - - + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - - + + + - - - + + + - - - + + + - - + + - - + + - - - - + + + + - - - - + + + + - - - - + + + + - - - + + + - - + + - - - - + + + + - - - - + + + + - - - + + + - - - + + + - - - + + + - - - + + + - - + + - - - - + + + + - - - - + + + + - - - - - + + + + + - - - - - + + + + + - - - - - - - + + + + + + + - - - - + + + + - - - - - - - + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - + + + + - - + + - - - - - + + + + + - - - - - - + + + + + + - - - - + + + + - - + + - - - - - + + + + + - - - - + + + + - - + + - - - - - + + + + + - - - - + + + + - - + + - - - + + + - - + + - - - - - + + + + + - - + + - - - - + + + + - - - + + + - - - - + + + + - - - - - - - - - - + + + + + + + + + + - - - - - + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - + + + + - - - - - + + + + + - - - - - + + + + + - - - + + + - - - + + + - - - - + + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - - + + + + - - - - - + + + + + - - - - + + + + - - - + + + - - - + + + - - - - + + + + - - - + + + - - - - - + + + + + - - - - + + + + - - - + + + - - + + - + @@ -9709,69 +9741,69 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - + - + - + - + - + - + - + - + - + - + - - + + @@ -9779,11 +9811,11 @@ - + - - + + @@ -9794,17 +9826,17 @@ - - - - - - - - - - - + + + + + + + + + + + @@ -9812,11 +9844,11 @@ - + - - + + @@ -9824,11 +9856,11 @@ - + - - + + @@ -9836,11 +9868,11 @@ - + - - + + @@ -9848,11 +9880,11 @@ - + - - + + @@ -9860,11 +9892,11 @@ - + - - + + @@ -9872,11 +9904,11 @@ - + - - + + @@ -9884,11 +9916,11 @@ - + - - + + @@ -9896,66 +9928,66 @@ - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -9986,12 +10018,12 @@ - + - + - + @@ -10006,286 +10038,286 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -10300,28 +10332,28 @@ - + - + - + - + - + - + - + - + - + @@ -10336,26 +10368,26 @@ - + - + - + - + - + - + - + - + @@ -10366,12 +10398,12 @@ - + - + - + @@ -10386,112 +10418,112 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -10506,51 +10538,51 @@ - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -10560,233 +10592,233 @@ - + - + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - + + + + - - - - - - - + + + + + + + - - + + - - - - - - - + + + + + + + - - + + - - - - - - + + + + + + - - + + - - - + + + - - + + - + - + - - + + - + - - + + - - - + + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - + + + + + + + + + - - - + + + - - + + - - + + - - + + - - - + + + @@ -10807,115 +10839,115 @@ - + - - + + - + - + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + - + - + - + - + - - + + - - + + - - + + - + - + - - - + + + - - + + - + - - + + @@ -10947,140 +10979,140 @@ - - - + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - - + + - + - - + + - - + + - - + + - - + + - + - + - + @@ -11088,31 +11120,31 @@ - + - - + + - + - - + + - + - - + + - + - + - + @@ -11120,8 +11152,8 @@ - - + + @@ -11135,8 +11167,8 @@ - - + + @@ -11144,32 +11176,32 @@ - - + + - + - + - + - + - + - + - + - - + + @@ -11240,13 +11272,13 @@ - + - + @@ -11255,18 +11287,18 @@ - + - + - - - - + + + + @@ -11278,7 +11310,7 @@ - + @@ -11310,7 +11342,7 @@ - + @@ -11319,10 +11351,10 @@ - - - - + + + + @@ -11330,8 +11362,8 @@ - - + + @@ -11384,8 +11416,8 @@ - - + + @@ -11393,9 +11425,9 @@ - - - + + + @@ -11403,7 +11435,7 @@ - + @@ -11411,8 +11443,8 @@ - - + + @@ -11444,8 +11476,8 @@ - - + + @@ -11507,16 +11539,16 @@ - + - + - + - + @@ -11594,7 +11626,7 @@ - + @@ -11639,8 +11671,8 @@ - - + + @@ -11651,26 +11683,26 @@ - + - + - + - + - - + + - + - + @@ -11678,7 +11710,7 @@ - + @@ -11702,20 +11734,20 @@ - - + + - + - + - - + + @@ -11741,48 +11773,48 @@ - - - + + + - + - + - + - + - - - + + + - + - - + + - + - - + + - + @@ -11794,8 +11826,8 @@ - - + + @@ -11803,7 +11835,7 @@ - + @@ -11814,7 +11846,7 @@ - + @@ -11835,13 +11867,13 @@ - + - + - + @@ -11871,10 +11903,10 @@ - + - + @@ -11898,7 +11930,7 @@ - + @@ -11910,10 +11942,10 @@ - + - + @@ -11939,11 +11971,11 @@ - - - - - + + + + + @@ -11954,24 +11986,24 @@ - + - + - - + + - + - - - + + + @@ -11979,8 +12011,8 @@ - - + + @@ -11991,10 +12023,10 @@ - + - + @@ -12007,21 +12039,24 @@ + + + - - + + - + - + - + - - + + @@ -12758,19 +12793,19 @@ - - + + - + - + - + @@ -12778,11 +12813,11 @@ - + - - + + @@ -12793,16 +12828,16 @@ - - + + - + - + - + @@ -12811,25 +12846,25 @@ - - - - - + + + + + - + - + - + @@ -12838,87 +12873,87 @@ - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -12932,29 +12967,29 @@ - + - - + + - + - + - + - + @@ -12963,7 +12998,7 @@ - + @@ -12971,10 +13006,10 @@ - + - + @@ -12983,13 +13018,13 @@ - + - - + + - + @@ -13004,29 +13039,29 @@ - + - + - + - + - + - + - + @@ -13035,8 +13070,8 @@ - - + + @@ -13053,21 +13088,21 @@ - + - + - + - + - + @@ -13076,7 +13111,7 @@ - + @@ -13093,7 +13128,7 @@ - + @@ -13119,18 +13154,18 @@ - + - + - + - + @@ -13142,30 +13177,30 @@ - - + + - + - + - + - + - + - + @@ -13217,13 +13252,13 @@ - + - + @@ -13232,23 +13267,23 @@ - + - + - + - + - - + + - + @@ -13257,7 +13292,7 @@ - + @@ -13268,7 +13303,7 @@ - + @@ -13282,16 +13317,16 @@ - + - + - + - + @@ -13309,2555 +13344,2560 @@ - + - + - + - + - + - + - + - + - + - + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + @@ -15866,8 +15906,8 @@ - - + + @@ -15878,25 +15918,25 @@ - - + + - + - - + + - + - - + + - + - + @@ -15905,22 +15945,22 @@ - - - - - - - + + + + + + + - + - + - - + + @@ -15928,50 +15968,50 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -15994,15 +16034,15 @@ - + - + - + - + @@ -16011,23 +16051,23 @@ - + - + - + - + @@ -16041,7 +16081,7 @@ - + @@ -16056,16 +16096,16 @@ - + - + - + - + @@ -16074,13 +16114,13 @@ - + - + - + @@ -16110,10 +16150,10 @@ - + - + @@ -16122,7 +16162,7 @@ - + @@ -16134,88 +16174,88 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -16224,22 +16264,22 @@ - + - + - + - + - + - + @@ -16253,118 +16293,118 @@ - + - + - + - + - + - + - + - + - - - + + + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -16379,34 +16419,34 @@ - + - + - + - - + + - + - + - + - + - + - - + + - + @@ -16415,7 +16455,7 @@ - + @@ -16423,42 +16463,42 @@ - + - + - + - + - + - + - + - + - + - + - + - + @@ -16466,22 +16506,22 @@ - - + + - + - + - + - + @@ -16492,42 +16532,42 @@ - + - + - + - + - + - + - + - + - + - + - + - + @@ -16535,9 +16575,9 @@ - + - + @@ -16546,8 +16586,8 @@ - - + + @@ -16564,34 +16604,34 @@ - + - + - + - + - + - + - + - + - + @@ -16600,37 +16640,37 @@ - + - + - + - + - + - + - + - + - + - + - + @@ -16639,33 +16679,36 @@ - + - + - + - + - + - + - + - + + + + - + - + @@ -16677,31 +16720,31 @@ - + - + - + - + - + - + - + - + - + - + @@ -16713,23 +16756,23 @@ - + - + - + - + - + - + @@ -16737,9 +16780,9 @@ - + - + @@ -16748,13 +16791,13 @@ - + - + @@ -16762,29 +16805,29 @@ - + - + - + - + - + - + - + - + - + @@ -16793,28 +16836,28 @@ - + - + - + - + - + - + - + @@ -16825,20 +16868,20 @@ - + - + - + - + - + @@ -16849,12 +16892,12 @@ - + - + - + @@ -16874,45 +16917,45 @@ - - + + - + - + - - + + - + - + - - + + - + - + - + - + @@ -16923,10 +16966,10 @@ - + - + @@ -16934,18 +16977,18 @@ - + - + - + - + - + @@ -16959,15 +17002,15 @@ - - - - - - - - - + + + + + + + + + @@ -16981,18 +17024,18 @@ - - - - + + + + - + - + @@ -17000,44 +17043,44 @@ - - + + - + - - - + + + - + - + - - + + - + - + - - + + - + @@ -17049,21 +17092,21 @@ - + - + - + - - - - - - - + + + + + + + @@ -17086,30 +17129,30 @@ - + - + - + - + - - + + - + - + - + - + @@ -17118,28 +17161,28 @@ - + - - - - - - + + + + + + - - + + - + - - + + @@ -17147,9 +17190,9 @@ - - - + + + @@ -17163,36 +17206,36 @@ - + - + - + - + - + - + - + - + - + - + - + @@ -17200,7 +17243,7 @@ - + @@ -17208,10 +17251,10 @@ - + - + @@ -17219,10 +17262,10 @@ - + - + @@ -17239,7 +17282,7 @@ - + @@ -17247,18 +17290,18 @@ - + - + - + - + - + @@ -17266,7 +17309,7 @@ - + @@ -17274,7 +17317,7 @@ - + @@ -17285,8 +17328,8 @@ - - + + @@ -17297,111 +17340,111 @@ - - - + + + - + - - - - + + + + - - - - - - - - + + + + + + + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - - + + + @@ -17419,25 +17462,25 @@ - - + + - + - + - + - + @@ -17478,163 +17521,163 @@ - + - + - - + + - - + + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -17642,8 +17685,8 @@ - - + + @@ -17656,71 +17699,71 @@ - - + + - + - + - - + + - + - + - - + + - + - + - - + + - + - + - - + + - + - + - - + + - + - + - + - - + + - + - - + + @@ -17728,8 +17771,8 @@ - - + + @@ -17737,29 +17780,29 @@ - - + + - - - - - - - - - - - - - + + + + + + + + + + + - + + + @@ -17797,7 +17840,7 @@ - + @@ -17827,22 +17870,22 @@ - + - + - + - + - + @@ -17893,13 +17936,13 @@ - + - + @@ -17915,1028 +17958,1028 @@ - - - + + + - - - + + + - - - - + + + + - + - - + + - + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - - - + + + + + - - + + - - - - - + + + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - - - + + + + - - + + - - - - + + + + - - + + - - + + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - - + + - - + + - + - - + + - + - - + + - + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - - + + + - - + + - - + + - - + + - - - - + + + + - - - - - + + + + + - + - - + + - + - - + + - + - - + + - - - + + + - - + + - - - - + + + + - - + + - + - - + + - - + + - - + + - + - - + + - - + + - - + + - + - - + + - + - - + + - + - + - + - - + + - - + + - - + + - - + + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - + - + - - - + + + - + - + - + - + - + - - - - + + + + - + - + - + - - + + - - + + - + - - - + + + - - - - + + + + - + - + - - + + - - - + + + - - - - + + + + - - - + + + - - - + + + - - - - + + + + - - - - + + + + - - - + + + - - - + + + - - - - + + + + - - - + + + - - - + + + - - - - - + + + + + - - - - + + + + - - - - - + + + + + - - - - - - - + + + + + + + - - + + - - - - - - + + + + + + - - + + - - - + + + - - - - + + + + - - - - + + + + - + - + - - - + + + - - - + + + - - - + + + - - + + - - - + + + - - - + + + - - - + + + - - + + - - + + - - - + + + - - - + + + - - - - + + + + - - + + - - + + - - - - + + + + - - - + + + - - + + - - - + + + - - + + - - + + - - - + + + - - + + - - - - - + + + + + - - + + - + - - - + + + - - + + - + - - + + - + @@ -18944,8 +18987,8 @@ - - + + @@ -18961,35 +19004,35 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - - - + + + + @@ -19003,40 +19046,40 @@ - - - + + + - - + + - - + + - - + + - - + + - - + + - - - + + + - - + + - + @@ -19044,189 +19087,189 @@ - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - - + + - + - + - + - - - + + + - + - + - + - + - - + + - + - - + + @@ -19246,43 +19289,43 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -19297,16 +19340,16 @@ - + - + - + @@ -19324,22 +19367,22 @@ - + - + - + - + - + @@ -19378,17 +19421,17 @@ - - - + + + - + - + @@ -19397,7 +19440,7 @@ - + @@ -19414,10 +19457,10 @@ - - - - + + + + @@ -19425,25 +19468,25 @@ - + - - - + + + - - + + - - + + @@ -19459,20 +19502,20 @@ - - + + - - - - - - + + + + + + @@ -19480,11 +19523,11 @@ - + - - + + @@ -19492,11 +19535,11 @@ - + - - + + @@ -19504,11 +19547,11 @@ - + - - + + @@ -19516,11 +19559,11 @@ - + - - + + @@ -19550,12 +19593,12 @@ - + - + - + @@ -19570,329 +19613,329 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -19907,12 +19950,12 @@ - + - + - + @@ -19927,9 +19970,9 @@ - - - + + + @@ -19943,23 +19986,23 @@ - + - + - + - + - + @@ -19967,43 +20010,43 @@ - - - - + + + + - - - - - + + + + + - - + + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - + + @@ -20038,24 +20081,24 @@ - - - + + + - - - - - - - - + + + + + + + + - - - + + + @@ -20093,10 +20136,10 @@ - + - + @@ -20105,31 +20148,31 @@ - + - + - + - + - + - + - + @@ -20141,11 +20184,11 @@ - + - + @@ -20156,11 +20199,11 @@ - + - - + + @@ -20183,14 +20226,14 @@ - + - - + + @@ -20198,10 +20241,10 @@ - - + + - + @@ -20210,7 +20253,7 @@ - + @@ -20218,13 +20261,13 @@ - - + + - + - + @@ -20236,10 +20279,10 @@ - + - + @@ -20275,10 +20318,10 @@ - + - + @@ -20290,19 +20333,19 @@ - - + + - + - + @@ -20328,14 +20371,14 @@ - + - + - + @@ -20380,8 +20423,8 @@ - - + + @@ -20419,7 +20462,7 @@ - + @@ -20449,22 +20492,22 @@ - + - + - + - + - + @@ -20515,13 +20558,13 @@ - + - + @@ -20536,82 +20579,82 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - - - + + + + + + + - - - - - + + + + + - - + + - - - - + + + + @@ -20651,11 +20694,11 @@ - + - + @@ -20665,32 +20708,32 @@ - - + + - - + + - - + + - - + + - + - + @@ -20816,7 +20859,7 @@ - + @@ -20824,11 +20867,11 @@ - + - + @@ -20836,7 +20879,7 @@ - + @@ -20847,155 +20890,155 @@ - - + + - + - + - + - + - + - + - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + @@ -21059,7 +21102,7 @@ - + @@ -21077,7 +21120,7 @@ - + @@ -21119,14 +21162,14 @@ - - + + - + - + @@ -21144,10 +21187,10 @@ - - - - + + + + @@ -21178,7 +21221,7 @@ - + @@ -21186,11 +21229,11 @@ - + - + @@ -21243,7 +21286,7 @@ - + @@ -21317,7 +21360,7 @@ - + @@ -21413,7 +21456,7 @@ - + @@ -21425,7 +21468,7 @@ - + @@ -21490,24 +21533,24 @@ - + - + - + - + - + @@ -21515,10 +21558,10 @@ - + - + @@ -21583,13 +21626,13 @@ - + - + @@ -21651,44 +21694,44 @@ - + - + - + - + - + - + - + @@ -21696,7 +21739,7 @@ - + @@ -21704,25 +21747,25 @@ - + - + - + - + @@ -21744,7 +21787,7 @@ - + @@ -21782,35 +21825,35 @@ - + - - + + - + - - - + + + - + @@ -21818,7 +21861,7 @@ - + @@ -21827,7 +21870,7 @@ - + @@ -21924,22 +21967,22 @@ - + - + - + - + @@ -21974,38 +22017,38 @@ - + - + - - + + - + - + - - - - - + + + + + - - - - - + + + + + - + @@ -22042,42 +22085,42 @@ - + + - - + - + - + - + - + - - + + - - + + @@ -22118,13 +22161,13 @@ - + - + @@ -22136,7 +22179,7 @@ - + @@ -22237,16 +22280,16 @@ - - + + - - + + - + - + @@ -22260,17 +22303,17 @@ - - + + - + - - + + @@ -22284,8 +22327,8 @@ - - + + @@ -22296,8 +22339,8 @@ - - + + @@ -22305,16 +22348,16 @@ - + - + - - + + - + @@ -22332,8 +22375,8 @@ - - + + @@ -22371,8 +22414,8 @@ - - + + @@ -22380,46 +22423,46 @@ - + - + - + - - + + - - + + - + - - + + - + - + - + - + - + @@ -22455,23 +22498,23 @@ - - + + - + - + - + - + - - + + @@ -22481,8 +22524,8 @@ - - + + @@ -22490,16 +22533,16 @@ - - + + - + - + @@ -22517,7 +22560,7 @@ - + @@ -22526,7 +22569,7 @@ - + @@ -22549,7 +22592,7 @@ - + @@ -22567,7 +22610,7 @@ - + @@ -22618,30 +22661,30 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - + @@ -22649,39 +22692,39 @@ - - - + + + - + - + - - + + - + - + - + - + - + @@ -22691,65 +22734,65 @@ - + - + - + - + - + - - - + + + - + - + - + - + - + - + - + - + @@ -22773,11 +22816,11 @@ - + - + @@ -22792,13 +22835,13 @@ - + - + @@ -22808,44 +22851,44 @@ - + - + - + - + - + - + - - + + - + - + - + - + @@ -23055,27 +23098,27 @@ - + - + - + - + - - - - - + + + + + @@ -23120,7 +23163,7 @@ - + @@ -23135,12 +23178,12 @@ - + - + @@ -23203,7 +23246,7 @@ - + @@ -23277,15 +23320,15 @@ - + - + - + @@ -23303,7 +23346,7 @@ - + @@ -23320,7 +23363,7 @@ - + @@ -23328,15 +23371,15 @@ - - - - - - - - - + + + + + + + + + @@ -23408,9 +23451,9 @@ - + - + @@ -23422,7 +23465,7 @@ - + @@ -23461,7 +23504,7 @@ - + @@ -23595,8 +23638,8 @@ - - + + @@ -23608,18 +23651,15 @@ - - - - - + + - + @@ -23627,54 +23667,31 @@ - + - - - - - - - - - - - - - - + + + - - - - + + + + - - - - - - - - - - - - + + + + - - + + - - - - - - + + @@ -23682,7 +23699,7 @@ - + @@ -23690,8 +23707,8 @@ - - + + @@ -23701,12 +23718,12 @@ - - - + + + - + @@ -23796,7 +23813,7 @@ - + @@ -23874,7 +23891,7 @@ - + @@ -23885,16 +23902,16 @@ - + - - + + - + - + @@ -23909,24 +23926,24 @@ - - - - - - - - - - - - + + + + + + + + + + + + - - + + - - + + @@ -23945,7 +23962,7 @@ - + @@ -23968,13 +23985,13 @@ - + - - + + @@ -23999,30 +24016,30 @@ - + - + - - + + - - + + - + - + @@ -24036,7 +24053,7 @@ - + @@ -24045,7 +24062,7 @@ - + @@ -24087,12 +24104,12 @@ - + - + @@ -24120,27 +24137,27 @@ - + - + - + - + - + @@ -24161,7 +24178,7 @@ - + @@ -24196,12 +24213,12 @@ - + - + @@ -24219,24 +24236,24 @@ - - + + - + - - + + - + - - - - - - + + + + + + @@ -24253,8 +24270,8 @@ - - + + @@ -24281,12 +24298,12 @@ - + - + @@ -24294,7 +24311,7 @@ - + @@ -24303,7 +24320,7 @@ - + @@ -24313,18 +24330,18 @@ - - + + - + - + - + @@ -24342,34 +24359,34 @@ - + - + - + - + - + - + - + - + - - - - + + + + @@ -24543,7 +24560,7 @@ - + @@ -24575,55 +24592,55 @@ - + - + - + - + - + - - - - + + + + - + - - + + - + - + - + - + - + - + - + @@ -24631,7 +24648,7 @@ - + @@ -24639,26 +24656,26 @@ - + - + - + - + - - + + - + @@ -24670,22 +24687,22 @@ - - - - - + + + + + - + - + - + @@ -24708,20 +24725,20 @@ - - + + - + - + - + - - + + @@ -24735,23 +24752,23 @@ - - - - + + + + - - - - - - - - - - - - + + + + + + + + + + + + @@ -24786,7 +24803,7 @@ - + @@ -24896,17 +24913,17 @@ - + - - + + - + @@ -24925,7 +24942,7 @@ - + @@ -25129,12 +25146,12 @@ - - + + - + @@ -25169,7 +25186,7 @@ - + @@ -25277,7 +25294,7 @@ - + @@ -25286,8 +25303,8 @@ - - + + @@ -25323,24 +25340,24 @@ - - + + - + - + - + @@ -25357,8 +25374,8 @@ - - + + @@ -25402,25 +25419,25 @@ - + - - + + - - - + + + - + - + @@ -25428,7 +25445,7 @@ - + @@ -25561,68 +25578,72 @@ - - - - - - + + + + + + - - - - - - + + + + + + - - - + + + - - + + - - - + + + - - + + - - + + + + + + - - - + + + - - + + - - - + + + - - - + + + - - - + + + - - + + @@ -25654,7 +25675,7 @@ - + @@ -25667,8 +25688,8 @@ - - + + @@ -25679,8 +25700,8 @@ - - + + @@ -25890,7 +25911,7 @@ - + @@ -25904,16 +25925,16 @@ - - + + - + - + @@ -25951,25 +25972,25 @@ - - - - - - - + + + + + + + - + - + - - + + - - + + @@ -25983,15 +26004,15 @@ - - - + + + - - - - + + + + @@ -26004,127 +26025,127 @@ - + - - + + - - + + - + - - + + - - + + - + - + - - + + - + - + - + - + - + - + - - + + - - - + + + - - - + + + - - - + + + - + - + - + - + - + - - + + - + - - + + - - + + @@ -26138,12 +26159,12 @@ - - + + - - + + @@ -26193,55 +26214,55 @@ - + - - + + - + - + - + - + - - + + - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - + + + @@ -26257,81 +26278,81 @@ - + - + - + - + - - + + - + - + - - + + - + - + - + - + - + - + - + - + - + @@ -26368,15 +26389,15 @@ - + - + - + @@ -26401,7 +26422,7 @@ - + @@ -26446,18 +26467,18 @@ - + - + - + diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h index 3394726..f177cd9 100644 --- a/Include/cpython/unicodeobject.h +++ b/Include/cpython/unicodeobject.h @@ -140,9 +140,11 @@ typedef struct { and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is set, use the PyASCIIObject structure. */ unsigned int ascii:1; + /* The object is statically allocated. */ + unsigned int statically_allocated:1; /* Padding to ensure that PyUnicode_DATA() is always aligned to 4 bytes (see issue #19537 on m68k). */ - unsigned int :25; + unsigned int :24; } state; } PyASCIIObject; diff --git a/Include/internal/pycore_global_objects.h b/Include/internal/pycore_global_objects.h index 5a3fb13..442f851 100644 --- a/Include/internal/pycore_global_objects.h +++ b/Include/internal/pycore_global_objects.h @@ -8,6 +8,7 @@ extern "C" { # error "this header requires Py_BUILD_CORE define" #endif +#include "pycore_hashtable.h" // _Py_hashtable_t #include "pycore_gc.h" // PyGC_Head #include "pycore_global_strings.h" // struct _Py_global_strings #include "pycore_hamt.h" // PyHamtNode_Bitmap @@ -28,6 +29,11 @@ extern "C" { #define _Py_SINGLETON(NAME) \ _Py_GLOBAL_OBJECT(singletons.NAME) +struct _Py_cached_objects { + // XXX We could statically allocate the hashtable. + _Py_hashtable_t *interned_strings; +}; + struct _Py_static_objects { struct { /* Small integers are preallocated in this array so that they diff --git a/Include/internal/pycore_hashtable.h b/Include/internal/pycore_hashtable.h index 6501ab1..f57978a 100644 --- a/Include/internal/pycore_hashtable.h +++ b/Include/internal/pycore_hashtable.h @@ -106,6 +106,7 @@ PyAPI_FUNC(int) _Py_hashtable_foreach( void *user_data); PyAPI_FUNC(size_t) _Py_hashtable_size(const _Py_hashtable_t *ht); +PyAPI_FUNC(size_t) _Py_hashtable_len(const _Py_hashtable_t *ht); /* Add a new entry to the hash. The key must not be present in the hash table. Return 0 on success, -1 on memory error. */ diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h index 24179b0..9b5e123 100644 --- a/Include/internal/pycore_runtime.h +++ b/Include/internal/pycore_runtime.h @@ -161,6 +161,7 @@ typedef struct pyruntimestate { /* All the objects that are shared by the runtime's interpreters. */ struct _Py_static_objects static_objects; + struct _Py_cached_objects cached_objects; /* The value to use for sys.path[0] in new subinterpreters. Normally this would be part of the PyConfig struct. However, diff --git a/Include/internal/pycore_runtime_init.h b/Include/internal/pycore_runtime_init.h index 7aace9f..660d7d7 100644 --- a/Include/internal/pycore_runtime_init.h +++ b/Include/internal/pycore_runtime_init.h @@ -164,6 +164,7 @@ extern PyTypeObject _PyExc_MemoryError; .kind = 1, \ .compact = 1, \ .ascii = (ASCII), \ + .statically_allocated = 1, \ }, \ } #define _PyASCIIObject_INIT(LITERAL) \ diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 0b8529a..446dc53 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -15,6 +15,7 @@ from test.support import os_helper from test.support.script_helper import assert_python_ok, assert_python_failure from test.support import threading_helper from test.support import import_helper +from test.support import interpreters import textwrap import unittest import warnings @@ -699,6 +700,33 @@ class SysModuleTest(unittest.TestCase): self.assertRaises(TypeError, sys.intern, S("abc")) + def test_subinterp_intern_dynamically_allocated(self): + s = "never interned before" + str(random.randrange(0, 10**9)) + t = sys.intern(s) + self.assertIs(t, s) + + interp = interpreters.create() + interp.run(textwrap.dedent(f''' + import sys + t = sys.intern({s!r}) + assert id(t) != {id(s)}, (id(t), {id(s)}) + assert id(t) != {id(t)}, (id(t), {id(t)}) + ''')) + + def test_subinterp_intern_statically_allocated(self): + # See Tools/build/generate_global_objects.py for the list + # of strings that are always statically allocated. + s = '__init__' + t = sys.intern(s) + + print('------------------------') + interp = interpreters.create() + interp.run(textwrap.dedent(f''' + import sys + t = sys.intern({s!r}) + assert id(t) == {id(t)}, (id(t), {id(t)}) + ''')) + def test_sys_flags(self): self.assertTrue(sys.flags) attrs = ("debug", diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-07-25-15-29-26.gh-issue-106931.kKU1le.rst b/Misc/NEWS.d/next/Core and Builtins/2023-07-25-15-29-26.gh-issue-106931.kKU1le.rst new file mode 100644 index 0000000..e0def53 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-07-25-15-29-26.gh-issue-106931.kKU1le.rst @@ -0,0 +1,3 @@ +Statically allocated string objects are now interned globally instead of +per-interpreter. This fixes a situation where such a string would only be +interned in a single interpreter. Normal string objects are unaffected. diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 26aa139..ec82eda 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -235,15 +235,54 @@ static inline PyObject *get_interned_dict(PyInterpreterState *interp) return _Py_INTERP_CACHED_OBJECT(interp, interned_strings); } +#define INTERNED_STRINGS _PyRuntime.cached_objects.interned_strings + Py_ssize_t _PyUnicode_InternedSize(void) { - return PyObject_Length(get_interned_dict(_PyInterpreterState_GET())); + PyObject *dict = get_interned_dict(_PyInterpreterState_GET()); + return _Py_hashtable_len(INTERNED_STRINGS) + PyDict_GET_SIZE(dict); +} + +static Py_hash_t unicode_hash(PyObject *); +static int unicode_compare_eq(PyObject *, PyObject *); + +static Py_uhash_t +hashtable_unicode_hash(const void *key) +{ + return unicode_hash((PyObject *)key); +} + +static int +hashtable_unicode_compare(const void *key1, const void *key2) +{ + PyObject *obj1 = (PyObject *)key1; + PyObject *obj2 = (PyObject *)key2; + if (obj1 != NULL && obj2 != NULL) { + return unicode_compare_eq(obj1, obj2); + } + else { + return obj1 == obj2; + } } static int init_interned_dict(PyInterpreterState *interp) { + if (_Py_IsMainInterpreter(interp)) { + assert(INTERNED_STRINGS == NULL); + _Py_hashtable_allocator_t hashtable_alloc = {PyMem_RawMalloc, PyMem_RawFree}; + INTERNED_STRINGS = _Py_hashtable_new_full( + hashtable_unicode_hash, + hashtable_unicode_compare, + NULL, + NULL, + &hashtable_alloc + ); + if (INTERNED_STRINGS == NULL) { + return -1; + } + } assert(get_interned_dict(interp) == NULL); PyObject *interned = interned = PyDict_New(); if (interned == NULL) { @@ -262,6 +301,10 @@ clear_interned_dict(PyInterpreterState *interp) Py_DECREF(interned); _Py_INTERP_CACHED_OBJECT(interp, interned_strings) = NULL; } + if (_Py_IsMainInterpreter(interp) && INTERNED_STRINGS != NULL) { + _Py_hashtable_destroy(INTERNED_STRINGS); + INTERNED_STRINGS = NULL; + } } #define _Py_RETURN_UNICODE_EMPTY() \ @@ -1222,6 +1265,7 @@ PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar) _PyUnicode_STATE(unicode).kind = kind; _PyUnicode_STATE(unicode).compact = 1; _PyUnicode_STATE(unicode).ascii = is_ascii; + _PyUnicode_STATE(unicode).statically_allocated = 0; if (is_ascii) { ((char*)data)[size] = 0; } @@ -1552,7 +1596,9 @@ unicode_dealloc(PyObject *unicode) * we accidentally decref an immortal string out of existence. Since * the string is an immortal object, just re-set the reference count. */ - if (PyUnicode_CHECK_INTERNED(unicode)) { + if (PyUnicode_CHECK_INTERNED(unicode) + || _PyUnicode_STATE(unicode).statically_allocated) + { _Py_SetImmortal(unicode); return; } @@ -14502,6 +14548,7 @@ unicode_subtype_new(PyTypeObject *type, PyObject *unicode) _PyUnicode_STATE(self).kind = kind; _PyUnicode_STATE(self).compact = 0; _PyUnicode_STATE(self).ascii = _PyUnicode_STATE(unicode).ascii; + _PyUnicode_STATE(self).statically_allocated = 0; _PyUnicode_UTF8_LENGTH(self) = 0; _PyUnicode_UTF8(self) = NULL; _PyUnicode_DATA_ANY(self) = NULL; @@ -14725,6 +14772,23 @@ _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p) return; } + /* Look in the global cache first. */ + PyObject *r = (PyObject *)_Py_hashtable_get(INTERNED_STRINGS, s); + if (r != NULL && r != s) { + Py_SETREF(*p, Py_NewRef(r)); + return; + } + + /* Handle statically allocated strings. */ + if (_PyUnicode_STATE(s).statically_allocated) { + assert(_Py_IsImmortal(s)); + if (_Py_hashtable_set(INTERNED_STRINGS, s, s) == 0) { + _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL_STATIC; + } + return; + } + + /* Look in the per-interpreter cache. */ PyObject *interned = get_interned_dict(interp); assert(interned != NULL); @@ -14740,9 +14804,11 @@ _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p) } if (_Py_IsImmortal(s)) { + // XXX Restrict this to the main interpreter? _PyUnicode_STATE(*p).interned = SSTATE_INTERNED_IMMORTAL_STATIC; - return; + return; } + #ifdef Py_REF_DEBUG /* The reference count value excluding the 2 references from the interned dictionary should be excluded from the RefTotal. The diff --git a/Python/hashtable.c b/Python/hashtable.c index 09501de..9f005c7 100644 --- a/Python/hashtable.c +++ b/Python/hashtable.c @@ -128,6 +128,13 @@ _Py_hashtable_size(const _Py_hashtable_t *ht) } +size_t +_Py_hashtable_len(const _Py_hashtable_t *ht) +{ + return ht->nentries; +} + + _Py_hashtable_entry_t * _Py_hashtable_get_entry_generic(_Py_hashtable_t *ht, const void *key) { diff --git a/Tools/build/deepfreeze.py b/Tools/build/deepfreeze.py index b084d3e..a11fe6a 100644 --- a/Tools/build/deepfreeze.py +++ b/Tools/build/deepfreeze.py @@ -208,6 +208,7 @@ class Printer: self.write(".kind = 1,") self.write(".compact = 1,") self.write(".ascii = 1,") + self.write(".statically_allocated = 1,") self.write(f"._data = {make_string_literal(s.encode('ascii'))},") return f"& {name}._ascii.ob_base" else: @@ -220,6 +221,7 @@ class Printer: self.write(f".kind = {kind},") self.write(".compact = 1,") self.write(".ascii = 0,") + self.write(".statically_allocated = 1,") utf8 = s.encode('utf-8') self.write(f'.utf8 = {make_string_literal(utf8)},') self.write(f'.utf8_length = {len(utf8)},') -- cgit v0.12