RULES in binary

Simon Peyton Jones simonpj at
Mon Jul 27 08:02:32 UTC 2015


If a RULE and an inlining “do the same thing”, the RULE is usually to be preferred because it duplicates less code.


From: Lennart Kolmodin [mailto:kolmodin at]
Sent: 26 July 2015 21:50
To: Simon Peyton Jones
Cc: ghc-devs at
Subject: Re: RULES in binary

Yes, this has been on my todo for a long time :)
Essentially all inlinings/rules in binary should be gone through and confirmed whether they're still needed.
I had a look now to get some insight.

Since a few versions GHC warns in this way when something might not go the way it was intended, a great way to learn more about how inlining and rules work and to avoid surprises.
All these warnings are proof of my poor understanding when I implemented it. Naturally it should all be fixed.

Here's how I reasoned when implementing it;
In Data.Binary.Get we have functions we always want to inline, even if GHC doesn't think it's a good idea. Therefore there are both INLINE pragmas as well as RULES to achieve this. GHC now warns that the function might get inlined before the rule triggers, which is ok since they do the same thing.
We should probably re-evaluate whether always inlining still is a good idea. If it is, we can keep the RULES to inline, and change the INLINE to NOINLINE and let the RULES do their job.

In Data.Binary.Internal.Get we attempt a trick where applicative code can become more efficient. It tries to rewrite the components of an expression "f <*> g <*> h" into something that does f, g and h with a single bounds check (the check for "do we have enough input bytes to continue?").
This trick relies so much on that the user's code has been inlined properly that it probably very rarely fires in a real application. It does wonders in the unrealistic micro benchmark, though :)
Probably those rules can be removed without any real code suffering. I'd like to add some more real world benchmarks, and finally test with the changes proposed above.


2015-07-24 13:44 GMT+02:00 Simon Peyton Jones <simonpj at<mailto:simonpj at>>:
In the binary library I’m seeing lots of these warnings:

libraries/binary/src/Data/Binary/Get.hs:420:1: warning:

    Rule "getWord16le/readN" may never fire

      because ‘getWord16le’ might inline first

    Probable fix: add an INLINE[n] or NOINLINE[n] pragma on this function

libraries/binary/src/Data/Binary/Builder/Base.hs:510:1: warning:

    Rule "flush/flush" may never fire

      because ‘flush’ might inline first

    Probable fix: add an INLINE[n] or NOINLINE[n] pragma on this function
The warnings look right to me: currently everything is very fragile and may not work as you intend.
You may want to look into this?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the ghc-devs mailing list