bpo-46608: exclude marshalled-frozen data if deep-freezing to save 300 KB space (GH-31074)

This reduces the size of the data segment by **300 KB** of the executable because if the modules are deep-frozen then the marshalled frozen data just wastes space. This was inspired by comment by @gvanrossum in https://github.com/python/cpython/pull/29118#issuecomment-958521863. Note: There is a new option `--deepfreeze-only` in `freeze_modules.py` to change this behavior, it is on be default to save disk space.
```console 
# du -s ./python before
27892   ./python
# du -s ./python after
27524   ./python
```

Automerge-Triggered-By: GH:ericsnowcurrently
This commit is contained in:
Kumar Aditya 2022-02-04 23:27:03 +05:30 committed by GitHub
parent 9b4e3d94a5
commit bf95ff91f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 87 additions and 84 deletions

View file

@ -9,7 +9,7 @@ import os
import ntpath
import posixpath
import sys
import argparse
from update_file import updating_file_with_tmpfile
@ -463,14 +463,15 @@ def replace_block(lines, start_marker, end_marker, replacements, file):
return lines[:start_pos + 1] + replacements + lines[end_pos:]
def regen_frozen(modules):
def regen_frozen(modules, deepfreeze_only: bool):
headerlines = []
parentdir = os.path.dirname(FROZEN_FILE)
for src in _iter_sources(modules):
# Adding a comment to separate sections here doesn't add much,
# so we don't.
header = relpath_for_posix_display(src.frozenfile, parentdir)
headerlines.append(f'#include "{header}"')
if not deepfreeze_only:
for src in _iter_sources(modules):
# Adding a comment to separate sections here doesn't add much,
# so we don't.
header = relpath_for_posix_display(src.frozenfile, parentdir)
headerlines.append(f'#include "{header}"')
externlines = []
bootstraplines = []
@ -500,9 +501,13 @@ def regen_frozen(modules):
externlines.append("extern PyObject *%s(void);" % get_code_name)
symbol = mod.symbol
pkg = '-' if mod.ispkg else ''
line = ('{"%s", %s, %s(int)sizeof(%s), GET_CODE(%s)},'
) % (mod.name, symbol, pkg, symbol, code_name)
pkg = 'true' if mod.ispkg else 'false'
if deepfreeze_only:
line = ('{"%s", NULL, 0, %s, GET_CODE(%s)},'
) % (mod.name, pkg, code_name)
else:
line = ('{"%s", %s, (int)sizeof(%s), %s, GET_CODE(%s)},'
) % (mod.name, symbol, symbol, pkg, code_name)
lines.append(line)
if mod.isalias:
@ -710,18 +715,19 @@ def regen_pcbuild(modules):
#######################################
# the script
def main():
def main(deepfreeze_only: bool):
# Expand the raw specs, preserving order.
modules = list(parse_frozen_specs())
# Regen build-related files.
regen_makefile(modules)
regen_pcbuild(modules)
regen_frozen(modules)
regen_frozen(modules, deepfreeze_only)
if __name__ == '__main__':
argv = sys.argv[1:]
if argv:
sys.exit(f'ERROR: got unexpected args {argv}')
main()
parser = argparse.ArgumentParser()
parser.add_argument("--deepfreeze-only", action="store_true",
help="Only use deepfrozen modules", default=True)
args = parser.parse_args()
main(args.deepfreeze_only)