Skip to content

Commit 1672e5c

Browse files
committed
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 b53e777 commit 1672e5c

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
@@ -1009,6 +1009,12 @@ newtype StreamState = StreamState (Ptr StreamState)
10091009
-- So we define c_inflateInit2 and c_deflateInit2 here as wrappers around
10101010
-- their _ counterparts and pass the extra args.
10111011

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

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

1020-
foreign import ccall unsafe "zlib.h inflate"
1026+
foreign import ccall SAFTY "zlib.h inflate"
10211027
c_inflate :: StreamState -> CInt -> IO CInt
10221028

10231029
foreign import ccall unsafe "zlib.h &inflateEnd"
@@ -1050,7 +1056,7 @@ foreign import ccall unsafe "zlib.h inflateSetDictionary"
10501056
-> CUInt
10511057
-> IO CInt
10521058

1053-
foreign import ccall unsafe "zlib.h deflate"
1059+
foreign import ccall SAFTY "zlib.h deflate"
10541060
c_deflate :: StreamState -> CInt -> IO CInt
10551061

10561062
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)