Skip to content

Commit 9a6c70b

Browse files
committed
restore
1 parent d278433 commit 9a6c70b

File tree

4 files changed

+23
-48
lines changed

4 files changed

+23
-48
lines changed
Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Benchmark.Nockma.Encoding.Natural where
22

3+
import Data.Serialize qualified as Serial
34
import Juvix.Prelude
45
import Juvix.Prelude.Bytes
56
import System.Random
@@ -9,7 +10,7 @@ randomNatural :: IO Natural
910
randomNatural = do
1011
sg <- getStdGen
1112
let numDigits :: Natural = 1000000
12-
return (fst (uniformR (10 ^ numDigits, 10 ^ numDigits + 100) sg))
13+
return (fst (uniformR (10 ^ numDigits, 10 ^ numDigits + 10000) sg))
1314

1415
randomNaturalBS :: IO ByteString
1516
randomNaturalBS = naturalToByteString <$> randomNatural
@@ -27,50 +28,25 @@ bm = bgroup "Natural Encoding" [byteStringToNatBm, natToByteStringBm]
2728
(\nat -> bench "Old" (nf old nat)),
2829
env
2930
randomNaturalBS
30-
(\nat -> bench "New" (nf new nat)),
31-
env
32-
randomNaturalBS
33-
( \nat ->
34-
bench
35-
"TEST"
36-
( nf
37-
( \i ->
38-
if new i == old i
39-
then True
40-
else error "wrong"
41-
)
42-
nat
43-
)
44-
)
31+
(\nat -> bench "New" (nf new nat))
4532
]
4633

4734
natToByteStringBm =
48-
let old = padByteStringWord . naturalToByteStringOld
49-
new = padByteStringWord . naturalToByteString
50-
new2 = padByteStringWord . naturalToByteStringTest False
35+
let shiftBased = padByteStringWord . naturalToByteStringOld
36+
wordBased = padByteStringWord . naturalToByteString
37+
byteBased = padByteStringWord . naturalToByteStringHelper True
5138
in bgroup
5239
"Natural -> ByteString"
5340
[ env
5441
randomNatural
55-
(\nat -> bench "Old" (nf old nat)),
42+
(\nat -> bench "Shift based" (nf shiftBased nat)),
5643
env
5744
randomNatural
58-
(\nat -> bench "New" (nf new nat)),
45+
(\nat -> bench "Word64 based" (nf wordBased nat)),
5946
env
6047
randomNatural
61-
(\nat -> bench "New2" (nf new2 nat)),
48+
(\nat -> bench "Word8 based" (nf byteBased nat)),
6249
env
6350
randomNatural
64-
( \nat ->
65-
bench
66-
"TEST"
67-
( nf
68-
( \i ->
69-
if new i == new2 i
70-
then True
71-
else error "wrong"
72-
)
73-
nat
74-
)
75-
)
51+
(\nat -> bench "Cereal library" (nf Serial.encode nat))
7652
]

bench2/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Test.Tasty.Bench
99
main :: IO ()
1010
main =
1111
defaultMain
12-
[ -- Effect.bm,
13-
-- Nockma.bm
12+
[ Effect.bm,
13+
Nockma.bm,
1414
Natural.bm
1515
]

src/Juvix/Compiler/Nockma/Encoding/ByteString.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# LANGUAGE MagicHash #-}
2-
31
module Juvix.Compiler.Nockma.Encoding.ByteString where
42

53
import Crypto.Hash.SHA256 qualified as SHA256

src/Juvix/Prelude/Bytes.hs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ naturalToBytes = \case
2929
NB b -> byteArrayToBytes (GHCByteArray.ByteArray b)
3030

3131
-- | Pad a ByteString with zeros up to the smallest length such that is
32-
-- divisible by the given arg
32+
-- divisible by `align`
3333
padByteStringMod :: Int -> ByteString -> ByteString
3434
padByteStringMod align bs =
3535
let (d, m) = divMod (BS.length bs) align
@@ -49,23 +49,24 @@ padByteString n bs
4949
naturalToByteStringLELen :: Int -> Natural -> ByteString
5050
naturalToByteStringLELen len = padByteString len . naturalToByteStringLE
5151

52+
-- | This is quadratic (`shiftR` is O(n))
5253
naturalToByteStringOld :: Natural -> ByteString
53-
naturalToByteStringOld = naturalToByteStringLE
54-
55-
-- | TODO: this is quadratic (`shiftR` is O(n))
56-
naturalToByteStringLE :: Natural -> ByteString
57-
naturalToByteStringLE = BS.toStrict . BS.toLazyByteString . go
54+
naturalToByteStringOld = do
55+
BS.toStrict . BS.toLazyByteString . go
5856
where
5957
go :: Natural -> BS.Builder
6058
go = \case
6159
0 -> mempty
6260
n -> BS.word8 (fromIntegral n) <> go (n `shiftR` 8)
6361

62+
naturalToByteStringLE :: Natural -> ByteString
63+
naturalToByteStringLE = naturalToByteString
64+
6465
-- | Little endian
65-
naturalToByteStringTest :: Bool -> Natural -> ByteString
66-
naturalToByteStringTest word64 n
66+
naturalToByteStringHelper :: Bool -> Natural -> ByteString
67+
naturalToByteStringHelper forceWord8 n
6768
-- most common case
68-
| word64 && 8 == bytesPerWord =
69+
| (not forceWord8) && 8 == bytesPerWord =
6970
let w :: [Word64] = naturalToWord64 n
7071
in build (mconcat (map BS.word64LE w))
7172
| otherwise = BS.pack (naturalToBytes n)
@@ -74,7 +75,7 @@ naturalToByteStringTest word64 n
7475
build = BS.toStrict . BS.toLazyByteString
7576

7677
naturalToByteString :: Natural -> ByteString
77-
naturalToByteString = naturalToByteStringTest True
78+
naturalToByteString = naturalToByteStringHelper False
7879

7980
-- | Little endian
8081
wordToBytes :: Word -> [Word8]

0 commit comments

Comments
 (0)