How to make my Haskell program faster? Comparison with C

后端 未结 4 1274
傲寒
傲寒 2021-01-31 16:44

I\'m working on an implementation of one of the SHA3 candidates, JH. I\'m at the point where the algorithm pass all KATs (Known Answer Tests) provided by NIST, and have also mad

4条回答
  •  囚心锁ツ
    2021-01-31 17:29

    • Switch to unboxed Vectors (from Array, used for constants)
    • Use unsafeIndex instead of incurring the bounds check and data dependency from safe indexing (i.e. !)
    • Unpack Block1024 as you did with Block512 (or at least use UnboxedTuples)
    • Use unsafeShift{R,L} so you don't incur the check on the shift value (coming in GHC 7.4)
    • Unfold the roundFunction so you have one rather ugly and verbose e8 function. This was significat in pureMD5 (the rolled version was prettier but massively slower than the unrolled version). You might be able to use TH to do this and keep the code smallish. If you do this then you'll have no need for constants as these values will be explicit in the code and result in a more cache friendly binary.
    • Unpack your Word128 values.
    • Define your own addition for Word128, don't lift Integer. See LargeWord for an example of how this can be done.
    • rem not mod
    • Compile with optimization (-O2) and try llvm (-fllvm)

    EDIT: And cabalize your git repo along with a benchmark so we can help you easier ;-). Good work on including a crypto-api instance.

提交回复
热议问题