Commits

Carter Schonwald committed 2715f7e

FIXED THE BUGS. also added some notes

  • Participants
  • Parent commits 2bea2d5

Comments (0)

Files changed (5)

 
 import GHC.Word
 import Criterion.Main
-import Data.Foldable 
+import Data.Foldable hiding (mapM_)
 import Criterion.Config
 import Data.Word (Word64)
 import Foreign.ForeignPtr (withForeignPtr)
 
 {-  Word64 = W64# Word# , who knew? #-}
 
-whnfIter:: Int ->(a->a)-> a -> Pure 
-whnfIter !cnt !f  !arg = whnf (\v -> foldl' (\ a b -> f a ) v [0 .. cnt]  ) arg
+
 
 {-# NOINLINE hsDoubleWord64 #-}
 hsDoubleWord64 :: Word64 -> Word64
 
 
 foreign import prim "c4hs_doubleWord" c4hs_doubleWord :: Word# ->Word#
-main =  defaultMainWith defaultConfig{cfgSamples=ljust 30} (return ()) [
+main =  defaultMainWith defaultConfig{cfgSamples=ljust 100} (return ()) [
         bgroup "double a  64bit word" [
-            bcompare [ bench "wrapped_cmmDoubleWord 1000" $! whnfIter 1000 wrapped_cmmDoubleWord 7 ,
-                        bench "hs double word  1000" $! whnfIter 1000 hsDoubleWord64 7 ,
-                        bench "hs double word INLINED 1000" $! whnfIter 1000 hsDoubleWord64Inlined 7 ,
-                        bench "hs double int  C4hs  1000" $!  whnfIter 1000  wrapped_C4HS_doubleword 7,
-                        bench " double int  C UNSAFE  1000" $!  whnfIter 1000  wrapper_c_doubleNum_UNSAFE 7,
-                        bench " double int  C SAFE 1000" $!  whnfIter 1000  wrapper_c_doubleNum_SAFE 7
+            bcompare [ bench "wrapped_cmmDoubleWord 1000" $! whnf wrapped_cmmDoubleWord 7 ,
+                        bench "hs double word  1000" $! whnf hsDoubleWord64 7 ,
+                        bench "hs double word INLINED 1000" $! whnf hsDoubleWord64Inlined 7 ,
+                        bench "hs double int  C4hs  1000" $!  whnf  wrapped_C4HS_doubleword 7,
+                        bench " double int  C UNSAFE  1000" $!  whnf  wrapper_c_doubleNum_UNSAFE 7,
+                        bench " double int  C SAFE 1000" $!  whnf  wrapper_c_doubleNum_SAFE 7
                         ]],
         bgroup "factorial   64bit word" [
-            bcompare [ bench "wrapped_cmm_factorialWord " $! whnf wrapped_cmm_factorialWord 10,
+            bcompare [  bench "wrapped_cmm_factorialWord " $! whnf wrapped_cmm_factorialWord 10,
+                       
                         bench "hs factorial word  " $! whnf hsFactorial 10,
                         bench "hs factorial  word INLINED " $! whnf hsFactorialInlined 10,
-                        bench "factorial   C4hs  1000" $!  whnf wrapped_C4HS_factorial 10 
+                        bench "factorial   C4hs  " $!  whnf wrapped_C4HS_factorial 10 ,
+                        bench "factorial unsafe C FFI"  $! whnf wrapper_c_factorial_UNSAFE 10 
                             ]   ] 
 
 

FunShim/readme.md

 
 the waf.py and wscript (also python) are courtesy of the alphaheavy 
 example found  at https://github.com/alphaHeavy/kospi
-I would have liked to change the build script to using Shake, but didn't find the time.
+I would have liked to change the build script to using Shake, but didn't find the time.
+patches welcome for that change to remove the python code and replace it with shake :) 

beamer-who-ya-gonna-call.pdf

Binary file modified.
 
 the /FunShim directory has all the demo code and associated (very hacky)
 build scripting are partially written using https://github.com/alphaHeavy/kospi
-examples, plus reading the primops.cmm file in ghc 7.6.3  https://github.com/ghc/ghc/blob/ghc-7.6/rts/PrimOps.cmm
+examples, plus reading the primops.cmm file in ghc 7.6.3  https://github.com/ghc/ghc/blob/ghc-7.6/rts/PrimOps.cmm
+
+
+
+All code in the Funshim directory is property of the respective authors, that said,
+do whatever you want with it, and if licensing matters, talk with me and I'll sort out 
+getting the relevant code permissively licensed.
+
+ 

who-ya-gonna-call.md

 How does CMM compare with Haskell code, C using the FFI's,
 and C modified to use the GHC calling convention
 
+
+# Conclusions from measurements (added after talk) 
+
+* Inlined Haskell can beat  CMM or C.
+
+* use unsafe C ffi for sub-millisecond codes, the calling overhead is less than 5ns!
+    
+    - for longer work, unsafe is unsafe! The call blocks that ghc thread from doing anything else till it returns.
+
+* use safe C ffi for any long running ffi operations, runs in a seperate thread meant for such work.
+
+* CMM or the LLVM modified C that uses the GHC calling convention is only suitable when you 
+need to either hook into the GHC Run Time System, or do multiple return values from the foreign call.
+
+    - Example of the first use case might be writing a concurrency primitive for atomic writes to an array
+
+    - Example of the latter might be returning multiple fields from a network packet as an unboxed tuple.
+
 # References and notes, added after the talk
 
 * Safe FFI C Calls from Haskell are run in their own thread, I incorrectly state