Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46443: deepfreeze use preallocated small ints #30715

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deepfreeze now uses cached small integers as it saves some space for common small integers.
5 changes: 5 additions & 0 deletions Tools/scripts/deepfreeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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:
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
return f"(PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS + {i}]"
if abs(i) < 2**15:
self._generate_int_for_bits(name, i, 2**15)
else:
Expand Down