
On Sun, Jul 02, 2017 at 03:52:34PM +1000, Chris Angelico wrote:
On Sun, Jul 2, 2017 at 3:41 PM, Steven D'Aprano <steve@pearwood.info> wrote:
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".
What you are describing sounds more like the output of a keyhole optimizer that folds constants, only extended to look inside functions. I expect that it would have to be a VERY clever optimizer, since it would have to do a complete whole-of-program static analysis to be sure that mysum has not been replaced, shadowed or redefined by the time it is called.
I won't say that is outright impossible, but it would be *extremely* hard to do something like that at compile time.
Isn't that the sort of thing that the "versioned globals dictionary" was supposed to do? If your globals haven't changed, you know that the optimizer was correct.
That only solves the problem of mysum being modified, not whether the arguments are ints. You still need to know whether it is safe to call some low-level (fast) integer addition routine, or whether you have to go through the (slow) high-level Python code. In any case, guards are a kind of runtime check. It might not be an explicit isinstance() check, but it is logically implies one. If x was an int, and nothing has changed, then x is still an int. If Victor is around, he might like to comment on how his FAT Python handles this.
But that's still a hard problem. Or at very least, it's decidedly non-trivial, and the costs are significant, so the net benefits aren't proven.
In fairness, they are proven for other languages, and they certainly worked for things like Psyco. So this isn't completely pie-in-the-sky dreaming. -- Steve