How does the dynamic nature of Python interoperate with PyPy's ability to inline functions?

Viewed 76

Suppose you have a function like f that calls a function m.g:

def f(x):
    return m.g(x, 2*x, x+1)

and f gets called a lot, so PyPy JITs it and inlines m.g into it. What if later, due to the "dynamic" nature of Python, m.g gets replaced by something else: Will the old JITed version of f be discarded right away, or could it still be called accidentally?

Also, what if your program does these redefinitions a lot, can the discarded JITed versions cause a memory leak?

2 Answers

To answer your last question: "if your program does these redefinitions a lot, can the discarded JITed versions cause a memory leak?" This is a good question, and the answer might be yes in some cases. The bad case might be if g is a function you just created with exec or eval, so that there is an unbounded number of function objects that end up being called here. This is a problem that we thought about fixing in the past, but never got around to do it. If you are experiencing what looks like a leak and have isolated this part of the code, then I'd say chances are that it is exactly what you are fearing. In that case, I'd recommend to write a mail to pypy-dev@python.org or come to #pypy on irc.freenode.net to describe your case.

You should have nothing to worry about. If what you're talking about, the wrong code being executed, were to happen, that would be a bug in the PyPy environment. That's very unlikely. Also, don't worry about a memory leak. Even if there was one, it wouldn't amount to enough memory that you'd ever notice or care. The only way you could possibly be affected by some sort of memory bug like that would be if you change that definition 1000s of times per execution of your code. I doubt that's the case.

“Premature optimization is the root of all evil”. Maybe you've heard of this famous quote. I think it also applies to dealing with problems that might occur in the future, but that are unlikely. Don't worry about any of this unless you see bad behavior. I very much doubt that you will. Trust in your tools!

Related