mirror of
https://github.com/python/cpython.git
synced 2025-08-02 08:02:56 +00:00
bpo-46712: Share global string identifiers in deepfreeze (GH-31261)
Where appropriate, deepfreeze.c now uses `&_Py_ID(blah)` references instead of locally defining constants. This saves some space.
This commit is contained in:
parent
ea9612a17b
commit
eb002dbe0d
3 changed files with 14 additions and 6 deletions
|
@ -0,0 +1 @@
|
||||||
|
Share global string identifiers in deep-frozen modules.
|
|
@ -15,9 +15,10 @@ import types
|
||||||
from typing import Dict, FrozenSet, TextIO, Tuple
|
from typing import Dict, FrozenSet, TextIO, Tuple
|
||||||
|
|
||||||
import umarshal
|
import umarshal
|
||||||
|
from generate_global_objects import get_identifiers_and_strings
|
||||||
|
|
||||||
verbose = False
|
verbose = False
|
||||||
|
identifiers = get_identifiers_and_strings()[0]
|
||||||
|
|
||||||
def isprintable(b: bytes) -> bool:
|
def isprintable(b: bytes) -> bool:
|
||||||
return all(0x20 <= c < 0x7f for c in b)
|
return all(0x20 <= c < 0x7f for c in b)
|
||||||
|
@ -167,6 +168,8 @@ class Printer:
|
||||||
return f"& {name}.ob_base.ob_base"
|
return f"& {name}.ob_base.ob_base"
|
||||||
|
|
||||||
def generate_unicode(self, name: str, s: str) -> str:
|
def generate_unicode(self, name: str, s: str) -> str:
|
||||||
|
if s in identifiers:
|
||||||
|
return f"&_Py_ID({s})"
|
||||||
kind, ascii = analyze_character_width(s)
|
kind, ascii = analyze_character_width(s)
|
||||||
if kind == PyUnicode_1BYTE_KIND:
|
if kind == PyUnicode_1BYTE_KIND:
|
||||||
datatype = "uint8_t"
|
datatype = "uint8_t"
|
||||||
|
|
|
@ -256,13 +256,10 @@ def generate_runtime_init(identifiers, strings):
|
||||||
printer.write(after)
|
printer.write(after)
|
||||||
|
|
||||||
|
|
||||||
#######################################
|
def get_identifiers_and_strings() -> tuple[set[str], dict[str, str]]:
|
||||||
# the script
|
|
||||||
|
|
||||||
def main() -> None:
|
|
||||||
identifiers = set(IDENTIFIERS)
|
identifiers = set(IDENTIFIERS)
|
||||||
strings = dict(STRING_LITERALS)
|
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 string is None:
|
||||||
if name not in IGNORED:
|
if name not in IGNORED:
|
||||||
identifiers.add(name)
|
identifiers.add(name)
|
||||||
|
@ -271,6 +268,13 @@ def main() -> None:
|
||||||
strings[name] = string
|
strings[name] = string
|
||||||
elif string != strings[name]:
|
elif string != strings[name]:
|
||||||
raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')
|
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_global_strings(identifiers, strings)
|
||||||
generate_runtime_init(identifiers, strings)
|
generate_runtime_init(identifiers, strings)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue