Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
GH-98522: Add version number to code objects, to provide better version numbers for closures and comprehensions.#98525
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
d1e02c4004b13246d77fd9e668c9ba0b8f42ab0453File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add an internal version number to code objects, to give better versioning of | ||
| inner functions and comprehensions, and thus better specialization of those | ||
| functions. This change is invisible to both Python and C extensions. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -44,6 +44,7 @@ def make_string_literal(b: bytes) -> str: | ||
| 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 @@ def generate_unicode(self, name: str, s: str) -> str: | ||
| 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 @@ def generate_code(self, name: str, code: types.CodeType) -> str: | ||
| 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 | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| 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") | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| if verbose: | ||
| print(f"Cache hits:{printer.hits}, misses:{printer.misses}") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this is called func_version and not code_version? I find it a bit confusing (note that in deepfreeze.py we now have
_Py_next_func_version ={next_code_version}).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is primarily used as the version for functions.
https://github.com/python/cpython/blob/main/Python/bytecodes.c#L2976
Caching it in the code object is a further optimization.