Skip to content

Commit f3fd0d7

Browse files
authored
Update FFI to be more portable and direct. (#25)
* Update FFI to be more portable and direct. * Update CHANGELOG
1 parent 102810b commit f3fd0d7

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ New features:
1111
Bugfixes:
1212

1313
Other improvements:
14+
- Update FFI to use a more direct and portable convention (#25 by @natefaubion)
1415

1516
## [v4.0.0](https://github.com/purescript/purescript-minibench/releases/tag/v4.0.0) - 2022-04-27
1617

src/Performance/Minibench.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
export const hrTime = process.hrtime;
1+
export function timeNs(k) {
2+
const t1 = process.hrtime();
3+
k();
4+
const t2 = process.hrtime(t1);
5+
return t2[0] * 1.0e9 + t2[1];
6+
}
27

38
export function gc() {
49
global.gc && global.gc();

src/Performance/Minibench.purs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,16 @@ import Effect.Console (log)
2020
import Effect.Ref as Ref
2121
import Effect.Uncurried (EffectFn1, runEffectFn1)
2222
import Data.Number (infinity, max, min, sqrt)
23-
import Partial.Unsafe (unsafeCrashWith)
2423

25-
-- | A wrapper around the Node `process.hrtime()` function.
26-
foreign import hrTime :: EffectFn1 (Array Int) (Array Int)
24+
-- | Returns the number of nanoseconds it takes to evaluate the given closure.
25+
foreign import timeNs :: forall a. EffectFn1 (Unit -> a) Number
2726

2827
-- | Force garbage collection.
2928
-- | Requires node to be run with the --force-gc flag.
3029
foreign import gc :: Effect Unit
3130

3231
foreign import toFixed :: Number -> String
3332

34-
fromHrTime :: Array Int -> Number
35-
fromHrTime [s, ns] = toNumber s * 1.0e9 + toNumber ns
36-
fromHrTime _ = unsafeCrashWith "fromHrTime: unexpected result from process.hrtime()"
37-
3833
withUnits :: Number -> String
3934
withUnits t
4035
| t < 1.0e3 = toFixed t <> " ns"
@@ -79,10 +74,8 @@ benchWith' n f = do
7974
maxRef <- Ref.new 0.0
8075
gc
8176
forE 0 n \_ -> do
82-
t1 <- runEffectFn1 hrTime [0, 0]
83-
t2 <- const (runEffectFn1 hrTime t1) (f unit)
84-
let ns = fromHrTime t2
85-
square = ns * ns
77+
ns <- runEffectFn1 timeNs f
78+
let square = ns * ns
8679
_ <- Ref.modify (_ + ns) sumRef
8780
_ <- Ref.modify (_ + square) sum2Ref
8881
_ <- Ref.modify (_ `min` ns) minRef

0 commit comments

Comments
 (0)