Closed markshannon closed 6 months ago
Assigning myself, since I already have a branch that does this in the compiler (no speedup, though, which is why it was put on pause).
See https://github.com/python/cpython/compare/main...brandtbucher:cpython:load-const-immortal. It currently handles None
, True
, False
and the small integers.
I'll try benchmarking it tonight, and if it's no faster I can also add support for the empty tuple and 0/1-character ASCII str
/bytes
as well. I also haven't yet looked into whether RETURN_CONST
should become RETURN_CONST_IMMORTAL
(I suspect it should, since that was the case for the LOAD_CONST
superinstructions).
Note that LOAD_CONST_IMMORTAL
is different from the LOAD_CONST_COMMON
that you were experimenting with.
LOAD_CONST_IMMORTAL
can handle any immortal object, but still requires the constants to be added to the code object, so is less space efficient.
It seems unlikely that we will want both forms.
LOAD_CONST_IMMORTAL
is a lot simpler to implement, but I suspect that LOAD_CONST_COMMON
would be better once we have the infrastructure to find and enumerate the most commonly used constants.
We should remove RETURN_CONST
https://github.com/faster-cpython/ideas/issues/577
Since many constants are strings that can be immortalized when interning, we don't necessarily know what will be immortal at compile time, but we will know when optimizing tier 2.
Since we already convert LOAD_CONST
to inline versions in tier 2, I'm closing this.
Constants are, um, constant. So if the argument of
LOAD_CONST
is immortal is will always be immortal.We could add a specialization of
LOAD_CONST
,LOAD_CONST_IMMORTAL
, which doesn't do any refcount modification.The neatest way to do this would be in the compiler, but it can be done at runtime:
A
LOAD_CONST_MORTAL
specialization is not possible, as PEP 683 allows mortal object to become immortal.