faster-cpython / ideas

1.67k stars 49 forks source link

Better optimization of side exits by recording type information #680

Open markshannon opened 1 month ago

markshannon commented 1 month ago

When optimizing a side exit, it would be useful to know what types and values are guaranteed. Many side traces start with guards that are unnecessary as the type of values is guaranteed by the previous executor. However, we do not currently use that information when optimizing side guards. If we were to record that information on exits, then we could do a better job optimizing exits.

For example, given the following code snippet:

x = a + b # ints
if (cond): # Not taken
    return x + a + b

If there is a side exit at the branch if(cond), then we should know that x, a and b are ints, but we throw that information away, resulting in extra guards in the final trace for return x + a + b

One record per executor

Instead of retaining type information on side exits, we can keep the type information at the entry to the trace and recompute the type information before optimizing the new side trace. (Credit @Fidget-Spinner)

Although this would slow down the optimizer a bit, it would save quite a lot of memory.

brandtbucher commented 1 month ago

We need to be careful not to insert side-exit executors into the tier one bytecode when doing this, right? I forget if we insert them currently...