Skip to content

CoreToStg.Prep: Speculative evaluation

Sebastian Graf requested to merge wip/T19224 into master

From Note [Speculative evaluation]:

Since call-by-value is much cheaper than call-by-need, we case-bind arguments that are either

  1. Strictly evaluated anyway, according to the StrictSig of the callee, or
  2. ok-for-spec, according to 'exprOkForSpeculation'

While (1) is a no-brainer and always beneficial, (2) is a bit more subtle, as the careful haddock for 'exprOkForSpeculation' points out. Still, by case-binding the argument we don't need to allocate a thunk for it, whose closure must be retained as long as the callee might evaluate it. And if it is evaluated on most code paths anyway, we get to turn the unknown eval in the callee into a known call at the call site.

NoFib Results:

--------------------------------------------------------------------------------
        Program         Allocs    Instrs
--------------------------------------------------------------------------------
           ansi          -9.4%    -10.4%
       maillist          -0.1%     -0.1%
      paraffins          -0.7%     -0.5%
            scc          -0.0%     +0.1%
       treejoin          -0.0%     -0.1%
--------------------------------------------------------------------------------
            Min          -9.4%    -10.4%
            Max           0.0%     +0.1%
 Geometric Mean          -0.1%     -0.1%

And binary sizes go down by 0.2%. TODO: Investigate what's happening in ansi.

Fixes #19224 (closed).

Edited by Sebastian Graf

Merge request reports