Guido van Rossum
๐ค SpeakerAppearances Over Time
Podcast Appearances
data type, which again, interprets the bit pattern as the address of a sequence of characters.
There are lots of lies in that story, but that's...
That's sort of a basic idea.
I can tell.
The optimization is the observation that in a particular line of code, so now you write your little Python program.
and you write a function, and that function sort of takes a bunch of inputs, and at some point it adds two of the inputs together.
Now, I bet you even if you call your function a thousand times that all those calls are likely all going to be about integers, because maybe your program is all about integers.
Or maybe on that particular line of code where there's that plus operator,
Every time the program hits that line, the variables A and B that are being added together happen to be strings.
And so what we do is instead of having this single byte code that says, here's an add operation and the implementation of add is fully generic.
It looks at the object from the object, it looks at the type, then it takes the type and it looks up the function pointer, then it calls the function.
Now the function has to look at the other argument and it has to double check that the other argument has the right type.
And then there's a bunch of error checking before it can actually just go ahead and add the two bit patterns in the right way.
What we do is every time we execute an add instruction like that, we keep a little note of...
In the end, after we hit the code that did the addition for a particular type, what type was it?
And then after a few times through that code, if it's the same type all the time,
we say, oh, so this add operation, even though it's the generic add operation, it might as well be the add integer operation.
And the add integer operation is much more efficient because it just says, assume that A and B are integers, do the addition operation, do it right there in line and produce the result.
And the big lie here is that in Python, even if you have great evidence that in the past it was always two integers that you were adding, at some point in the future, that same line of code could still be hit with two floating points or two strings or maybe a string and an integer.
It's not a great lie.