Re: Most efficient way to decode large C array of 32bit words?
[email protected] Thu, 21 Sep 2023 17:35:28 +1200
| Newsgroups | gmane.comp.lang.haskell.ffi |
|---|---|
| Message-ID | <[email protected]> |
--===============4319725307942157487==
Content-Type: multipart/alternative; boundary="=-8u7itYh/IPuGlZX0Q9iK"
--=-8u7itYh/IPuGlZX0Q9iK
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
<div style=3D"height: initial;" dir=3D"auto">I couldn't stop thinking a=
bout this, so keeping a close on benchmarks I found my solution:<div><br></=
div><div><div>clonePtr ptr l =3D do</div><div> ret <- mallo=
cForeignPtrArray l</div><div> withForeignPtr ret $ \ptr' ->=
copyArray ptr ptr' l</div><div> return ret</div><div>peekLazy=
fp 0 =3D []</div><div>peekLazy fp l =3D x:peekLazy (fp `plusForeignPtr` 1)=
(pred l)</div><div> where x =3D unsafePerformIO $ withForeignPtr fp =
peek</div><div>iterateLazy ptr l =3D do</div><div> fp <- cl=
onePtr 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 th=
e function call ends.</div></div></div>
=20
<div dir=3D"auto">On Thu, Jan 1, 1970 at 12:00 AM, [email protected] w=
rote:<br><blockquote type=3D"cite"><div style=3D"height: initial;" dir=3D"a=
uto">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 wh=
ich 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 performan=
ce problem identified by clear box benchmarking are a couple calls to `peek=
Array`. I've previously used `forM [0..length - 1] $ peekOffset arr`, but s=
witching away from that yielded only minor performance benefit. Commenting =
out this `peekArray` removes all the overhead I'm seeing.</div><div><br></d=
iv><div>(Strangely commenting out any usage of the returned list does likew=
ise, even though I've verified that the whole list doesn't get iterated ove=
r in WHNF)</div><div><br></div><div>The same benchmarks indicate that the p=
ostprocessing I'm applying over those lists has imperceptible overhead.</di=
v><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 w=
orth of output.</div></div>
=20
=20
</blockquote></div>
--=-8u7itYh/IPuGlZX0Q9iK--
--===============4319725307942157487==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline
X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KRkZJIG1haWxp
bmcgbGlzdApGRklAaGFza2VsbC5vcmcKaHR0cDovL21haWwuaGFza2VsbC5vcmcvY2dpLWJpbi9t
YWlsbWFuL2xpc3RpbmZvL2ZmaQo=
--===============4319725307942157487==--