This repository was archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Makes Eff Harder, Better, Faster, Stronger #31
Open
safareli
wants to merge
6
commits into
purescript-deprecated:master
Choose a base branch
from
safareli:fast
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fea0cdb
Make Eff faster and safer
safareli 993e2c3
add tests
safareli 9e35b46
add bench project
safareli b41072f
fix warnings
safareli f1d8772
use lower bench number
safareli 8504913
remove bench scripts from root package
safareli 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
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,12 @@ | ||
{ | ||
"name": "purescript-eff-aff-bench", | ||
"dependencies": { | ||
"purescript-foldable-traversable": "^3.6.1", | ||
"purescript-minibench": "safareli/purescript-minibench#un-log", | ||
"purescript-eff": "safareli/purescript-eff#fast", | ||
"purescript-aff": "^4.0.1" | ||
}, | ||
"resolutions": { | ||
"purescript-eff": "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 @@ | ||
../node_modules/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how useful will be this bench project might be in future, we can also remove it too. This file is result of running |
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,10 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"clean": "rimraf output && rimraf .pulp-cache", | ||
"bench": "npm run build && npm run run", | ||
"run": "node --expose-gc -e 'require(\"./output/Bench.Main/index.js\").main()'", | ||
"build": "pulp build -- --censor-lib --strict" | ||
} | ||
} | ||
|
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,20 @@ | ||
"use strict"; | ||
|
||
exports.mkArr = function(){ | ||
return { count: 0 }; | ||
}; | ||
|
||
exports.pushToArr = function(xs) { | ||
return function(x) { | ||
return function() { | ||
xs.count += 1 | ||
return xs; | ||
}; | ||
}; | ||
}; | ||
|
||
exports.log = function(x) { | ||
return function(){ | ||
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,110 @@ | ||
module Bench.Main where | ||
|
||
import Prelude | ||
|
||
import Control.Monad.Eff (Eff) | ||
import Control.Monad.Aff (Aff, launchAff_) | ||
import Control.Monad.Eff.Class (class MonadEff, liftEff) | ||
import Control.Monad.Eff.Console (CONSOLE) | ||
import Control.Monad.Eff.Unsafe (unsafePerformEff) | ||
import Data.Traversable (for_, intercalate) | ||
import Performance.Minibench (BenchResult, benchWith', withUnits) | ||
|
||
|
||
type BenchEff = (console :: CONSOLE) | ||
|
||
testApply :: forall m. MonadEff BenchEff m => Int -> m Unit | ||
testApply n' = do | ||
arr <- liftEff mkArr | ||
applyLoop (void <<< liftEff <<< 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. MonadEff BenchEff m => Int -> m Unit | ||
testBindRight n' = do | ||
arr <- liftEff mkArr | ||
bindRightLoop (void <<< liftEff <<< 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. MonadEff BenchEff m => Int -> m Unit | ||
testBindLeft n' = do | ||
arr <- liftEff mkArr | ||
bindLeftLoop (void <<< liftEff <<< 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. MonadEff BenchEff m => Int -> m Unit | ||
testMap n = do | ||
arr <- liftEff mkArr | ||
res <- mapLoop n (liftEff $ 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 :: Eff BenchEff Unit | ||
main = do | ||
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] | ||
|
||
bench | ||
:: Int | ||
-> String | ||
-> (Int -> Eff BenchEff Unit) | ||
-> (Int -> Aff BenchEff Unit) | ||
-> Array Int | ||
-> Eff BenchEff Unit | ||
bench n name buildEff buildAff vals = for_ vals \val -> do | ||
logBench [name <> " build", "Eff", show val] $ benchWith' n \_ -> buildEff val | ||
logBench' id [name <> " build", "Aff", show val] $ benchWith' n \_ -> buildAff val | ||
let eff = liftEff $ buildEff val | ||
logBench [name <> " run", "Eff", show val] $ benchWith' n \_ -> unsafePerformEff eff | ||
let aff = launchAff_ $ buildAff val | ||
logBench' id [name <> " run", "Aff", show val] $ benchWith' n \_ -> unsafePerformEff aff | ||
|
||
logBench' :: (String -> String) -> Array String -> Eff BenchEff BenchResult -> Eff BenchEff Unit | ||
logBench' f msg benchEff = do | ||
res <- benchEff | ||
let | ||
logStr = intercalate " | " | ||
$ append msg | ||
$ map (f <<< withUnits) [res.mean, res.stdDev, res.min, res.max] | ||
log $ "| " <> logStr <> " |" | ||
|
||
logBench :: Array String -> Eff BenchEff BenchResult -> Eff BenchEff Unit | ||
logBench = logBench' \s -> "**" <> s <> "**" | ||
|
||
foreign import data Arr :: Type -> Type | ||
foreign import mkArr :: forall e a. Eff e (Arr a) | ||
foreign import pushToArr :: forall e a. Arr a -> a -> Eff e a | ||
foreign import log :: forall e a. a -> Eff e 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
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,56 @@ | ||
"use strict"; | ||
|
||
exports.mkArr = function(){ | ||
return []; | ||
}; | ||
|
||
exports.unArr = function(xs){ | ||
return xs.slice(0); | ||
}; | ||
|
||
exports.pushToArr = function(xs) { | ||
return function(x) { | ||
return function() { | ||
xs.push(x); | ||
return x; | ||
}; | ||
}; | ||
}; | ||
|
||
exports.assert = function(isOk) { | ||
return function(msg) { | ||
return function() { | ||
if (isOk == false) { | ||
throw new Error("assertion failed: " + msg); | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
exports.naturals = function(n) { | ||
var res = []; | ||
for (var index = 0; index < n; index++) { | ||
res[index] = index; | ||
} | ||
return res; | ||
}; | ||
|
||
exports.log = function(x) { | ||
return function(){ | ||
console.log(x) | ||
} | ||
}; | ||
|
||
|
||
exports.time = function(x) { | ||
return function(){ | ||
console.time(x) | ||
} | ||
}; | ||
|
||
|
||
exports.timeEnd = function(x) { | ||
return function(){ | ||
console.timeEnd(x) | ||
} | ||
}; |
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.
purescript/purescript-minibench#9