-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove remaining uses of FFI under -fpure-haskell (#660)
All of these were standard C functions that GHC's JS backend actually somewhat supports; their shims can be found in the compiler source at "rts/js/mem.js". But it seems simpler to just get rid of all FFI uses with -fpure-haskell rather than try to keep track of which functions GHC supports. The pure Haskell implementation of memcmp runs about 6-7x as fast as the simple one-byte-at-a-time implementation for long equal buffers, which makes it... about the same speed as the pre-existing shim, even though the latter is also a one-byte- at-a-time implementation! Apparently GHC's JS backend is not yet able to produce efficient code for tight loops like these yet; the biggest problem is that it does not perform any loopification so each iteration must go through a generic-call indirection. Unfortunately that means that this patch probably makes 'strlen' and 'memchr' much slower with the JS backend. (cherry picked from commit 305604c)
- Loading branch information
Showing
11 changed files
with
134 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ cabal.sandbox.config | |
dist-newstyle/ | ||
cabal.project.local* | ||
.nvimrc | ||
.ghc.environment* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
-- | | ||
-- Module : Data.ByteString.Utils.UnalignedAccess | ||
-- Copyright : (c) Matthew Craven 2023-2024 | ||
-- License : BSD-style | ||
-- Maintainer : [email protected] | ||
-- Stability : internal | ||
-- Portability : non-portable | ||
-- | ||
-- Primitives for reading and writing at potentially-unaligned memory locations | ||
|
||
{-# LANGUAGE CPP #-} | ||
|
||
{-# LANGUAGE MagicHash #-} | ||
{-# LANGUAGE UnboxedTuples #-} | ||
|
||
#include "bytestring-cpp-macros.h" | ||
|
||
module Data.ByteString.Utils.UnalignedWrite | ||
module Data.ByteString.Utils.UnalignedAccess | ||
( unalignedWriteU16 | ||
, unalignedWriteU32 | ||
, unalignedWriteU64 | ||
, unalignedWriteFloat | ||
, unalignedWriteDouble | ||
, unalignedReadU64 | ||
) where | ||
|
||
import Foreign.Ptr | ||
|
@@ -42,6 +53,10 @@ unalignedWriteDouble :: Double -> Ptr Word8 -> IO () | |
unalignedWriteDouble = coerce $ \(D# x#) (Ptr p#) s | ||
-> (# writeWord8OffAddrAsDouble# p# 0# x# s, () #) | ||
|
||
unalignedReadU64 :: Ptr Word8 -> IO Word64 | ||
unalignedReadU64 = coerce $ \(Ptr p#) s | ||
-> case readWord8OffAddrAsWord64# p# 0# s of | ||
(# s', w64# #) -> (# s', W64# w64# #) | ||
|
||
#elif HS_UNALIGNED_POKES_OK | ||
import Foreign.Storable | ||
|
@@ -61,6 +76,8 @@ unalignedWriteFloat x p = poke (castPtr p) x | |
unalignedWriteDouble :: Double -> Ptr Word8 -> IO () | ||
unalignedWriteDouble x p = poke (castPtr p) x | ||
|
||
unalignedReadU64 :: Ptr Word8 -> IO Word64 | ||
unalignedReadU64 p = peek (castPtr p) | ||
|
||
#else | ||
foreign import ccall unsafe "static fpstring.h fps_unaligned_write_u16" | ||
|
@@ -73,5 +90,7 @@ foreign import ccall unsafe "static fpstring.h fps_unaligned_write_HsFloat" | |
unalignedWriteFloat :: Float -> Ptr Word8 -> IO () | ||
foreign import ccall unsafe "static fpstring.h fps_unaligned_write_HsDouble" | ||
unalignedWriteDouble :: Double -> Ptr Word8 -> IO () | ||
foreign import ccall unsafe "static fpstring.h fps_unaligned_read_u64" | ||
unalignedReadU64 :: Ptr Word8 -> IO Word64 | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters