]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46712: Share global string identifiers in deepfreeze (GH-31261)
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Fri, 25 Feb 2022 18:05:24 +0000 (23:35 +0530)
committerGitHub <noreply@github.com>
Fri, 25 Feb 2022 18:05:24 +0000 (10:05 -0800)
Where appropriate, deepfreeze.c now uses `&_Py_ID(blah)` references instead of locally defining constants. This saves some space.

Misc/NEWS.d/next/Core and Builtins/2022-02-24-07-50-43.bpo-46712.pw7vQV.rst [new file with mode: 0644]
Tools/scripts/deepfreeze.py
Tools/scripts/generate_global_objects.py

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-24-07-50-43.bpo-46712.pw7vQV.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-24-07-50-43.bpo-46712.pw7vQV.rst
new file mode 100644 (file)
index 0000000..9dbf7e0
--- /dev/null
@@ -0,0 +1 @@
+Share global string identifiers in deep-frozen modules.
index b62be3713feb8b76a3f62da8e26af6e3ae8d61e5..8ea232fc466908c145b2d5605bb42ab8ef5b310d 100644 (file)
@@ -15,9 +15,10 @@ import types
 from typing import Dict, FrozenSet, TextIO, Tuple
 
 import umarshal
+from generate_global_objects import get_identifiers_and_strings
 
 verbose = False
-
+identifiers = get_identifiers_and_strings()[0]
 
 def isprintable(b: bytes) -> bool:
     return all(0x20 <= c < 0x7f for c in b)
@@ -167,6 +168,8 @@ class Printer:
         return f"& {name}.ob_base.ob_base"
 
     def generate_unicode(self, name: str, s: str) -> str:
+        if s in identifiers:
+            return f"&_Py_ID({s})"
         kind, ascii = analyze_character_width(s)
         if kind == PyUnicode_1BYTE_KIND:
             datatype = "uint8_t"
index bad7865f1ff83bc4eae5a2e918e9e5b0bf4363d0..506aa86575c4e8a07cccca217de42fda79db7462 100644 (file)
@@ -256,13 +256,10 @@ def generate_runtime_init(identifiers, strings):
         printer.write(after)
 
 
-#######################################
-# the script
-
-def main() -> None:
+def get_identifiers_and_strings() -> tuple[set[str], dict[str, str]]:
     identifiers = set(IDENTIFIERS)
     strings = dict(STRING_LITERALS)
-    for name, string, filename, lno, _ in iter_global_strings():
+    for name, string, *_ in iter_global_strings():
         if string is None:
             if name not in IGNORED:
                 identifiers.add(name)
@@ -271,6 +268,13 @@ def main() -> None:
                 strings[name] = string
             elif string != strings[name]:
                 raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')
+    return identifiers, strings
+
+#######################################
+# the script
+
+def main() -> None:
+    identifiers, strings = get_identifiers_and_strings()
 
     generate_global_strings(identifiers, strings)
     generate_runtime_init(identifiers, strings)