Skip to content

Commit

Permalink
Merge pull request #21 from haskell-works/replace-constructN-with-gen…
Browse files Browse the repository at this point in the history
…erate

Replace constructN with generate
  • Loading branch information
newhoggy authored Feb 2, 2020
2 parents e8120db + 1653208 commit 0e06fd3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
59 changes: 29 additions & 30 deletions src/HaskellWorks/Data/Json/Standard/Cursor/Internal/StateMachine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,67 +27,66 @@ newtype IntState = IntState Int deriving (Eq, Ord, Show, Num)
data State = InJson | InString | InEscape | InValue deriving (Eq, Enum, Bounded, Show)

phiTable :: DV.Vector (DVS.Vector Word8)
phiTable = DV.constructN 5 gos
where gos :: DV.Vector (DVS.Vector Word8) -> DVS.Vector Word8
gos v = DVS.constructN 256 go
where vi = DV.length v
go :: DVS.Vector Word8 -> Word8
go u = fromIntegral (snd (stateMachine (fromIntegral ui) (toEnum vi)))
where ui = DVS.length u
phiTable = DV.generate 5 gos
where gos :: Int -> DVS.Vector Word8
gos vj = DVS.generate 256 go
where vi = fromIntegral vj
go :: Int -> Word8
go uj = fromIntegral (snd (stateMachine (fromIntegral uj) (toEnum vi)))
{-# NOINLINE phiTable #-}

phiTable2 :: DVS.Vector Word8
phiTable2 = DVS.constructN (4 * fromIntegral iLen) go
phiTable2 = DVS.generate (4 * fromIntegral iLen) go
where iLen = 256 :: Int
go :: DVS.Vector Word8 -> Word8
go u = fromIntegral (snd (stateMachine (fromIntegral ui) (toEnum (fromIntegral uj))))
where (uj, ui) = fromIntegral (DVS.length u) `divMod` iLen
go :: Int -> Word8
go uj = fromIntegral (snd (stateMachine (fromIntegral ui) (toEnum (fromIntegral uk))))
where (uk, ui) = fromIntegral uj `divMod` iLen
{-# NOINLINE phiTable2 #-}

lookupPhiTable :: IntState -> Word8 -> Word8
lookupPhiTable (IntState s) w = DVS.unsafeIndex phiTable2 (s * 256 + fromIntegral w)
{-# INLINE lookupPhiTable #-}

phiTableSimd :: DVS.Vector Word32
phiTableSimd = DVS.constructN 256 go
where go :: DVS.Vector Word32 -> Word32
go v = (snd (stateMachine vi InJson ) .<. 0) .|.
phiTableSimd = DVS.generate 256 go
where go :: Int -> Word32
go vj = (snd (stateMachine vi InJson ) .<. 0) .|.
(snd (stateMachine vi InString) .<. 8) .|.
(snd (stateMachine vi InEscape) .<. 16) .|.
(snd (stateMachine vi InValue ) .<. 24)
where vi = fromIntegral (DVS.length v)
where vi = fromIntegral vj
{-# NOINLINE phiTableSimd #-}

transitionTable :: DV.Vector (DVS.Vector Word8)
transitionTable = DV.constructN 5 gos
where gos :: DV.Vector (DVS.Vector Word8) -> DVS.Vector Word8
gos v = DVS.constructN 256 go
where vi = DV.length v
go :: DVS.Vector Word8 -> Word8
go u = fromIntegral (fromEnum (fst (stateMachine ui (toEnum vi))))
where ui = fromIntegral (DVS.length u)
transitionTable = DV.generate 5 gos
where gos :: Int -> DVS.Vector Word8
gos vj = DVS.generate 256 go
where vi = fromIntegral vj
go :: Int -> Word8
go uj = fromIntegral (fromEnum (fst (stateMachine ui (toEnum vi))))
where ui = fromIntegral uj
{-# NOINLINE transitionTable #-}

transitionTable2 :: DVS.Vector Word8
transitionTable2 = DVS.constructN (4 * fromIntegral iLen) go
transitionTable2 = DVS.generate (4 * fromIntegral iLen) go
where iLen = 256 :: Int
go :: DVS.Vector Word8 -> Word8
go u = fromIntegral (fromEnum (fst (stateMachine (fromIntegral ui) (toEnum (fromIntegral uj)))))
where (uj, ui) = fromIntegral (DVS.length u) `divMod` iLen
go :: Int -> Word8
go uj = fromIntegral (fromEnum (fst (stateMachine (fromIntegral ui) (toEnum (fromIntegral uk)))))
where (uk, ui) = fromIntegral uj `divMod` iLen
{-# NOINLINE transitionTable2 #-}

lookupTransitionTable :: IntState -> Word8 -> IntState
lookupTransitionTable (IntState s) w = fromIntegral (DVS.unsafeIndex transitionTable2 (s * 256 + fromIntegral w))
{-# INLINE lookupTransitionTable #-}

transitionTableSimd :: DVS.Vector Word64
transitionTableSimd = DVS.constructN 256 go
where go :: DVS.Vector Word64 -> Word64
go v = fromIntegral (fromEnum (fst (stateMachine vi InJson ))) .|.
transitionTableSimd = DVS.generate 256 go
where go :: Int -> Word64
go vj = fromIntegral (fromEnum (fst (stateMachine vi InJson ))) .|.
fromIntegral (fromEnum (fst (stateMachine vi InString))) .|.
fromIntegral (fromEnum (fst (stateMachine vi InEscape))) .|.
fromIntegral (fromEnum (fst (stateMachine vi InValue )))
where vi = fromIntegral (DVS.length v)
where vi = fromIntegral vj
{-# NOINLINE transitionTableSimd #-}

stateMachine :: Word8 -> State -> (State, Word32)
Expand Down
18 changes: 9 additions & 9 deletions src/HaskellWorks/Data/Json/Standard/Cursor/SemiIndex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,16 @@ buildFromByteString3 bss = makePreSiChunk <$> buildFromByteString2 bss
where makePreSiChunk :: DVS.Vector Word64 -> PreSiChunk (DVS.Vector Word64)
makePreSiChunk v = PreSiChunk (makeIb v) (makeBpOp v) (makeBpCl v)
makeIb :: DVS.Vector Word64 -> DVS.Vector Word64
makeIb v = asVector64 $ BS.toByteString $ DVS.constructN (DVS.length v) go
where go :: DVS.Vector Word16 -> Word16
go u = let ui = DVS.length u in fromIntegral (pext (DVS.unsafeIndex v ui) 0x4444444444444444)
makeIb v = asVector64 $ BS.toByteString $ DVS.generate (DVS.length v) go
where go :: Int -> Word16
go uj = let ui = fromIntegral uj in fromIntegral (pext (DVS.unsafeIndex v ui) 0x4444444444444444)
makeBpOp :: DVS.Vector Word64 -> DVS.Vector Word64
makeBpOp v = asVector64 $ BS.toByteString $ DVS.constructN (DVS.length v) go
where go :: DVS.Vector Word16 -> Word16
go u = let ui = DVS.length u in fromIntegral (pext (DVS.unsafeIndex v ui) 0x2222222222222222)
makeBpCl v = asVector64 $ BS.toByteString $ DVS.constructN (DVS.length v) go
where go :: DVS.Vector Word16 -> Word16
go u = let ui = DVS.length u in fromIntegral (pext (DVS.unsafeIndex v ui) 0x1111111111111111)
makeBpOp v = asVector64 $ BS.toByteString $ DVS.generate (DVS.length v) go
where go :: Int -> Word16
go uj = let ui = fromIntegral uj in fromIntegral (pext (DVS.unsafeIndex v ui) 0x2222222222222222)
makeBpCl v = asVector64 $ BS.toByteString $ DVS.generate (DVS.length v) go
where go :: Int -> Word16
go uj = let ui = fromIntegral uj in fromIntegral (pext (DVS.unsafeIndex v ui) 0x1111111111111111)

toIbBpBuilders :: [PreSiChunk (DVS.Vector Word64)] -> [SiChunk (DVS.Vector Word64)]
toIbBpBuilders = go 0 0
Expand Down

0 comments on commit 0e06fd3

Please sign in to comment.