@@ -42,6 +42,7 @@ import Control.Monad.Primitive (PrimMonad(..))
4242import Control.Monad.R.Class
4343import Control.Monad.ST.Unsafe (unsafeSTToIO )
4444import qualified Data.Semigroup as Sem
45+ import Data.Maybe (isJust )
4546import Data.Monoid
4647import Data.Default.Class (Default (.. ))
4748import qualified Foreign.R as R
@@ -232,35 +233,42 @@ initLock = unsafePerformIO $ newMVar ()
232233-- initialize multiple times concurrently, but there is nothing stopping the
233234-- compiler from doing so when compiling quasiquotes.
234235
235- -- | Create a new embedded instance of the R interpreter. Only works from the
236- -- main thread of the program. That is, from the same thread of execution that
237- -- the program's @main@ function is running on. In GHCi, use @-fno-ghci-sandbox@
238- -- to achieve this.
239- initialize :: Config -> IO ()
240- initialize Config {.. } = do
241- #ifndef mingw32_HOST_OS
242- #if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
243- -- NOTE: OS X and FreeBSD does not allow removing the stack size limit completely,
244- -- instead forcing a hard limit of just under 64MB.
245- let stackLimit = ResourceLimit 67104768
246- #else
247- let stackLimit = ResourceLimitUnknown
236+ #if !defined(mingw32_HOST_OS) & !defined(darwin_HOST_OS)
237+ explainStackIncrease :: [String ] -> String
238+ explainStackIncrease cmds = unlines $
239+ [ " Language.R.Interpreter: cannot increase stack size limit."
240+ , " Try increasing your stack size limit manually:"
241+ ] ++ cmds
248242#endif
249- setResourceLimit ResourceStackSize (ResourceLimits stackLimit stackLimit)
250- `onException` (hPutStrLn stderr $
251- " Language.R.Interpreter: "
252- ++ " Cannot increase stack size limit."
253- ++ " Try increasing your stack size limit manually:"
243+
244+ setLimit :: IO ()
254245#ifdef darwin_HOST_OS
255- ++ " $ launchctl limit stack 67104768"
256- ++ " $ ulimit -s 65532"
246+ setLimit = return ()
247+ #elif defined(mingw32_HOST_OS)
248+ setLimit = return ()
257249#elif defined(freebsd_HOST_OS)
258- ++ " $ ulimit -s 67104768"
250+ setLimit = do
251+ let stackLimit = ResourceLimit 67104768
252+ setResourceLimit ResourceStackSize (ResourceLimits stackLimit stackLimit)
253+ `onException` hPutStrLn stderr (explainStackIncrease [" $ ulimit -s 67104768" ])
259254#else
260- ++ " $ ulimit -s unlimited"
261- #endif
262- )
255+ setLimit = do
256+ let stackLimit = ResourceLimitUnknown
257+ setResourceLimit ResourceStackSize (ResourceLimits stackLimit stackLimit)
258+ `onException` hPutStrLn stderr (explainStackIncrease [" $ ulimit -s unlimited" ])
263259#endif
260+
261+
262+ -- | Create a new embedded instance of the R interpreter. Only works from the
263+ -- main thread of the program. That is, from the same thread of execution that
264+ -- the program's @main@ function is running on. In GHCi, use @-fno-ghci-sandbox@
265+ -- to achieve this.
266+ --
267+ -- Set `H_DISABLE_INCREASE_STACK_SIZE` to disable changing rlimit.
268+ initialize :: Config -> IO ()
269+ initialize Config {.. } = do
270+ shouldSetLimit <- isJust <$> lookupEnv " H_DISABLE_INCREASE_STACK_SIZE"
271+ unless shouldSetLimit setLimit
264272 initialized <- fmap (== 1 ) $ peek isRInitializedPtr
265273 -- See note [Concurrent initialization]
266274 unless initialized $ withMVar initLock $ const $ do
0 commit comments