2019-08-22 02:49:08 +03:00
|
|
|
{-# OPTIONS_GHC -O2 #-}
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Fast implementation of Jam (Noun → Atom).
|
|
|
|
|
|
|
|
This is based on the implementation of `flat`.
|
|
|
|
-}
|
2020-01-24 08:28:38 +03:00
|
|
|
module Urbit.Noun.Jam (jam, jamBS) where
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
import ClassyPrelude hiding (hash)
|
2019-07-12 00:41:09 +03:00
|
|
|
|
2020-01-23 12:22:30 +03:00
|
|
|
import Urbit.Atom
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Noun.Core
|
2019-07-12 04:16:40 +03:00
|
|
|
|
2019-07-12 22:24:44 +03:00
|
|
|
import Data.Bits (clearBit, setBit, shiftL, shiftR, (.|.))
|
2019-07-02 05:43:10 +03:00
|
|
|
import Data.Vector.Primitive ((!))
|
|
|
|
import Foreign.Marshal.Alloc (callocBytes, free)
|
|
|
|
import Foreign.Ptr (Ptr, castPtr, plusPtr)
|
|
|
|
import Foreign.Storable (poke)
|
|
|
|
import GHC.Int (Int(I#))
|
2019-07-12 22:24:44 +03:00
|
|
|
import GHC.Integer.GMP.Internals (BigNat)
|
|
|
|
import GHC.Natural (Natural(NatJ#, NatS#))
|
2019-07-02 05:43:10 +03:00
|
|
|
import GHC.Prim (Word#, plusWord#, word2Int#)
|
|
|
|
import GHC.Word (Word(W#))
|
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
|
|
|
|
2020-03-18 21:25:58 +03:00
|
|
|
import qualified Urbit.Atom.Fast as Atom
|
2019-07-02 05:43:10 +03:00
|
|
|
import qualified Data.ByteString.Unsafe as BS
|
|
|
|
import qualified Data.HashTable.IO as H
|
|
|
|
import qualified Data.Vector.Primitive as VP
|
|
|
|
|
|
|
|
|
|
|
|
-- Exports ---------------------------------------------------------------------
|
|
|
|
|
2019-07-12 00:41:09 +03:00
|
|
|
jamBS :: Noun -> ByteString
|
|
|
|
jamBS n = doPut bt sz (writeNoun n)
|
2019-07-02 05:43:10 +03:00
|
|
|
where
|
2019-07-04 03:53:21 +03:00
|
|
|
(sz, bt) = unsafePerformIO (compress n)
|
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
jam :: Noun -> Atom
|
2020-01-23 12:22:30 +03:00
|
|
|
jam = bytesAtom . jamBS
|
2019-05-21 02:04:28 +03:00
|
|
|
|
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
-- Types -----------------------------------------------------------------------
|
2019-05-21 02:04:28 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
{-|
|
|
|
|
The encoder state.
|
2019-05-21 02:04:28 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
- ptr: Pointer into the output buffer.
|
|
|
|
- reg: Next 64 bits of output, partially written.
|
|
|
|
- off: Number of bits already written into `reg`
|
|
|
|
- pos: Total number of bits written.
|
|
|
|
-}
|
|
|
|
data S = S
|
|
|
|
{ ptr :: {-# UNPACK #-} !(Ptr Word)
|
|
|
|
, reg :: {-# UNPACK #-} !Word
|
|
|
|
, off :: {-# UNPACK #-} !Int
|
|
|
|
, pos :: {-# UNPACK #-} !Word
|
|
|
|
} deriving (Show,Eq,Ord)
|
|
|
|
|
|
|
|
data PutResult a = PutResult {-# UNPACK #-} !S !a
|
|
|
|
deriving Functor
|
|
|
|
|
|
|
|
newtype Put a = Put
|
2019-07-04 03:53:21 +03:00
|
|
|
{ runPut :: H.CuckooHashTable Word Word
|
2019-07-02 05:43:10 +03:00
|
|
|
-> S
|
|
|
|
-> IO (PutResult a)
|
|
|
|
}
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2019-05-21 02:04:28 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
{-# INLINE getRef #-}
|
|
|
|
getRef :: Put (Maybe Word)
|
2019-07-12 22:24:44 +03:00
|
|
|
getRef = Put $ \tbl s -> PutResult s <$> H.lookup tbl (pos s)
|
2019-05-21 02:04:28 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-07-02 05:43:10 +03:00
|
|
|
1. Write the register to the output, and increment the output pointer.
|
|
|
|
-}
|
|
|
|
{-# INLINE flush #-}
|
|
|
|
flush :: Put ()
|
|
|
|
flush = Put $ \tbl s@S{..} -> do
|
|
|
|
poke ptr reg
|
|
|
|
pure $ PutResult (s { ptr = ptr `plusPtr` 8 }) ()
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
{-# INLINE update #-}
|
|
|
|
update :: (S -> S) -> Put ()
|
2019-07-12 22:24:44 +03:00
|
|
|
update f = Put $ \tbl s@S{..} -> pure (PutResult (f s) ())
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
{-# INLINE setRegOff #-}
|
|
|
|
setRegOff :: Word -> Int -> Put ()
|
2019-07-12 22:24:44 +03:00
|
|
|
setRegOff r o = update $ \s@S{..} -> (s {reg=r, off=o})
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
{-# INLINE setReg #-}
|
|
|
|
setReg :: Word -> Put ()
|
2019-07-12 22:24:44 +03:00
|
|
|
setReg r = update $ \s@S{..} -> (s { reg=r })
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
{-# INLINE getS #-}
|
|
|
|
getS :: Put S
|
|
|
|
getS = Put $ \tbl s -> pure (PutResult s s)
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
{-# INLINE putS #-}
|
|
|
|
putS :: S -> Put ()
|
|
|
|
putS s = Put $ \tbl _ -> pure (PutResult s ())
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-07-02 05:43:10 +03:00
|
|
|
To write a bit:
|
2019-06-29 04:46:33 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
| reg |= 1 << off
|
|
|
|
| off <- (off + 1) % 64
|
|
|
|
| if (!off):
|
|
|
|
| buf[w++] <- reg
|
|
|
|
| reg <- 0
|
|
|
|
-}
|
|
|
|
{-# INLINE writeBit #-}
|
|
|
|
writeBit :: Bool -> Put ()
|
|
|
|
writeBit b = Put $ \tbl s@S{..} -> do
|
|
|
|
let s' = s { reg = (if b then setBit else clearBit) reg off
|
|
|
|
, off = (off + 1) `mod` 64
|
|
|
|
, pos = pos + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if off == 63
|
|
|
|
then runPut (flush >> setRegOff 0 0) tbl s'
|
|
|
|
else pure $ PutResult s' ()
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-07-02 05:43:10 +03:00
|
|
|
To write a 64bit word:
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
| reg |= w << off
|
|
|
|
| buf[bufI++] = reg
|
|
|
|
| reg = w >> (64 - off)
|
2019-05-21 10:25:58 +03:00
|
|
|
-}
|
2019-07-02 05:43:10 +03:00
|
|
|
{-# INLINE writeWord #-}
|
|
|
|
writeWord :: Word -> Put ()
|
|
|
|
writeWord wor = do
|
|
|
|
S{..} <- getS
|
|
|
|
setReg (reg .|. shiftL wor off)
|
|
|
|
flush
|
2019-07-12 22:24:44 +03:00
|
|
|
update $ \s -> s { pos = 64 + pos
|
|
|
|
, reg = shiftR wor (64 - off)
|
|
|
|
}
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-07-02 05:43:10 +03:00
|
|
|
To write some bits (< 64) from a word:
|
|
|
|
|
|
|
|
| wor = takeBits(wid, wor)
|
|
|
|
| reg = reg .|. (wor << off)
|
|
|
|
| off = (off + wid) % 64
|
|
|
|
|
|
|
|
|
| if (off + wid >= 64)
|
|
|
|
| buf[w] = x
|
|
|
|
| reg = wor >> (wid - off)
|
2019-05-21 10:25:58 +03:00
|
|
|
-}
|
2019-07-02 05:43:10 +03:00
|
|
|
{-# INLINE writeBitsFromWord #-}
|
|
|
|
writeBitsFromWord :: Int -> Word -> Put ()
|
|
|
|
writeBitsFromWord wid wor = do
|
2020-03-18 21:25:58 +03:00
|
|
|
wor <- pure (Atom.takeBitsWord wid wor)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
oldSt <- getS
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
let newSt = oldSt { reg = reg oldSt .|. shiftL wor (off oldSt)
|
|
|
|
, off = (off oldSt + wid) `mod` 64
|
|
|
|
, pos = fromIntegral wid + pos oldSt
|
|
|
|
}
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
putS newSt
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
when (wid + off oldSt >= 64) $ do
|
|
|
|
flush
|
|
|
|
setReg (shiftR wor (wid - off newSt))
|
2020-01-23 07:16:09 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Write all of the the signficant bits of a direct atom.
|
2019-07-02 05:43:10 +03:00
|
|
|
-}
|
|
|
|
{-# INLINE writeAtomWord# #-}
|
|
|
|
writeAtomWord# :: Word# -> Put ()
|
|
|
|
writeAtomWord# w = do
|
2020-03-18 21:25:58 +03:00
|
|
|
writeBitsFromWord (I# (word2Int# (Atom.wordBitWidth# w))) (W# w)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
{-# INLINE writeAtomWord #-}
|
|
|
|
writeAtomWord :: Word -> Put ()
|
|
|
|
writeAtomWord (W# w) = writeAtomWord# w
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-07-02 05:43:10 +03:00
|
|
|
Write all of the the signficant bits of an indirect atom.
|
|
|
|
|
|
|
|
TODO Use memcpy when the bit-offset of the output is divisible by 8.
|
|
|
|
-}
|
|
|
|
{-# INLINE writeAtomBigNat #-}
|
|
|
|
writeAtomBigNat :: BigNat -> Put ()
|
2020-03-18 21:25:58 +03:00
|
|
|
writeAtomBigNat !(Atom.bigNatWords -> words) = do
|
2019-07-02 05:43:10 +03:00
|
|
|
let lastIdx = VP.length words - 1
|
2019-07-12 22:24:44 +03:00
|
|
|
for_ [0..(lastIdx-1)] $ \i ->
|
2019-07-02 05:43:10 +03:00
|
|
|
writeWord (words ! i)
|
|
|
|
writeAtomWord (words ! lastIdx)
|
|
|
|
|
|
|
|
{-# INLINE writeAtomBits #-}
|
|
|
|
writeAtomBits :: Atom -> Put ()
|
2019-07-12 04:16:40 +03:00
|
|
|
writeAtomBits = \case NatS# wd -> writeAtomWord# wd
|
|
|
|
NatJ# bn -> writeAtomBigNat bn
|
2019-07-02 05:43:10 +03:00
|
|
|
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
-- Put Instances ---------------------------------------------------------------
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
instance Functor Put where
|
|
|
|
fmap f g = Put $ \tbl s -> do
|
|
|
|
PutResult s' a <- runPut g tbl s
|
|
|
|
pure $ PutResult s' (f a)
|
|
|
|
{-# INLINE fmap #-}
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
instance Applicative Put where
|
|
|
|
pure x = Put (\_ s -> return $ PutResult s x)
|
|
|
|
{-# INLINE pure #-}
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
Put f <*> Put g = Put $ \tbl s1 -> do
|
|
|
|
PutResult s2 f' <- f tbl s1
|
|
|
|
PutResult s3 g' <- g tbl s2
|
|
|
|
return $ PutResult s3 (f' g')
|
|
|
|
{-# INLINE (<*>) #-}
|
2019-05-21 10:25:58 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
Put f *> Put g = Put $ \tbl s1 -> do
|
|
|
|
PutResult s2 _ <- f tbl s1
|
|
|
|
g tbl s2
|
|
|
|
{-# INLINE (*>) #-}
|
|
|
|
|
|
|
|
instance Monad Put where
|
|
|
|
return = pure
|
|
|
|
{-# INLINE return #-}
|
|
|
|
|
|
|
|
(>>) = (*>)
|
|
|
|
{-# INLINE (>>) #-}
|
|
|
|
|
|
|
|
Put x >>= f = Put $ \tbl s -> do
|
|
|
|
PutResult s' x' <- x tbl s
|
|
|
|
runPut (f x') tbl s'
|
|
|
|
{-# INLINE (>>=) #-}
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2019-07-04 03:53:21 +03:00
|
|
|
doPut :: H.CuckooHashTable Word Word -> Word -> Put () -> ByteString
|
2019-07-10 00:57:48 +03:00
|
|
|
doPut !tbl !sz m =
|
2019-07-02 05:43:10 +03:00
|
|
|
unsafePerformIO $ do
|
2019-07-04 07:01:40 +03:00
|
|
|
-- traceM "doPut"
|
2019-07-02 05:43:10 +03:00
|
|
|
buf <- callocBytes (fromIntegral (wordSz*8))
|
|
|
|
_ <- runPut (m >> mbFlush) tbl (S buf 0 0 0)
|
|
|
|
BS.unsafePackCStringFinalizer (castPtr buf) byteSz (free buf)
|
|
|
|
where
|
2019-07-10 00:57:48 +03:00
|
|
|
!wordSz = fromIntegral (sz `divUp` 64)
|
|
|
|
!byteSz = fromIntegral (sz `divUp` 8)
|
|
|
|
!divUp = \x y -> (x `div` y) + (if x `mod` y == 0 then 0 else 1)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
mbFlush :: Put ()
|
|
|
|
mbFlush = do
|
|
|
|
shouldFlush <- (/= 0) . off <$> getS
|
|
|
|
when shouldFlush flush
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2019-07-02 05:43:10 +03:00
|
|
|
TODO Handle back references
|
2019-05-21 10:25:58 +03:00
|
|
|
-}
|
2019-07-12 00:41:09 +03:00
|
|
|
writeNoun :: Noun -> Put ()
|
2019-07-10 00:57:48 +03:00
|
|
|
writeNoun !n =
|
2019-07-02 05:43:10 +03:00
|
|
|
getRef >>= \case
|
|
|
|
Just bk -> writeBackRef bk
|
2019-07-12 00:41:09 +03:00
|
|
|
Nothing -> case n of Atom a -> writeAtom a
|
|
|
|
Cell h t -> writeCell h t
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
{-# INLINE writeMat #-}
|
|
|
|
writeMat :: Atom -> Put ()
|
|
|
|
writeMat 0 = writeBit True
|
|
|
|
writeMat atm = do
|
|
|
|
writeBitsFromWord (preWid+1) (shiftL 1 preWid)
|
|
|
|
writeBitsFromWord (preWid-1) atmWid
|
|
|
|
writeAtomBits atm
|
|
|
|
where
|
2020-03-18 21:25:58 +03:00
|
|
|
atmWid = Atom.atomBitWidth atm
|
|
|
|
preWid = fromIntegral (Atom.wordBitWidth atmWid)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
{-# INLINE writeCell #-}
|
2019-07-12 00:41:09 +03:00
|
|
|
writeCell :: Noun -> Noun -> Put ()
|
2019-07-10 00:57:48 +03:00
|
|
|
writeCell !h !t = do
|
2019-07-02 05:43:10 +03:00
|
|
|
writeBit True
|
|
|
|
writeBit False
|
|
|
|
writeNoun h
|
|
|
|
writeNoun t
|
|
|
|
|
|
|
|
{-# INLINE writeAtom #-}
|
|
|
|
writeAtom :: Atom -> Put ()
|
2019-07-10 00:57:48 +03:00
|
|
|
writeAtom !a = do
|
2019-07-02 05:43:10 +03:00
|
|
|
writeBit False
|
|
|
|
writeMat a
|
|
|
|
|
|
|
|
{-# INLINE writeBackRef #-}
|
|
|
|
writeBackRef :: Word -> Put ()
|
2019-07-10 00:57:48 +03:00
|
|
|
writeBackRef !a = do
|
2019-07-02 05:43:10 +03:00
|
|
|
p <- pos <$> getS
|
|
|
|
writeBit True
|
|
|
|
writeBit True
|
2019-07-12 04:16:40 +03:00
|
|
|
writeMat (fromIntegral a)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Calculate Jam Size and Backrefs ---------------------------------------------
|
|
|
|
|
|
|
|
{-# INLINE matSz #-}
|
|
|
|
matSz :: Atom -> Word
|
2019-07-10 00:57:48 +03:00
|
|
|
matSz !a = W# (matSz# a)
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
{-# INLINE matSz# #-}
|
|
|
|
matSz# :: Atom -> Word#
|
|
|
|
matSz# 0 = 1##
|
|
|
|
matSz# a = preW `plusWord#` preW `plusWord#` atmW
|
|
|
|
where
|
2020-03-18 21:25:58 +03:00
|
|
|
atmW = Atom.atomBitWidth# a
|
|
|
|
preW = Atom.wordBitWidth# atmW
|
2019-07-02 05:43:10 +03:00
|
|
|
|
2019-07-04 07:01:40 +03:00
|
|
|
{-# INLINE atomSz #-}
|
|
|
|
atomSz :: Atom -> Word
|
2019-07-10 00:57:48 +03:00
|
|
|
atomSz !w = 1 + matSz w
|
2019-07-04 07:01:40 +03:00
|
|
|
|
2019-07-04 03:53:21 +03:00
|
|
|
{-# INLINE refSz #-}
|
|
|
|
refSz :: Word -> Word
|
2019-07-10 00:57:48 +03:00
|
|
|
refSz !w = 1 + jamWordSz w
|
2019-07-04 07:01:40 +03:00
|
|
|
|
|
|
|
{-# INLINE jamWordSz #-}
|
|
|
|
jamWordSz :: Word -> Word
|
|
|
|
jamWordSz 0 = 2
|
|
|
|
jamWordSz (W# w) = 1 + 2*(W# preW) + (W# atmW)
|
|
|
|
where
|
2020-03-18 21:25:58 +03:00
|
|
|
atmW = Atom.wordBitWidth# w
|
|
|
|
preW = Atom.wordBitWidth# atmW
|
2019-07-02 05:43:10 +03:00
|
|
|
|
2019-07-12 00:41:09 +03:00
|
|
|
compress :: Noun -> IO (Word, H.CuckooHashTable Word Word)
|
2019-07-10 00:57:48 +03:00
|
|
|
compress !top = do
|
2019-07-05 01:40:36 +03:00
|
|
|
let sz = max 50
|
|
|
|
$ min 10_000_000
|
2019-07-12 00:41:09 +03:00
|
|
|
$ (2*) $ (10^) $ floor $ logBase 600 $ fromIntegral $ nounSize top
|
2019-07-05 01:06:41 +03:00
|
|
|
|
2019-07-12 00:41:09 +03:00
|
|
|
nodes :: H.BasicHashTable Noun Word <- H.newSized sz
|
|
|
|
backs :: H.CuckooHashTable Word Word <- H.newSized sz
|
2019-07-02 05:43:10 +03:00
|
|
|
|
2019-07-12 00:41:09 +03:00
|
|
|
let proc :: Word -> Noun -> IO Word
|
2019-07-10 00:57:48 +03:00
|
|
|
proc !pos = \case
|
2019-07-12 00:41:09 +03:00
|
|
|
Atom a -> pure (atomSz a)
|
|
|
|
Cell h t -> do !hSz <- go (pos+2) h
|
|
|
|
!tSz <- go (pos+2+hSz) t
|
|
|
|
pure (2+hSz+tSz)
|
|
|
|
|
|
|
|
go :: Word -> Noun -> IO Word
|
2019-07-10 00:57:48 +03:00
|
|
|
go !p !inp = do
|
2019-07-02 05:43:10 +03:00
|
|
|
H.lookup nodes inp >>= \case
|
|
|
|
Nothing -> do
|
|
|
|
H.insert nodes inp p
|
|
|
|
proc p inp
|
2019-07-04 03:53:21 +03:00
|
|
|
Just bak -> do
|
2019-07-04 07:01:40 +03:00
|
|
|
let rs = refSz bak
|
|
|
|
doRef = H.insert backs p bak $> rs
|
|
|
|
noRef = proc p inp
|
|
|
|
case inp of
|
2019-07-12 00:41:09 +03:00
|
|
|
Cell _ _ -> doRef
|
|
|
|
Atom a | rs < atomSz (fromIntegral a) -> doRef
|
|
|
|
_ -> noRef
|
2019-07-02 05:43:10 +03:00
|
|
|
|
|
|
|
res <- go 0 top
|
2019-07-05 01:40:36 +03:00
|
|
|
|
2019-07-02 05:43:10 +03:00
|
|
|
pure (res, backs)
|