[Python-Dev] Opcode cache in ceval loop

Sven R. Kunze srkunze at mail.de
Fri Feb 5 10:07:09 EST 2016


On 05.02.2016 00:06, Matthias Bussonnier wrote:
>> On Feb 4, 2016, at 08:22, Sven R. Kunze <srkunze at mail.de> wrote:
>>
>> On 04.02.2016 16:57, Matthias Bussonnier wrote:
>>>> On Feb 3, 2016, at 13:22, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
>>>>
>>>>
>>>> An ideal way would be to calculate a hit/miss ratio over time
>>>> for each cached opcode, but that would be an expensive
>>>> calculation.
>>> Do you mean like a sliding windows ?
>>> Otherwise if you just want a let's say 20% miss threshold, you increment by 1 on hit,
>>> and decrement by 4 on miss.
>> Division is expensive.
> I'm not speaking about division here.
> if you +M / -N the counter will decrease in average only if the hit/miss ratio
> is below N/(M+N), but you do not need to do the division.
>
> Then you deoptimize only if you get < 0.

I see but it looks still more complicated. :)

>
>>
>>> On Feb 3, 2016, at 13:37, Sven R. Kunze <srkunze at mail.de> wrote:
>>>
>>>> On 03.02.2016 22:22, Yury Selivanov wrote:
>>>>> One way of tackling this is to give each optimized opcode
>>>>> a counter for hit/misses.  When we have a "hit" we increment
>>>>> that counter, when it's a miss, we decrement it.
>>>> Within a given range, I suppose. Like:
>>>>
>>>> c = min(c+1, 100)
>>> Min might be overkill, maybe you can use a or mask, to limit the windows range
>>> to 256 consecutive call ?
>> Sure, that is how I would have written it in Python. But I would suggest an AND mask. ;-)
>
> Sure, implementation detail I would say. Should not write emails before breakfast...

;-)

> The other problem, with the mask, is if your increment hit 256 you wrap around back to 0
> where it deoptimize (which is not what you want), so you might need to not mask the
> sign bit and deoptimize only on a certain negative threshold.
>
>
> Does it make sens ?

Definitely. I am curious about the actual implementation of this idea.

Best,
Sven






More information about the Python-Dev mailing list