-
Notifications
You must be signed in to change notification settings - Fork 21
stack safe #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
stack safe #12
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/.* | ||
!/.gitignore | ||
!/.travis.yml | ||
/bower_components/ | ||
/output/ |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "purescript-eff-aff-bench", | ||
"dependencies": { | ||
"purescript-minibench": "^2.0.0", | ||
"purescript-effect": "safareli/purescript-effect#fast", | ||
"purescript-aff": "^5.0.0" | ||
}, | ||
"resolutions": { | ||
"purescript-effect": "fast" | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"clean": "rimraf output && rimraf .pulp-cache", | ||
"start": "npm run build && npm run run", | ||
"run": "node --expose-gc -e 'require(\"./output/Bench.Main/index.js\").main()'", | ||
"build": "eslint src && pulp build -- --censor-lib --strict" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^4.19.1", | ||
"pulp": "^12.2.0", | ||
"purescript-psa": "^0.6.0", | ||
"rimraf": "^2.6.2" | ||
}, | ||
"dependencies": { | ||
"bower": "^1.8.8", | ||
"purescript": "^0.12.5" | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
"use strict"; | ||
|
||
exports.mkArr = function(){ | ||
return { count: 0 }; | ||
}; | ||
|
||
exports.pushToArr = function(xs) { | ||
return function() { | ||
return function() { | ||
xs.count += 1; | ||
return xs; | ||
}; | ||
}; | ||
}; | ||
|
||
exports.log = function(x) { | ||
return function(){ | ||
// eslint-disable-next-line | ||
console.log(x); | ||
}; | ||
}; |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
module Bench.Main where | ||
|
||
import Prelude | ||
|
||
import Effect (Effect) | ||
import Effect.Aff (Aff, launchAff_) | ||
import Effect.Class (class MonadEffect, liftEffect) | ||
import Effect.Unsafe (unsafePerformEffect) | ||
import Data.Traversable (for_, intercalate) | ||
import Performance.Minibench (BenchResult, benchWith', withUnits) | ||
|
||
|
||
testApply :: forall m. MonadEffect m => Int -> m Unit | ||
testApply n' = do | ||
arr <- liftEffect mkArr | ||
applyLoop (void <<< liftEffect <<< pushToArr arr) n' | ||
where | ||
applyLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit | ||
applyLoop eff max = go (pure unit) 0 | ||
where | ||
go acc n | n == max = acc | ||
go acc n = go (acc <* eff n) (n + 1) | ||
|
||
|
||
testBindRight :: forall m. MonadEffect m => Int -> m Unit | ||
testBindRight n' = do | ||
arr <- liftEffect mkArr | ||
bindRightLoop (void <<< liftEffect <<< pushToArr arr) n' | ||
where | ||
bindRightLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit | ||
bindRightLoop eff max = go (pure unit) 0 | ||
where | ||
go acc n | n == max = acc | ||
go acc n = go (eff (max - n - 1) >>= const acc) (n + 1) | ||
|
||
|
||
testBindLeft :: forall m. MonadEffect m => Int -> m Unit | ||
testBindLeft n' = do | ||
arr <- liftEffect mkArr | ||
bindLeftLoop (void <<< liftEffect <<< pushToArr arr) n' | ||
where | ||
bindLeftLoop :: Monad m => (Int -> m Unit) -> Int -> m Unit | ||
bindLeftLoop eff max = go (pure unit) 0 | ||
where | ||
go acc n | n == max = acc | ||
go acc n = go (acc >>= const (eff n)) (n + 1) | ||
|
||
|
||
testMap :: forall m. MonadEffect m => Int -> m Unit | ||
testMap n = do | ||
arr <- liftEffect mkArr | ||
res <- mapLoop n (liftEffect $ pushToArr arr 0) | ||
pure unit | ||
where | ||
mapLoop :: Monad m => Int -> m Int -> m Int | ||
mapLoop max i = | ||
if max == 0 | ||
then i | ||
else mapLoop (max - 1) (map (_ + 1) i) | ||
|
||
|
||
main :: Effect Unit | ||
main = do | ||
log "<details><summary>benchmark</summary>" | ||
log "| bench | type | n | mean | stddev | min | max |" | ||
log "| ----- | ---- | - | ---- | ------ | --- | --- |" | ||
bench 10 ">>=R" testBindRight testBindRight [100, 1000, 5000] | ||
bench 10 ">>=L" testBindLeft testBindLeft [100, 1000, 5000] | ||
bench 10 "map" testMap testMap [100, 1000, 5000] | ||
bench 10 "apply" testApply testApply [100, 1000, 5000] | ||
log "| - | - | - | - | - | - | - |" | ||
bench 2 ">>=R" testBindRight testBindRight [10000, 50000, 100000, 1000000] | ||
bench 2 ">>=L" testBindLeft testBindLeft [10000, 50000, 100000, 1000000] | ||
bench 2 "map" testMap testMap [10000, 50000, 100000, 1000000, 350000, 700000] | ||
bench 2 "apply" testApply testApply [10000, 50000, 100000, 1000000] | ||
log "</details>" | ||
|
||
bench | ||
:: Int | ||
-> String | ||
-> (Int -> Effect Unit) | ||
-> (Int -> Aff Unit) | ||
-> Array Int | ||
-> Effect Unit | ||
bench n name buildEffect buildAff vals = for_ vals \val -> do | ||
logBench [name <> " build", "Eff", show val] $ benchWith' n \_ -> buildEffect val | ||
logBench' identity [name <> " build", "Aff", show val] $ benchWith' n \_ -> buildAff val | ||
let eff = liftEffect $ buildEffect val | ||
logBench [name <> " run", "Eff", show val] $ benchWith' n \_ -> unsafePerformEffect eff | ||
let aff = launchAff_ $ buildAff val | ||
logBench' identity [name <> " run", "Aff", show val] $ benchWith' n \_ -> unsafePerformEffect aff | ||
|
||
logBench' :: (String -> String) -> Array String -> Effect BenchResult -> Effect Unit | ||
logBench' f msg benchEffect = do | ||
res <- benchEffect | ||
let | ||
logStr = intercalate " | " | ||
$ append msg | ||
$ map (f <<< withUnits) [res.mean, res.stdDev, res.min, res.max] | ||
log $ "| " <> logStr <> " |" | ||
|
||
logBench :: Array String -> Effect BenchResult -> Effect Unit | ||
logBench = logBench' \s -> "**" <> s <> "**" | ||
|
||
foreign import data Arr :: Type -> Type | ||
foreign import mkArr :: forall a. Effect (Arr a) | ||
foreign import pushToArr :: forall a. Arr a -> a -> Effect a | ||
foreign import log :: forall a. a -> Effect Unit | ||
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💀 this should be updated before merge