<div style="height: initial;" dir="auto">I couldn't stop thinking about this, so keeping a close on benchmarks I found my solution:<div><br></div><div><div>clonePtr ptr l = do</div><div> ret <- mallocForeignPtrArray l</div><div> withForeignPtr ret $ \ptr' -> copyArray ptr ptr' l</div><div> return ret</div><div>peekLazy fp 0 = []</div><div>peekLazy fp l = x:peekLazy (fp `plusForeignPtr` 1) (pred l)</div><div> where x = unsafePerformIO $ withForeignPtr fp peek</div><div>iterateLazy ptr l = do</div><div> fp <- clonePtr ptr l</div><div> return $ peekLazy fp $ fromEnum l<br><br>The clone's in there because I free the C library's datastructures as the function call ends.</div></div></div>
<div dir="auto">On Thu, Jan 1, 1970 at 12:00 AM, adrian@openwork.nz wrote:<br><blockquote type="cite"><div style="height: initial;" dir="auto">I've got an interesting puzzle I'm pulling my hair out over.<div><br></div><div>I'm writing (pure functional) Haskell bindings for a C library which returns 2 large arrays of 32bit words (or rather arrays of structs, all of who's fields are very conveniently aligned). But these bindings I wrote are far too slow for my uses.</div><div><br></div><div>A primary performance problem identified by clear box benchmarking are a couple calls to `peekArray`. I've previously used `forM [0..length - 1] $ peekOffset arr`, but switching away from that yielded only minor performance benefit. Commenting out this `peekArray` removes all the overhead I'm seeing.</div><div><br></div><div>(Strangely commenting out any usage of the returned list does likewise, even though I've verified that the whole list doesn't get iterated over in WHNF)</div><div><br></div><div>The same benchmarks indicate that the postprocessing I'm applying over those lists has imperceptible overhead.</div><div><br></div><div>In short: What's the fastest `peekArray` alternative for `Ptr Word32` you recommend?</div><div><br></div><div>P.S. I'm inputting a nearly 1mb novel (Dracula by Bram Stoker) to get an estimated 20mb * 2 worth of output.</div></div>
</blockquote></div>