GH-98522: Add version number to code objects. (GH-98525)

* Add version number to code object for better versioning of functions.

* Improves specialization for closures and list comprehensions.
This commit is contained in:
Mark Shannon 2022-12-09 12:18:45 +00:00 committed by GitHub
parent 3c5355496b
commit fb713b2183
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 23 additions and 4 deletions

View file

@ -44,6 +44,7 @@ CO_FAST_LOCAL = 0x20
CO_FAST_CELL = 0x40
CO_FAST_FREE = 0x80
next_code_version = 1
def get_localsplus(code: types.CodeType):
a = collections.defaultdict(int)
@ -227,6 +228,7 @@ class Printer:
def generate_code(self, name: str, code: types.CodeType) -> str:
global next_code_version
# The ordering here matches PyCode_NewWithPosOnlyArgs()
# (but see below).
co_consts = self.generate(name + "_consts", code.co_consts)
@ -268,6 +270,8 @@ class Printer:
self.write(f".co_nplaincellvars = {nplaincellvars},")
self.write(f".co_ncellvars = {ncellvars},")
self.write(f".co_nfreevars = {nfreevars},")
self.write(f".co_version = {next_code_version},")
next_code_version += 1
self.write(f".co_localsplusnames = {co_localsplusnames},")
self.write(f".co_localspluskinds = {co_localspluskinds},")
self.write(f".co_filename = {co_filename},")
@ -461,6 +465,7 @@ def generate(args: list[str], output: TextIO) -> None:
with printer.block(f"if ({p} < 0)"):
printer.write("return -1;")
printer.write("return 0;")
printer.write(f"\nuint32_t _Py_next_func_version = {next_code_version};\n")
if verbose:
print(f"Cache hits: {printer.hits}, misses: {printer.misses}")