haskell-tls / hs-certificate

Certificate and Key Reader/Writer in haskell
60 stars 57 forks source link

Memory improvements #73

Closed adinapoli closed 8 years ago

adinapoli commented 8 years ago

Hey @vincenthz ,

see this and that for context.

I have obtained a small improvement it memory efficiency and time by using a deque instead of a plain list in onContainer. The main insight is that:

case reverse l of
    ((End _, _) : l2) -> f $ reverse l2
    _

Is basically equivalent to this:

case Deq.uncons s of
  Just ((Start _, _), l) -> case Deq.unsnoc l of
                                         Just ((End _, _), l2) -> f (F.toList l2)
                                         _ -> f []
  _

Avoiding the first reverse is possible just by taking the last element of the deque with snoc, and this way we do not have to reverse the list back a second time as l2 is already in the correct order, as we have simply removed things from the head or the tail of the deque.

What we have got here is certainly curing the symptoms but not the illness; we would probably need a better algorithm if we want to get any major performance boost.

adinapoli commented 8 years ago

@vincenthz I have actually realised the majority of the memory improvements are going to come from the patches for hs-pem and hs-asn1. Using deque here doesn't add any substantial improvement (on the contrary, it seems to degrade performance, after I reran the profiling) and at the same time it won't build on older GHC, so I'm closing this!