Skip to content

Commit dfbcc27

Browse files
authored
Merge pull request #78 from haskell-works/choose-more-efficient-data-structure-for-table-lookup
Choose more efficient data structure for table lookup
2 parents 07e2b41 + 07b852e commit dfbcc27

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/HaskellWorks/Data/Xml/Conduit.hs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,35 @@ module HaskellWorks.Data.Xml.Conduit
1010
, isInterestingWord8
1111
) where
1212

13-
import Data.Array.Unboxed as A
1413
import Data.ByteString as BS
14+
import Data.Vector.Storable ((!))
1515
import Data.Word
1616
import Data.Word8
1717
import HaskellWorks.Data.Bits.BitWise
1818
import Prelude as P
1919

20-
import qualified Data.Bits as BITS
21-
22-
interestingWord8s :: A.UArray Word8 Word8
23-
interestingWord8s = A.array (0, 255) [
24-
(w, if w == _bracketleft
25-
|| w == _braceleft
26-
|| w == _parenleft
27-
|| w == _bracketleft
28-
|| w == _less
29-
|| w == _a || w == _v || w == _t
30-
then 1
31-
else 0)
32-
| w <- [0 .. 255]]
20+
import qualified Data.Bits as BITS
21+
import qualified Data.Vector.Storable as DVS
22+
23+
interestingWord8s :: DVS.Vector Word8
24+
interestingWord8s = DVS.constructN 256 go
25+
where go :: DVS.Vector Word8 -> Word8
26+
go v = if w == _bracketleft
27+
|| w == _braceleft
28+
|| w == _parenleft
29+
|| w == _bracketleft
30+
|| w == _less
31+
|| w == _a
32+
|| w == _v
33+
|| w == _t
34+
then 1
35+
else 0
36+
where w :: Word8
37+
w = fromIntegral (DVS.length v)
3338
{-# NOINLINE interestingWord8s #-}
3439

3540
isInterestingWord8 :: Word8 -> Word8
36-
isInterestingWord8 b = interestingWord8s ! b
41+
isInterestingWord8 b = fromIntegral (interestingWord8s ! fromIntegral b)
3742
{-# INLINABLE isInterestingWord8 #-}
3843

3944
blankedXmlToInterestBits :: [BS.ByteString] -> [BS.ByteString]
@@ -55,7 +60,7 @@ blankedXmlToInterestBits' rs is = case is of
5560
where gen :: ByteString -> Maybe (Word8, ByteString)
5661
gen as = if BS.length as == 0
5762
then Nothing
58-
else Just ( BS.foldr' (\b m -> (interestingWord8s ! b) .|. (m .<. 1)) 0 (BS.take 8 as)
63+
else Just ( BS.foldr' (\b m -> (interestingWord8s ! fromIntegral b) .|. (m .<. 1)) 0 (BS.take 8 as)
5964
, BS.drop 8 as
6065
)
6166

0 commit comments

Comments
 (0)