[Python-checkins] bpo-46443: deepfreeze: use small ints and singleton zero bytes (GH-30715)

gvanrossum webhook-mailer at python.org
Thu Jan 20 01:13:37 EST 2022


https://github.com/python/cpython/commit/194ecc6d44adc1fb39a56ca696418368b69432ce
commit: 194ecc6d44adc1fb39a56ca696418368b69432ce
branch: main
author: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2022-01-19T22:13:21-08:00
summary:

bpo-46443: deepfreeze: use small ints and singleton zero bytes (GH-30715)

files:
A Misc/NEWS.d/next/Build/2022-01-20-05-27-07.bpo-46443.udCVII.rst
M Tools/scripts/deepfreeze.py

diff --git a/Misc/NEWS.d/next/Build/2022-01-20-05-27-07.bpo-46443.udCVII.rst b/Misc/NEWS.d/next/Build/2022-01-20-05-27-07.bpo-46443.udCVII.rst
new file mode 100644
index 0000000000000..8e3fa197be9da
--- /dev/null
+++ b/Misc/NEWS.d/next/Build/2022-01-20-05-27-07.bpo-46443.udCVII.rst
@@ -0,0 +1 @@
+Deepfreeze now uses cached small integers as it saves some space for common small integers.
\ No newline at end of file
diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py
index 002d680e10c2f..49638b8400285 100644
--- a/Tools/scripts/deepfreeze.py
+++ b/Tools/scripts/deepfreeze.py
@@ -113,6 +113,7 @@ def __init__(self, file: TextIO):
         self.write('#include "Python.h"')
         self.write('#include "internal/pycore_gc.h"')
         self.write('#include "internal/pycore_code.h"')
+        self.write('#include "internal/pycore_long.h"')
         self.write("")
 
     @contextlib.contextmanager
@@ -148,6 +149,8 @@ def field(self, obj: object, name: str) -> None:
         self.write(f".{name} = {getattr(obj, name)},")
 
     def generate_bytes(self, name: str, b: bytes) -> str:
+        if b == b"":
+            return "(PyObject *)&_Py_SINGLETON(bytes_empty)"
         self.write("static")
         with self.indent():
             with self.block("struct"):
@@ -313,6 +316,8 @@ def _generate_int_for_bits(self, name: str, i: int, digit: int) -> None:
                 self.write(f".ob_digit = {{ {ds} }},")
 
     def generate_int(self, name: str, i: int) -> str:
+        if -5 <= i <= 256:
+            return f"(PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + {i}]"
         if abs(i) < 2**15:
             self._generate_int_for_bits(name, i, 2**15)
         else:



More information about the Python-checkins mailing list