Remix.run Logo
somat 4 days ago

If the compiler is examining the function to know what to optimize. why does it not know what that function does so it can't optimize? That is, I assume you are correct but you lost me in the logic. is it eval?

Animats 4 days ago | parent | next [-]

Because, in Python, you can monkey-patch anything from anywhere. It's not done much, but the capability is there.

ufo 4 days ago | parent | prev | next [-]

The issue in dynamic languages like python is that almost every little thing is impossible to be sure at compile time:

* The type of arguments passed to the function. Everything might point to the function always being passed values of a particular type, but you still can't be sure.

* If you call another function, you can't be sure that you'll actually call the function you expect, because any function or global variable may be overwritten at runtime.

* There are many places in the language that might call an invisible function call (method overloads, garbage collection methods, etc) and any of those invisible calls could potentially monkey patch something

All of these complexities, and more, are why JIT compilers like PyPy exist. Instead of optimizing at compile time, they observe things at runtime, optimize optimistically assuming that the program won't try anything funny, but stays ready to roll back the optimizations in case the program does try something funny.

yongjik 4 days ago | parent | prev | next [-]

For example, you can create an instance x of class Foo, change its one method x.bar to do something completely different at runtime, and any existing code that calls Foo.bar will happily execute your version. Which means Python cannot optimize any function that calls Foo.bar even if it "knows" what Foo.bar does.

Jensson 4 days ago | parent | prev [-]

> why does it not know what that function does so it can't optimize?

Because that seem to require the compiler to be an AGI. Its very hard to explain why its hard to make a truly intelligent program, but you just have to trust that tons of people have tried for entire careers to solve this and so far none have solved it.