[Python-ideas] Bytecode JIT
Chris Angelico
rosuav at gmail.com
Sat Jul 1 19:32:39 EDT 2017
On Sun, Jul 2, 2017 at 8:52 AM, Soni L. <fakedme+py at gmail.com> wrote:
> On 2017-07-01 07:34 PM, Victor Stinner wrote:
>>
>> Let's say that you have a function "def mysum (x; y): return x+y", do you
>> always want to use your new IADD instruction here? What if I call mysum
>> ("a", "b")?
>>
>> Victor
>
>
> Let's say that you do. Given how short it is, it would just get inlined.
> Your call of mysum ("a", "b") would indeed not use IADD, nor would it be a
> call. It would potentially not invoke any operators, but instead get
> replaced with "ab".
>
> When you have a tracing JIT, you can do away with a lot of overhead. You can
> inline functions, variables, do away with typechecks, and many other things.
> This holds true even if that JIT never emits a single byte of machine code.
Let's try a more complicated example.
# demo.py
def mysum(x, y):
return x + y
def do_stuff(a, b):
print(mysum("foo", "bar"))
print(mysum(5, 7))
print(mysum(a, 42))
print(mysum(b, "spam"))
What can you optimize here? Now let's look at a file that might call it:
# cruel.py
import demo
def nasty(x, y):
demo.mysum = random.choice([
lambda x, y: x + y,
lambda x, y: f"{x} + f{y}",
lambda x, y: "muahahaha",
])
return Ellipsis
demo.mysum = nasty
demo.do_stuff("what", "now?")
Unless you can prove that this doesn't happen, you can't really
optimize much of mysum away. That's where a tracing JIT compiler has
the advantage: it can notice *at run time* that you're not doing this
kind of thing, and in effect, forfeit the optimizations when you're
running your tests (since test suites are where this kind of
monkey-patching tends to happen).
ChrisA
More information about the Python-ideas
mailing list