Skip to content

Commit 194ecc6

Browse files
bpo-46443: deepfreeze: use small ints and singleton zero bytes (GH-30715)
1 parent 263c0dd commit 194ecc6

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deepfreeze now uses cached small integers as it saves some space for common small integers.

Tools/scripts/deepfreeze.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def __init__(self, file: TextIO):
113113
self.write('#include "Python.h"')
114114
self.write('#include "internal/pycore_gc.h"')
115115
self.write('#include "internal/pycore_code.h"')
116+
self.write('#include "internal/pycore_long.h"')
116117
self.write("")
117118

118119
@contextlib.contextmanager
@@ -148,6 +149,8 @@ def field(self, obj: object, name: str) -> None:
148149
self.write(f".{name} = {getattr(obj, name)},")
149150

150151
def generate_bytes(self, name: str, b: bytes) -> str:
152+
if b == b"":
153+
return "(PyObject *)&_Py_SINGLETON(bytes_empty)"
151154
self.write("static")
152155
with self.indent():
153156
with self.block("struct"):
@@ -313,6 +316,8 @@ def _generate_int_for_bits(self, name: str, i: int, digit: int) -> None:
313316
self.write(f".ob_digit = {{ {ds} }},")
314317

315318
def generate_int(self, name: str, i: int) -> str:
319+
if -5 <= i <= 256:
320+
return f"(PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + {i}]"
316321
if abs(i) < 2**15:
317322
self._generate_int_for_bits(name, i, 2**15)
318323
else:

0 commit comments

Comments
 (0)