Defining a custom newByteArray primop that uses calloc?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Defining a custom newByteArray primop that uses calloc?

Brandon Simmons
In my tests, using calloc from:

    https://hackage.haskell.org/package/missing-foreign-0.1.1/docs/Foreign-Marshal-MissingAlloc.html

was about twice as fast as allocating and zeroing the same amount of
memory with `newByteArray` + any of `copy/set/fillMutableByteArray`
(all three were nearly identical). Is there a way I can reasonably
define my own `newByteArray` that uses calloc?

FWIW here are a couple of the benchmarks I'm working with in criterion:

    arrTestSet :: Int -> IO ()
    arrTestSet len = do
        let eBytes = (P.sizeOf (undefined::Int))*len
        a <- P.newAlignedPinnedByteArray
                    eBytes
                    (P.alignment (undefined :: Int))
        void $ P.setByteArray a 0 len (1::Int)

    arrTestCallocAndWrite :: Int -> IO ()
    arrTestCallocAndWrite len = do
        ptr <- callocBytes (len*(P.sizeOf(undefined::Int))) :: IO (Ptr Int)
        pokeElemOff ptr 0 1
        free ptr

Thanks,
Brandon
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Defining a custom newByteArray primop that uses calloc?

Erik de Castro Lopo-34
Brandon Simmons wrote:

> In my tests, using calloc from:
>
>     https://hackage.haskell.org/package/missing-foreign-0.1.1/docs/Foreign-Marshal-MissingAlloc.html
>
> was about twice as fast as allocating and zeroing the same amount of
> memory with `newByteArray` + any of `copy/set/fillMutableByteArray`
> (all three were nearly identical). Is there a way I can reasonably
> define my own `newByteArray` that uses calloc?
>
> FWIW here are a couple of the benchmarks I'm working with in criterion:
>
>     arrTestSet :: Int -> IO ()
>     arrTestSet len = do
>         let eBytes = (P.sizeOf (undefined::Int))*len
>         a <- P.newAlignedPinnedByteArray

Why pinned memory?

Erik
--
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Reply | Threaded
Open this post in threaded view
|

Re: Defining a custom newByteArray primop that uses calloc?

Carter Schonwald
you cant provide > 1 word alignment guarantees if the bytearray isnt pinned, ... at least with the current RTS setup. (though that would be pretty handy if possible)

On Wed, Nov 26, 2014 at 8:55 PM, Erik de Castro Lopo <[hidden email]> wrote:
Brandon Simmons wrote:

> In my tests, using calloc from:
>
>     https://hackage.haskell.org/package/missing-foreign-0.1.1/docs/Foreign-Marshal-MissingAlloc.html
>
> was about twice as fast as allocating and zeroing the same amount of
> memory with `newByteArray` + any of `copy/set/fillMutableByteArray`
> (all three were nearly identical). Is there a way I can reasonably
> define my own `newByteArray` that uses calloc?
>
> FWIW here are a couple of the benchmarks I'm working with in criterion:
>
>     arrTestSet :: Int -> IO ()
>     arrTestSet len = do
>         let eBytes = (P.sizeOf (undefined::Int))*len
>         a <- P.newAlignedPinnedByteArray

Why pinned memory?

Erik
--
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/
_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


_______________________________________________
Glasgow-haskell-users mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users