binary-parsers icon indicating copy to clipboard operation
binary-parsers copied to clipboard

Implement `scan` without low-level functions (pointer arithmetic)?

Open nh2 opened this issue 1 year ago • 0 comments

Hi,

I am reading your library to find some ways to make the JPEG parser of JuicyPixels faster.

I noticed the implementation of your scan function:

https://github.com/winterland1989/binary-parsers/blob/11124a955f1ded9057633a124a965a4abd12e93e/Data/Binary/Parser/Word8.hs#L228-L255

and am wondering if there's a reason it needs to use withForeignPtr. Since the given function is s -> Word8 -> Maybe s, scan should only need to read one Word8 out of the chunk after another, which could be accomplished just using unsafeIndex.

So why is the function implemented in such a low-level fashion? Is it that repeated uses of unsafeIndex would result in repeated uses of that one's use withForeignPtr, and withForeignPtr allocates or is otherwise slow somehow?

Thanks for your insight!

nh2 avatar Nov 30 '22 22:11 nh2