Skip to content

Commit 8a5c0d8

Browse files
dcouttstolysz
authored andcommitted
Add an experimental flag for safe ffi calls
We suspect that long running zlib calls may be starving other threads in some applications. So we cana try those apps with this flag and if that is indeed the problem then we can try doing this automatically where there's minimal perf impact (e.g. larger calls).
1 parent 343eb93 commit 8a5c0d8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Codec/Compression/Zlib/Stream.hsc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,12 @@ newtype StreamState = StreamState (Ptr StreamState)
10101010
-- So we define c_inflateInit2 and c_deflateInit2 here as wrappers around
10111011
-- their _ counterparts and pass the extra args.
10121012

1013+
##ifdef NON_BLOCKING_FFI
1014+
##define SAFTY safe
1015+
##else
1016+
##define SAFTY unsafe
1017+
##endif
1018+
10131019
foreign import ccall unsafe "zlib.h inflateInit2_"
10141020
c_inflateInit2_ :: StreamState -> CInt -> Ptr CChar -> CInt -> IO CInt
10151021

@@ -1018,7 +1024,7 @@ c_inflateInit2 z n =
10181024
withCAString #{const_str ZLIB_VERSION} $ \versionStr ->
10191025
c_inflateInit2_ z n versionStr (#{const sizeof(z_stream)} :: CInt)
10201026

1021-
foreign import ccall unsafe "zlib.h inflate"
1027+
foreign import ccall SAFTY "zlib.h inflate"
10221028
c_inflate :: StreamState -> CInt -> IO CInt
10231029

10241030
foreign import ccall unsafe "zlib.h &inflateEnd"
@@ -1051,7 +1057,7 @@ foreign import ccall unsafe "zlib.h inflateSetDictionary"
10511057
-> CUInt
10521058
-> IO CInt
10531059

1054-
foreign import ccall unsafe "zlib.h deflate"
1060+
foreign import ccall SAFTY "zlib.h deflate"
10551061
c_deflate :: StreamState -> CInt -> IO CInt
10561062

10571063
foreign import ccall unsafe "zlib.h &deflateEnd"

zlib.cabal

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ source-repository head
3636
type: darcs
3737
location: http://code.haskell.org/zlib/
3838

39+
flag non-blocking-ffi
40+
default: False
41+
description: The (de)compression calls can sometimes take a long time, which
42+
prevents other Haskell threads running. Enabling this flag
43+
avoids this unfairness, but with greater overall cost.
44+
3945
library
4046
exposed-modules: Codec.Compression.GZip,
4147
Codec.Compression.Zlib,
@@ -57,6 +63,8 @@ library
5763
build-depends: ghc-prim
5864
includes: zlib.h
5965
ghc-options: -Wall -fwarn-tabs
66+
if flag(non-blocking-ffi)
67+
cpp-options: -DNON_BLOCKING_FFI
6068
if !os(windows)
6169
-- Normally we use the the standard system zlib:
6270
extra-libraries: z

0 commit comments

Comments
 (0)