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
Simplifying and enhancing const_key #54
Simplifying and enhancing const_key #54
Conversation
Codecov Report
@@ Coverage Diff @@
## master #54 +/- ##
==========================================
+ Coverage 93.18% 94.27% +1.09%
==========================================
Files 17 17
Lines 2830 2847 +17
==========================================
+ Hits 2637 2684 +47
+ Misses 193 163 -30
Continue to review full report at Codecov.
|
fix #51 |
Could you please add tests covering #51 to ensure the new behavior fixes it ? And please update the changelog too. |
@MatthieuDartiailh Done. Sorry for the late PR. |
Thanks ! I will do my best to release over the week-end. |
@MatthieuDartiailh Thank you, and take your time! |
Explanation
using
marshal.dumps
forconst_key
In CPython, code objects compiled from a source code file, are able to get serialized into binary contents via
marshal.dumps
.This gives us some useful observations:
Constants(all elements in
code.co_consts
) of every code object, are also able to get serialized into binary contents viamarshal.dumps
.marshal.dumps
must be able to distinguish data of different types and values, even for something we might make mistakes on, e.g., #32 (comment), and keep the property of data with structural equality. This is because,marshal.dumps
is a foundation of CPython: CPython relies on this to keep the same behaviour of.py
and.pyc
.Hence,
marshal.dumps
can precisely give us a perfect encoding that can distinguish objects from each other and keep the equality of structure data, for all constants supported by Python. On the other side, the encoding itself is a simplebytes
, thus, if we just want to keep thesame behaviour as CPython's,
marshal.dumps
can sufficiently become an implementation of the functionconst_key
mentioned in #51 .This already keeps the total compatibility to CPython.
More general constants in bytecode, and compatibility
However, for this good reason given by @SnoopyLane , this will be very beneficial:
Hence we can also support non-
marshal
-able constants in code objects.This time, my proposal for this extension is,
type(const), id(const)
is also the same strategy used before: https://github.com/vstinner/bytecode/blob/45d643f6a706ae95586a874ea0fbcb18e797bfa1/bytecode/instr.py#L37 .Advantage of this implementation
No need any more to synchronize our code for
const_key
with_PyCode_ConstantKey
, but keep full compatibility to CPython.A little more efficient when we're calculating keys for
tuple
or other built-in structural data.Code size for
const_key
gets reduced, also, imports of modules likemath
andtypes
can get eliminated.