{-# LANGUAGE BangPatterns, CPP, MagicHash, UnboxedTuples,
GeneralizedNewtypeDeriving #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
module FastString
(
bytesFS,
fastStringToByteString,
mkFastStringByteString,
fastZStringToByteString,
unsafeMkByteString,
FastZString,
hPutFZS,
zString,
lengthFZS,
FastString(..),
fsLit,
mkFastString,
mkFastStringBytes,
mkFastStringByteList,
mkFastStringForeignPtr,
mkFastString#,
unpackFS,
zEncodeFS,
uniqueOfFS,
lengthFS,
nullFS,
appendFS,
headFS,
tailFS,
concatFS,
consFS,
nilFS,
isUnderscoreFS,
hPutFS,
getFastStringTable,
getFastStringZEncCounter,
PtrString (..),
sLit,
mkPtrString#,
mkPtrString,
unpackPtrString,
lengthPS
) where
#include "GhclibHsVersions.h"
import GhcPrelude as Prelude
import Encoding
import FastFunctions
import PlainPanic
import Util
import Control.Concurrent.MVar
import Control.DeepSeq
import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC
import qualified Data.ByteString.Internal as BS
import qualified Data.ByteString.Unsafe as BS
import Foreign.C
import GHC.Exts
import System.IO
import Data.Data
import Data.IORef
import Data.Char
import Data.Semigroup as Semi
import GHC.IO
import Foreign
#if GHC_STAGE >= 2
import GHC.Conc.Sync (sharedCAF)
#endif
import GHC.Base ( unpackCString#, unpackNBytes# )
bytesFS :: FastString -> ByteString
bytesFS :: FastString -> ByteString
bytesFS FastString
f = FastString -> ByteString
fs_bs FastString
f
{-# DEPRECATED fastStringToByteString "Use `bytesFS` instead" #-}
fastStringToByteString :: FastString -> ByteString
fastStringToByteString :: FastString -> ByteString
fastStringToByteString = FastString -> ByteString
bytesFS
fastZStringToByteString :: FastZString -> ByteString
fastZStringToByteString :: FastZString -> ByteString
fastZStringToByteString (FastZString ByteString
bs) = ByteString
bs
unsafeMkByteString :: String -> ByteString
unsafeMkByteString :: String -> ByteString
unsafeMkByteString = String -> ByteString
BSC.pack
hashFastString :: FastString -> Int
hashFastString :: FastString -> Int
hashFastString (FastString Int
_ Int
_ ByteString
bs FastZString
_)
= IO Int -> Int
forall a. IO a -> a
inlinePerformIO (IO Int -> Int) -> IO Int -> Int
forall a b. (a -> b) -> a -> b
$ ByteString -> (CStringLen -> IO Int) -> IO Int
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.unsafeUseAsCStringLen ByteString
bs ((CStringLen -> IO Int) -> IO Int)
-> (CStringLen -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ptr, Int
len) ->
Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> IO Int) -> Int -> IO Int
forall a b. (a -> b) -> a -> b
$ Ptr Word8 -> Int -> Int
hashStr (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
ptr) Int
len
newtype FastZString = FastZString ByteString
deriving FastZString -> ()
(FastZString -> ()) -> NFData FastZString
forall a. (a -> ()) -> NFData a
rnf :: FastZString -> ()
$crnf :: FastZString -> ()
NFData
hPutFZS :: Handle -> FastZString -> IO ()
hPutFZS :: Handle -> FastZString -> IO ()
hPutFZS Handle
handle (FastZString ByteString
bs) = Handle -> ByteString -> IO ()
BS.hPut Handle
handle ByteString
bs
zString :: FastZString -> String
zString :: FastZString -> String
zString (FastZString ByteString
bs) =
IO String -> String
forall a. IO a -> a
inlinePerformIO (IO String -> String) -> IO String -> String
forall a b. (a -> b) -> a -> b
$ ByteString -> (CStringLen -> IO String) -> IO String
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.unsafeUseAsCStringLen ByteString
bs CStringLen -> IO String
peekCAStringLen
lengthFZS :: FastZString -> Int
lengthFZS :: FastZString -> Int
lengthFZS (FastZString ByteString
bs) = ByteString -> Int
BS.length ByteString
bs
mkFastZStringString :: String -> FastZString
mkFastZStringString :: String -> FastZString
mkFastZStringString String
str = ByteString -> FastZString
FastZString (String -> ByteString
BSC.pack String
str)
data FastString = FastString {
FastString -> Int
uniq :: {-# UNPACK #-} !Int,
FastString -> Int
n_chars :: {-# UNPACK #-} !Int,
FastString -> ByteString
fs_bs :: {-# UNPACK #-} !ByteString,
FastString -> FastZString
fs_zenc :: FastZString
}
instance Eq FastString where
FastString
f1 == :: FastString -> FastString -> Bool
== FastString
f2 = FastString -> Int
uniq FastString
f1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== FastString -> Int
uniq FastString
f2
instance Ord FastString where
FastString
a <= :: FastString -> FastString -> Bool
<= FastString
b = case FastString -> FastString -> Ordering
cmpFS FastString
a FastString
b of { Ordering
LT -> Bool
True; Ordering
EQ -> Bool
True; Ordering
GT -> Bool
False }
FastString
a < :: FastString -> FastString -> Bool
< FastString
b = case FastString -> FastString -> Ordering
cmpFS FastString
a FastString
b of { Ordering
LT -> Bool
True; Ordering
EQ -> Bool
False; Ordering
GT -> Bool
False }
FastString
a >= :: FastString -> FastString -> Bool
>= FastString
b = case FastString -> FastString -> Ordering
cmpFS FastString
a FastString
b of { Ordering
LT -> Bool
False; Ordering
EQ -> Bool
True; Ordering
GT -> Bool
True }
FastString
a > :: FastString -> FastString -> Bool
> FastString
b = case FastString -> FastString -> Ordering
cmpFS FastString
a FastString
b of { Ordering
LT -> Bool
False; Ordering
EQ -> Bool
False; Ordering
GT -> Bool
True }
max :: FastString -> FastString -> FastString
max FastString
x FastString
y | FastString
x FastString -> FastString -> Bool
forall a. Ord a => a -> a -> Bool
>= FastString
y = FastString
x
| Bool
otherwise = FastString
y
min :: FastString -> FastString -> FastString
min FastString
x FastString
y | FastString
x FastString -> FastString -> Bool
forall a. Ord a => a -> a -> Bool
<= FastString
y = FastString
x
| Bool
otherwise = FastString
y
compare :: FastString -> FastString -> Ordering
compare FastString
a FastString
b = FastString -> FastString -> Ordering
cmpFS FastString
a FastString
b
instance IsString FastString where
fromString :: String -> FastString
fromString = String -> FastString
fsLit
instance Semi.Semigroup FastString where
<> :: FastString -> FastString -> FastString
(<>) = FastString -> FastString -> FastString
appendFS
instance Monoid FastString where
mempty :: FastString
mempty = FastString
nilFS
mappend :: FastString -> FastString -> FastString
mappend = FastString -> FastString -> FastString
forall a. Semigroup a => a -> a -> a
(Semi.<>)
mconcat :: [FastString] -> FastString
mconcat = [FastString] -> FastString
concatFS
instance Show FastString where
show :: FastString -> String
show FastString
fs = ShowS
forall a. Show a => a -> String
show (FastString -> String
unpackFS FastString
fs)
instance Data FastString where
toConstr :: FastString -> Constr
toConstr FastString
_ = String -> Constr
abstractConstr String
"FastString"
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FastString
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_ = String -> Constr -> c FastString
forall a. HasCallStack => String -> a
error String
"gunfold"
dataTypeOf :: FastString -> DataType
dataTypeOf FastString
_ = String -> DataType
mkNoRepType String
"FastString"
instance NFData FastString where
rnf :: FastString -> ()
rnf FastString
fs = FastString -> () -> ()
seq FastString
fs ()
cmpFS :: FastString -> FastString -> Ordering
cmpFS :: FastString -> FastString -> Ordering
cmpFS f1 :: FastString
f1@(FastString Int
u1 Int
_ ByteString
_ FastZString
_) f2 :: FastString
f2@(FastString Int
u2 Int
_ ByteString
_ FastZString
_) =
if Int
u1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
u2 then Ordering
EQ else
ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (FastString -> ByteString
bytesFS FastString
f1) (FastString -> ByteString
bytesFS FastString
f2)
foreign import ccall unsafe "memcmp"
memcmp :: Ptr a -> Ptr b -> CSize -> IO CInt
data FastStringTable = FastStringTable
{-# UNPACK #-} !(IORef Int)
{-# UNPACK #-} !(IORef Int)
(Array# (IORef FastStringTableSegment))
data FastStringTableSegment = FastStringTableSegment
{-# UNPACK #-} !(MVar ())
{-# UNPACK #-} !(IORef Int)
(MutableArray# RealWorld [FastString])
segmentBits, numSegments, segmentMask, initialNumBuckets :: Int
segmentBits :: Int
segmentBits = Int
8
numSegments :: Int
numSegments = Int
256
segmentMask :: Int
segmentMask = Int
0xff
initialNumBuckets :: Int
initialNumBuckets = Int
64
hashToSegment# :: Int# -> Int#
hashToSegment# :: Int# -> Int#
hashToSegment# Int#
hash# = Int#
hash# Int# -> Int# -> Int#
`andI#` Int#
segmentMask#
where
!(I# Int#
segmentMask#) = Int
segmentMask
hashToIndex# :: MutableArray# RealWorld [FastString] -> Int# -> Int#
hashToIndex# :: MutableArray# RealWorld [FastString] -> Int# -> Int#
hashToIndex# MutableArray# RealWorld [FastString]
buckets# Int#
hash# =
(Int#
hash# Int# -> Int# -> Int#
`uncheckedIShiftRL#` Int#
segmentBits#) Int# -> Int# -> Int#
`remInt#` Int#
size#
where
!(I# Int#
segmentBits#) = Int
segmentBits
size# :: Int#
size# = MutableArray# RealWorld [FastString] -> Int#
forall d a. MutableArray# d a -> Int#
sizeofMutableArray# MutableArray# RealWorld [FastString]
buckets#
maybeResizeSegment :: IORef FastStringTableSegment -> IO FastStringTableSegment
maybeResizeSegment :: IORef FastStringTableSegment -> IO FastStringTableSegment
maybeResizeSegment IORef FastStringTableSegment
segmentRef = do
segment :: FastStringTableSegment
segment@(FastStringTableSegment MVar ()
lock IORef Int
counter MutableArray# RealWorld [FastString]
old#) <- IORef FastStringTableSegment -> IO FastStringTableSegment
forall a. IORef a -> IO a
readIORef IORef FastStringTableSegment
segmentRef
let oldSize# :: Int#
oldSize# = MutableArray# RealWorld [FastString] -> Int#
forall d a. MutableArray# d a -> Int#
sizeofMutableArray# MutableArray# RealWorld [FastString]
old#
newSize# :: Int#
newSize# = Int#
oldSize# Int# -> Int# -> Int#
*# Int#
2#
(I# Int#
n#) <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef IORef Int
counter
if Int# -> Bool
isTrue# (Int#
n# Int# -> Int# -> Int#
<# Int#
newSize#)
then FastStringTableSegment -> IO FastStringTableSegment
forall (m :: * -> *) a. Monad m => a -> m a
return FastStringTableSegment
segment
else do
resizedSegment :: FastStringTableSegment
resizedSegment@(FastStringTableSegment MVar ()
_ IORef Int
_ MutableArray# RealWorld [FastString]
new#) <- (State# RealWorld
-> (# State# RealWorld, FastStringTableSegment #))
-> IO FastStringTableSegment
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld
-> (# State# RealWorld, FastStringTableSegment #))
-> IO FastStringTableSegment)
-> (State# RealWorld
-> (# State# RealWorld, FastStringTableSegment #))
-> IO FastStringTableSegment
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s1# ->
case Int#
-> [FastString]
-> State# RealWorld
-> (# State# RealWorld, MutableArray# RealWorld [FastString] #)
forall a d.
Int# -> a -> State# d -> (# State# d, MutableArray# d a #)
newArray# Int#
newSize# [] State# RealWorld
s1# of
(# State# RealWorld
s2#, MutableArray# RealWorld [FastString]
arr# #) -> (# State# RealWorld
s2#, MVar ()
-> IORef Int
-> MutableArray# RealWorld [FastString]
-> FastStringTableSegment
FastStringTableSegment MVar ()
lock IORef Int
counter MutableArray# RealWorld [FastString]
arr# #)
[Int] -> (Int -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. (Int# -> Int
I# Int#
oldSize#) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> IO ()) -> IO ()) -> (Int -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(I# Int#
i#) -> do
[FastString]
fsList <- (State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString]
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString])
-> (State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString]
forall a b. (a -> b) -> a -> b
$ MutableArray# RealWorld [FastString]
-> Int# -> State# RealWorld -> (# State# RealWorld, [FastString] #)
forall d a.
MutableArray# d a -> Int# -> State# d -> (# State# d, a #)
readArray# MutableArray# RealWorld [FastString]
old# Int#
i#
[FastString] -> (FastString -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [FastString]
fsList ((FastString -> IO ()) -> IO ()) -> (FastString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \FastString
fs -> do
let
!(I# Int#
hash#) = FastString -> Int
hashFastString FastString
fs
idx# :: Int#
idx# = MutableArray# RealWorld [FastString] -> Int# -> Int#
hashToIndex# MutableArray# RealWorld [FastString]
new# Int#
hash#
(State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s1# ->
case MutableArray# RealWorld [FastString]
-> Int# -> State# RealWorld -> (# State# RealWorld, [FastString] #)
forall d a.
MutableArray# d a -> Int# -> State# d -> (# State# d, a #)
readArray# MutableArray# RealWorld [FastString]
new# Int#
idx# State# RealWorld
s1# of
(# State# RealWorld
s2#, [FastString]
bucket #) -> case MutableArray# RealWorld [FastString]
-> Int# -> [FastString] -> State# RealWorld -> State# RealWorld
forall d a. MutableArray# d a -> Int# -> a -> State# d -> State# d
writeArray# MutableArray# RealWorld [FastString]
new# Int#
idx# (FastString
fsFastString -> [FastString] -> [FastString]
forall a. a -> [a] -> [a]
: [FastString]
bucket) State# RealWorld
s2# of
State# RealWorld
s3# -> (# State# RealWorld
s3#, () #)
IORef FastStringTableSegment -> FastStringTableSegment -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef FastStringTableSegment
segmentRef FastStringTableSegment
resizedSegment
FastStringTableSegment -> IO FastStringTableSegment
forall (m :: * -> *) a. Monad m => a -> m a
return FastStringTableSegment
resizedSegment
{-# NOINLINE stringTable #-}
stringTable :: FastStringTable
stringTable :: FastStringTable
stringTable = IO FastStringTable -> FastStringTable
forall a. IO a -> a
unsafePerformIO (IO FastStringTable -> FastStringTable)
-> IO FastStringTable -> FastStringTable
forall a b. (a -> b) -> a -> b
$ do
let !(I# Int#
numSegments#) = Int
numSegments
!(I# Int#
initialNumBuckets#) = Int
initialNumBuckets
loop :: MutableArray# RealWorld (IORef FastStringTableSegment)
-> Int# -> State# RealWorld -> State# RealWorld
loop MutableArray# RealWorld (IORef FastStringTableSegment)
a# Int#
i# State# RealWorld
s1#
| Int# -> Bool
isTrue# (Int#
i# Int# -> Int# -> Int#
==# Int#
numSegments#) = State# RealWorld
s1#
| Bool
otherwise = case () -> IO (MVar ())
forall a. a -> IO (MVar a)
newMVar () IO (MVar ()) -> State# RealWorld -> (# State# RealWorld, MVar () #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
`unIO` State# RealWorld
s1# of
(# State# RealWorld
s2#, MVar ()
lock #) -> case Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0 IO (IORef Int)
-> State# RealWorld -> (# State# RealWorld, IORef Int #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
`unIO` State# RealWorld
s2# of
(# State# RealWorld
s3#, IORef Int
counter #) -> case Int#
-> [FastString]
-> State# RealWorld
-> (# State# RealWorld, MutableArray# RealWorld [FastString] #)
forall a d.
Int# -> a -> State# d -> (# State# d, MutableArray# d a #)
newArray# Int#
initialNumBuckets# [] State# RealWorld
s3# of
(# State# RealWorld
s4#, MutableArray# RealWorld [FastString]
buckets# #) -> case FastStringTableSegment -> IO (IORef FastStringTableSegment)
forall a. a -> IO (IORef a)
newIORef
(MVar ()
-> IORef Int
-> MutableArray# RealWorld [FastString]
-> FastStringTableSegment
FastStringTableSegment MVar ()
lock IORef Int
counter MutableArray# RealWorld [FastString]
buckets#) IO (IORef FastStringTableSegment)
-> State# RealWorld
-> (# State# RealWorld, IORef FastStringTableSegment #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
`unIO` State# RealWorld
s4# of
(# State# RealWorld
s5#, IORef FastStringTableSegment
segment #) -> case MutableArray# RealWorld (IORef FastStringTableSegment)
-> Int#
-> IORef FastStringTableSegment
-> State# RealWorld
-> State# RealWorld
forall d a. MutableArray# d a -> Int# -> a -> State# d -> State# d
writeArray# MutableArray# RealWorld (IORef FastStringTableSegment)
a# Int#
i# IORef FastStringTableSegment
segment State# RealWorld
s5# of
State# RealWorld
s6# -> MutableArray# RealWorld (IORef FastStringTableSegment)
-> Int# -> State# RealWorld -> State# RealWorld
loop MutableArray# RealWorld (IORef FastStringTableSegment)
a# (Int#
i# Int# -> Int# -> Int#
+# Int#
1#) State# RealWorld
s6#
IORef Int
uid <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
603979776
IORef Int
n_zencs <- Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
FastStringTable
tab <- (State# RealWorld -> (# State# RealWorld, FastStringTable #))
-> IO FastStringTable
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, FastStringTable #))
-> IO FastStringTable)
-> (State# RealWorld -> (# State# RealWorld, FastStringTable #))
-> IO FastStringTable
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s1# ->
case Int#
-> IORef FastStringTableSegment
-> State# RealWorld
-> (# State# RealWorld,
MutableArray# RealWorld (IORef FastStringTableSegment) #)
forall a d.
Int# -> a -> State# d -> (# State# d, MutableArray# d a #)
newArray# Int#
numSegments# (String -> IORef FastStringTableSegment
forall a. String -> a
panic String
"string_table") State# RealWorld
s1# of
(# State# RealWorld
s2#, MutableArray# RealWorld (IORef FastStringTableSegment)
arr# #) -> case MutableArray# RealWorld (IORef FastStringTableSegment)
-> Int# -> State# RealWorld -> State# RealWorld
loop MutableArray# RealWorld (IORef FastStringTableSegment)
arr# Int#
0# State# RealWorld
s2# of
State# RealWorld
s3# -> case MutableArray# RealWorld (IORef FastStringTableSegment)
-> State# RealWorld
-> (# State# RealWorld, Array# (IORef FastStringTableSegment) #)
forall d a.
MutableArray# d a -> State# d -> (# State# d, Array# a #)
unsafeFreezeArray# MutableArray# RealWorld (IORef FastStringTableSegment)
arr# State# RealWorld
s3# of
(# State# RealWorld
s4#, Array# (IORef FastStringTableSegment)
segments# #) ->
(# State# RealWorld
s4#, IORef Int
-> IORef Int
-> Array# (IORef FastStringTableSegment)
-> FastStringTable
FastStringTable IORef Int
uid IORef Int
n_zencs Array# (IORef FastStringTableSegment)
segments# #)
#if GHC_STAGE < 2
FastStringTable -> IO FastStringTable
forall (m :: * -> *) a. Monad m => a -> m a
return FastStringTable
tab
#else
sharedCAF tab getOrSetLibHSghcFastStringTable
foreign import ccall unsafe "getOrSetLibHSghcFastStringTable"
getOrSetLibHSghcFastStringTable :: Ptr a -> IO (Ptr a)
#endif
mkFastString# :: Addr# -> FastString
mkFastString# :: Addr# -> FastString
mkFastString# Addr#
a# = Ptr Word8 -> Int -> FastString
mkFastStringBytes Ptr Word8
forall a. Ptr a
ptr (CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Ptr Word8 -> CSize
ptrStrLength Ptr Word8
forall a. Ptr a
ptr))
where ptr :: Ptr a
ptr = Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
a#
mkFastStringWith
:: (Int -> IORef Int-> IO FastString) -> Ptr Word8 -> Int -> IO FastString
mkFastStringWith :: (Int -> IORef Int -> IO FastString)
-> Ptr Word8 -> Int -> IO FastString
mkFastStringWith Int -> IORef Int -> IO FastString
mk_fs !Ptr Word8
ptr !Int
len = do
FastStringTableSegment MVar ()
lock IORef Int
_ MutableArray# RealWorld [FastString]
buckets# <- IORef FastStringTableSegment -> IO FastStringTableSegment
forall a. IORef a -> IO a
readIORef IORef FastStringTableSegment
segmentRef
let idx# :: Int#
idx# = MutableArray# RealWorld [FastString] -> Int# -> Int#
hashToIndex# MutableArray# RealWorld [FastString]
buckets# Int#
hash#
[FastString]
bucket <- (State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString]
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString])
-> (State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString]
forall a b. (a -> b) -> a -> b
$ MutableArray# RealWorld [FastString]
-> Int# -> State# RealWorld -> (# State# RealWorld, [FastString] #)
forall d a.
MutableArray# d a -> Int# -> State# d -> (# State# d, a #)
readArray# MutableArray# RealWorld [FastString]
buckets# Int#
idx#
Maybe FastString
res <- [FastString] -> Int -> Ptr Word8 -> IO (Maybe FastString)
bucket_match [FastString]
bucket Int
len Ptr Word8
ptr
case Maybe FastString
res of
Just FastString
found -> FastString -> IO FastString
forall (m :: * -> *) a. Monad m => a -> m a
return FastString
found
Maybe FastString
Nothing -> do
IO ()
noDuplicate
Int
n <- IO Int
get_uid
FastString
new_fs <- Int -> IORef Int -> IO FastString
mk_fs Int
n IORef Int
n_zencs
MVar () -> (() -> IO FastString) -> IO FastString
forall a b. MVar a -> (a -> IO b) -> IO b
withMVar MVar ()
lock ((() -> IO FastString) -> IO FastString)
-> (() -> IO FastString) -> IO FastString
forall a b. (a -> b) -> a -> b
$ \()
_ -> FastString -> IO FastString
insert FastString
new_fs
where
!(FastStringTable IORef Int
uid IORef Int
n_zencs Array# (IORef FastStringTableSegment)
segments#) = FastStringTable
stringTable
get_uid :: IO Int
get_uid = IORef Int -> (Int -> (Int, Int)) -> IO Int
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef Int
uid ((Int -> (Int, Int)) -> IO Int) -> (Int -> (Int, Int)) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Int
n -> (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1,Int
n)
!(I# Int#
hash#) = Ptr Word8 -> Int -> Int
hashStr Ptr Word8
ptr Int
len
(# IORef FastStringTableSegment
segmentRef #) = Array# (IORef FastStringTableSegment)
-> Int# -> (# IORef FastStringTableSegment #)
forall a. Array# a -> Int# -> (# a #)
indexArray# Array# (IORef FastStringTableSegment)
segments# (Int# -> Int#
hashToSegment# Int#
hash#)
insert :: FastString -> IO FastString
insert FastString
fs = do
FastStringTableSegment MVar ()
_ IORef Int
counter MutableArray# RealWorld [FastString]
buckets# <- IORef FastStringTableSegment -> IO FastStringTableSegment
maybeResizeSegment IORef FastStringTableSegment
segmentRef
let idx# :: Int#
idx# = MutableArray# RealWorld [FastString] -> Int# -> Int#
hashToIndex# MutableArray# RealWorld [FastString]
buckets# Int#
hash#
[FastString]
bucket <- (State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString]
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString])
-> (State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString]
forall a b. (a -> b) -> a -> b
$ MutableArray# RealWorld [FastString]
-> Int# -> State# RealWorld -> (# State# RealWorld, [FastString] #)
forall d a.
MutableArray# d a -> Int# -> State# d -> (# State# d, a #)
readArray# MutableArray# RealWorld [FastString]
buckets# Int#
idx#
Maybe FastString
res <- [FastString] -> Int -> Ptr Word8 -> IO (Maybe FastString)
bucket_match [FastString]
bucket Int
len Ptr Word8
ptr
case Maybe FastString
res of
Just FastString
found -> FastString -> IO FastString
forall (m :: * -> *) a. Monad m => a -> m a
return FastString
found
Maybe FastString
Nothing -> do
(State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s1# ->
case MutableArray# RealWorld [FastString]
-> Int# -> [FastString] -> State# RealWorld -> State# RealWorld
forall d a. MutableArray# d a -> Int# -> a -> State# d -> State# d
writeArray# MutableArray# RealWorld [FastString]
buckets# Int#
idx# (FastString
fsFastString -> [FastString] -> [FastString]
forall a. a -> [a] -> [a]
: [FastString]
bucket) State# RealWorld
s1# of
State# RealWorld
s2# -> (# State# RealWorld
s2#, () #)
IORef Int -> (Int -> Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' IORef Int
counter Int -> Int
forall a. Enum a => a -> a
succ
FastString -> IO FastString
forall (m :: * -> *) a. Monad m => a -> m a
return FastString
fs
bucket_match :: [FastString] -> Int -> Ptr Word8 -> IO (Maybe FastString)
bucket_match :: [FastString] -> Int -> Ptr Word8 -> IO (Maybe FastString)
bucket_match [] Int
_ Ptr Word8
_ = Maybe FastString -> IO (Maybe FastString)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FastString
forall a. Maybe a
Nothing
bucket_match (v :: FastString
v@(FastString Int
_ Int
_ ByteString
bs FastZString
_):[FastString]
ls) Int
len Ptr Word8
ptr
| Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
BS.length ByteString
bs = do
Bool
b <- ByteString -> (Ptr CChar -> IO Bool) -> IO Bool
forall a. ByteString -> (Ptr CChar -> IO a) -> IO a
BS.unsafeUseAsCString ByteString
bs ((Ptr CChar -> IO Bool) -> IO Bool)
-> (Ptr CChar -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \Ptr CChar
buf ->
Ptr Word8 -> Ptr Word8 -> Int -> IO Bool
cmpStringPrefix Ptr Word8
ptr (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
buf) Int
len
if Bool
b then Maybe FastString -> IO (Maybe FastString)
forall (m :: * -> *) a. Monad m => a -> m a
return (FastString -> Maybe FastString
forall a. a -> Maybe a
Just FastString
v)
else [FastString] -> Int -> Ptr Word8 -> IO (Maybe FastString)
bucket_match [FastString]
ls Int
len Ptr Word8
ptr
| Bool
otherwise =
[FastString] -> Int -> Ptr Word8 -> IO (Maybe FastString)
bucket_match [FastString]
ls Int
len Ptr Word8
ptr
mkFastStringBytes :: Ptr Word8 -> Int -> FastString
mkFastStringBytes :: Ptr Word8 -> Int -> FastString
mkFastStringBytes !Ptr Word8
ptr !Int
len =
IO FastString -> FastString
forall a. IO a -> a
unsafeDupablePerformIO (IO FastString -> FastString) -> IO FastString -> FastString
forall a b. (a -> b) -> a -> b
$
(Int -> IORef Int -> IO FastString)
-> Ptr Word8 -> Int -> IO FastString
mkFastStringWith (Ptr Word8 -> Int -> Int -> IORef Int -> IO FastString
copyNewFastString Ptr Word8
ptr Int
len) Ptr Word8
ptr Int
len
mkFastStringForeignPtr :: Ptr Word8 -> ForeignPtr Word8 -> Int -> IO FastString
mkFastStringForeignPtr :: Ptr Word8 -> ForeignPtr Word8 -> Int -> IO FastString
mkFastStringForeignPtr Ptr Word8
ptr !ForeignPtr Word8
fp Int
len
= (Int -> IORef Int -> IO FastString)
-> Ptr Word8 -> Int -> IO FastString
mkFastStringWith (ForeignPtr Word8
-> Ptr Word8 -> Int -> Int -> IORef Int -> IO FastString
mkNewFastString ForeignPtr Word8
fp Ptr Word8
ptr Int
len) Ptr Word8
ptr Int
len
mkFastStringByteString :: ByteString -> FastString
mkFastStringByteString :: ByteString -> FastString
mkFastStringByteString ByteString
bs =
IO FastString -> FastString
forall a. IO a -> a
inlinePerformIO (IO FastString -> FastString) -> IO FastString -> FastString
forall a b. (a -> b) -> a -> b
$
ByteString -> (CStringLen -> IO FastString) -> IO FastString
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.unsafeUseAsCStringLen ByteString
bs ((CStringLen -> IO FastString) -> IO FastString)
-> (CStringLen -> IO FastString) -> IO FastString
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
ptr, Int
len) -> do
let ptr' :: Ptr b
ptr' = Ptr CChar -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
ptr
(Int -> IORef Int -> IO FastString)
-> Ptr Word8 -> Int -> IO FastString
mkFastStringWith (ByteString -> Ptr Word8 -> Int -> Int -> IORef Int -> IO FastString
mkNewFastStringByteString ByteString
bs Ptr Word8
forall a. Ptr a
ptr' Int
len) Ptr Word8
forall a. Ptr a
ptr' Int
len
mkFastString :: String -> FastString
mkFastString :: String -> FastString
mkFastString String
str =
IO FastString -> FastString
forall a. IO a -> a
inlinePerformIO (IO FastString -> FastString) -> IO FastString -> FastString
forall a b. (a -> b) -> a -> b
$ do
let l :: Int
l = String -> Int
utf8EncodedLength String
str
ForeignPtr Word8
buf <- Int -> IO (ForeignPtr Word8)
forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes Int
l
ForeignPtr Word8 -> (Ptr Word8 -> IO FastString) -> IO FastString
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
buf ((Ptr Word8 -> IO FastString) -> IO FastString)
-> (Ptr Word8 -> IO FastString) -> IO FastString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr -> do
Ptr Word8 -> String -> IO ()
utf8EncodeString Ptr Word8
ptr String
str
Ptr Word8 -> ForeignPtr Word8 -> Int -> IO FastString
mkFastStringForeignPtr Ptr Word8
ptr ForeignPtr Word8
buf Int
l
mkFastStringByteList :: [Word8] -> FastString
mkFastStringByteList :: [Word8] -> FastString
mkFastStringByteList [Word8]
str = ByteString -> FastString
mkFastStringByteString ([Word8] -> ByteString
BS.pack [Word8]
str)
mkZFastString :: IORef Int -> ByteString -> FastZString
mkZFastString :: IORef Int -> ByteString -> FastZString
mkZFastString IORef Int
n_zencs ByteString
bs = IO FastZString -> FastZString
forall a. IO a -> a
unsafePerformIO (IO FastZString -> FastZString) -> IO FastZString -> FastZString
forall a b. (a -> b) -> a -> b
$ do
IORef Int -> (Int -> (Int, ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef Int
n_zencs ((Int -> (Int, ())) -> IO ()) -> (Int -> (Int, ())) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Int
n -> (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1, ())
FastZString -> IO FastZString
forall (m :: * -> *) a. Monad m => a -> m a
return (FastZString -> IO FastZString) -> FastZString -> IO FastZString
forall a b. (a -> b) -> a -> b
$ String -> FastZString
mkFastZStringString (ShowS
zEncodeString (ByteString -> String
utf8DecodeByteString ByteString
bs))
mkNewFastString :: ForeignPtr Word8 -> Ptr Word8 -> Int -> Int
-> IORef Int -> IO FastString
mkNewFastString :: ForeignPtr Word8
-> Ptr Word8 -> Int -> Int -> IORef Int -> IO FastString
mkNewFastString ForeignPtr Word8
fp Ptr Word8
ptr Int
len Int
uid IORef Int
n_zencs = do
let bs :: ByteString
bs = ForeignPtr Word8 -> Int -> Int -> ByteString
BS.fromForeignPtr ForeignPtr Word8
fp Int
0 Int
len
zstr :: FastZString
zstr = IORef Int -> ByteString -> FastZString
mkZFastString IORef Int
n_zencs ByteString
bs
Int
n_chars <- Ptr Word8 -> Int -> IO Int
countUTF8Chars Ptr Word8
ptr Int
len
FastString -> IO FastString
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> ByteString -> FastZString -> FastString
FastString Int
uid Int
n_chars ByteString
bs FastZString
zstr)
mkNewFastStringByteString :: ByteString -> Ptr Word8 -> Int -> Int
-> IORef Int -> IO FastString
mkNewFastStringByteString :: ByteString -> Ptr Word8 -> Int -> Int -> IORef Int -> IO FastString
mkNewFastStringByteString ByteString
bs Ptr Word8
ptr Int
len Int
uid IORef Int
n_zencs = do
let zstr :: FastZString
zstr = IORef Int -> ByteString -> FastZString
mkZFastString IORef Int
n_zencs ByteString
bs
Int
n_chars <- Ptr Word8 -> Int -> IO Int
countUTF8Chars Ptr Word8
ptr Int
len
FastString -> IO FastString
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> ByteString -> FastZString -> FastString
FastString Int
uid Int
n_chars ByteString
bs FastZString
zstr)
copyNewFastString :: Ptr Word8 -> Int -> Int -> IORef Int -> IO FastString
copyNewFastString :: Ptr Word8 -> Int -> Int -> IORef Int -> IO FastString
copyNewFastString Ptr Word8
ptr Int
len Int
uid IORef Int
n_zencs = do
ForeignPtr Word8
fp <- Ptr Word8 -> Int -> IO (ForeignPtr Word8)
copyBytesToForeignPtr Ptr Word8
ptr Int
len
let bs :: ByteString
bs = ForeignPtr Word8 -> Int -> Int -> ByteString
BS.fromForeignPtr ForeignPtr Word8
fp Int
0 Int
len
zstr :: FastZString
zstr = IORef Int -> ByteString -> FastZString
mkZFastString IORef Int
n_zencs ByteString
bs
Int
n_chars <- Ptr Word8 -> Int -> IO Int
countUTF8Chars Ptr Word8
ptr Int
len
FastString -> IO FastString
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> ByteString -> FastZString -> FastString
FastString Int
uid Int
n_chars ByteString
bs FastZString
zstr)
copyBytesToForeignPtr :: Ptr Word8 -> Int -> IO (ForeignPtr Word8)
copyBytesToForeignPtr :: Ptr Word8 -> Int -> IO (ForeignPtr Word8)
copyBytesToForeignPtr Ptr Word8
ptr Int
len = do
ForeignPtr Word8
fp <- Int -> IO (ForeignPtr Word8)
forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes Int
len
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr' -> Ptr Word8 -> Ptr Word8 -> Int -> IO ()
forall a. Ptr a -> Ptr a -> Int -> IO ()
copyBytes Ptr Word8
ptr' Ptr Word8
ptr Int
len
ForeignPtr Word8 -> IO (ForeignPtr Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return ForeignPtr Word8
fp
cmpStringPrefix :: Ptr Word8 -> Ptr Word8 -> Int -> IO Bool
cmpStringPrefix :: Ptr Word8 -> Ptr Word8 -> Int -> IO Bool
cmpStringPrefix Ptr Word8
ptr1 Ptr Word8
ptr2 Int
len =
do CInt
r <- Ptr Word8 -> Ptr Word8 -> CSize -> IO CInt
forall a b. Ptr a -> Ptr b -> CSize -> IO CInt
memcmp Ptr Word8
ptr1 Ptr Word8
ptr2 (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (CInt
r CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0)
hashStr :: Ptr Word8 -> Int -> Int
hashStr :: Ptr Word8 -> Int -> Int
hashStr (Ptr Addr#
a#) (I# Int#
len#) = Int# -> Int# -> Int
loop Int#
0# Int#
0#
where
loop :: Int# -> Int# -> Int
loop Int#
h Int#
n =
if Int# -> Bool
isTrue# (Int#
n Int# -> Int# -> Int#
==# Int#
len#) then
Int# -> Int
I# Int#
h
else
let
!c :: Int#
c = Char# -> Int#
ord# (Addr# -> Int# -> Char#
indexCharOffAddr# Addr#
a# Int#
n)
!h2 :: Int#
h2 = (Int#
h Int# -> Int# -> Int#
*# Int#
16777619#) Int# -> Int# -> Int#
`xorI#` Int#
c
in
Int# -> Int# -> Int
loop Int#
h2 (Int#
n Int# -> Int# -> Int#
+# Int#
1#)
lengthFS :: FastString -> Int
lengthFS :: FastString -> Int
lengthFS FastString
f = FastString -> Int
n_chars FastString
f
nullFS :: FastString -> Bool
nullFS :: FastString -> Bool
nullFS FastString
f = ByteString -> Bool
BS.null (FastString -> ByteString
fs_bs FastString
f)
unpackFS :: FastString -> String
unpackFS :: FastString -> String
unpackFS (FastString Int
_ Int
_ ByteString
bs FastZString
_) = ByteString -> String
utf8DecodeByteString ByteString
bs
zEncodeFS :: FastString -> FastZString
zEncodeFS :: FastString -> FastZString
zEncodeFS (FastString Int
_ Int
_ ByteString
_ FastZString
ref) = FastZString
ref
appendFS :: FastString -> FastString -> FastString
appendFS :: FastString -> FastString -> FastString
appendFS FastString
fs1 FastString
fs2 = ByteString -> FastString
mkFastStringByteString
(ByteString -> FastString) -> ByteString -> FastString
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> ByteString
BS.append (FastString -> ByteString
bytesFS FastString
fs1) (FastString -> ByteString
bytesFS FastString
fs2)
concatFS :: [FastString] -> FastString
concatFS :: [FastString] -> FastString
concatFS = ByteString -> FastString
mkFastStringByteString (ByteString -> FastString)
-> ([FastString] -> ByteString) -> [FastString] -> FastString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> ByteString
BS.concat ([ByteString] -> ByteString)
-> ([FastString] -> [ByteString]) -> [FastString] -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FastString -> ByteString) -> [FastString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map FastString -> ByteString
fs_bs
headFS :: FastString -> Char
headFS :: FastString -> Char
headFS (FastString Int
_ Int
0 ByteString
_ FastZString
_) = String -> Char
forall a. String -> a
panic String
"headFS: Empty FastString"
headFS (FastString Int
_ Int
_ ByteString
bs FastZString
_) =
IO Char -> Char
forall a. IO a -> a
inlinePerformIO (IO Char -> Char) -> IO Char -> Char
forall a b. (a -> b) -> a -> b
$ ByteString -> (Ptr CChar -> IO Char) -> IO Char
forall a. ByteString -> (Ptr CChar -> IO a) -> IO a
BS.unsafeUseAsCString ByteString
bs ((Ptr CChar -> IO Char) -> IO Char)
-> (Ptr CChar -> IO Char) -> IO Char
forall a b. (a -> b) -> a -> b
$ \Ptr CChar
ptr ->
Char -> IO Char
forall (m :: * -> *) a. Monad m => a -> m a
return ((Char, Int) -> Char
forall a b. (a, b) -> a
fst (Ptr Word8 -> (Char, Int)
utf8DecodeChar (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
ptr)))
tailFS :: FastString -> FastString
tailFS :: FastString -> FastString
tailFS (FastString Int
_ Int
0 ByteString
_ FastZString
_) = String -> FastString
forall a. String -> a
panic String
"tailFS: Empty FastString"
tailFS (FastString Int
_ Int
_ ByteString
bs FastZString
_) =
IO FastString -> FastString
forall a. IO a -> a
inlinePerformIO (IO FastString -> FastString) -> IO FastString -> FastString
forall a b. (a -> b) -> a -> b
$ ByteString -> (Ptr CChar -> IO FastString) -> IO FastString
forall a. ByteString -> (Ptr CChar -> IO a) -> IO a
BS.unsafeUseAsCString ByteString
bs ((Ptr CChar -> IO FastString) -> IO FastString)
-> (Ptr CChar -> IO FastString) -> IO FastString
forall a b. (a -> b) -> a -> b
$ \Ptr CChar
ptr ->
do let (Char
_, Int
n) = Ptr Word8 -> (Char, Int)
utf8DecodeChar (Ptr CChar -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr CChar
ptr)
FastString -> IO FastString
forall (m :: * -> *) a. Monad m => a -> m a
return (FastString -> IO FastString) -> FastString -> IO FastString
forall a b. (a -> b) -> a -> b
$! ByteString -> FastString
mkFastStringByteString (Int -> ByteString -> ByteString
BS.drop Int
n ByteString
bs)
consFS :: Char -> FastString -> FastString
consFS :: Char -> FastString -> FastString
consFS Char
c FastString
fs = String -> FastString
mkFastString (Char
c Char -> ShowS
forall a. a -> [a] -> [a]
: FastString -> String
unpackFS FastString
fs)
uniqueOfFS :: FastString -> Int
uniqueOfFS :: FastString -> Int
uniqueOfFS (FastString Int
u Int
_ ByteString
_ FastZString
_) = Int
u
nilFS :: FastString
nilFS :: FastString
nilFS = String -> FastString
mkFastString String
""
isUnderscoreFS :: FastString -> Bool
isUnderscoreFS :: FastString -> Bool
isUnderscoreFS FastString
fs = FastString
fs FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
== String -> FastString
fsLit String
"_"
getFastStringTable :: IO [[[FastString]]]
getFastStringTable :: IO [[[FastString]]]
getFastStringTable =
[Int] -> (Int -> IO [[FastString]]) -> IO [[[FastString]]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0 .. Int
numSegments Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> IO [[FastString]]) -> IO [[[FastString]]])
-> (Int -> IO [[FastString]]) -> IO [[[FastString]]]
forall a b. (a -> b) -> a -> b
$ \(I# Int#
i#) -> do
let (# IORef FastStringTableSegment
segmentRef #) = Array# (IORef FastStringTableSegment)
-> Int# -> (# IORef FastStringTableSegment #)
forall a. Array# a -> Int# -> (# a #)
indexArray# Array# (IORef FastStringTableSegment)
segments# Int#
i#
FastStringTableSegment MVar ()
_ IORef Int
_ MutableArray# RealWorld [FastString]
buckets# <- IORef FastStringTableSegment -> IO FastStringTableSegment
forall a. IORef a -> IO a
readIORef IORef FastStringTableSegment
segmentRef
let bucketSize :: Int
bucketSize = Int# -> Int
I# (MutableArray# RealWorld [FastString] -> Int#
forall d a. MutableArray# d a -> Int#
sizeofMutableArray# MutableArray# RealWorld [FastString]
buckets#)
[Int] -> (Int -> IO [FastString]) -> IO [[FastString]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0 .. Int
bucketSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> IO [FastString]) -> IO [[FastString]])
-> (Int -> IO [FastString]) -> IO [[FastString]]
forall a b. (a -> b) -> a -> b
$ \(I# Int#
j#) ->
(State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString]
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString])
-> (State# RealWorld -> (# State# RealWorld, [FastString] #))
-> IO [FastString]
forall a b. (a -> b) -> a -> b
$ MutableArray# RealWorld [FastString]
-> Int# -> State# RealWorld -> (# State# RealWorld, [FastString] #)
forall d a.
MutableArray# d a -> Int# -> State# d -> (# State# d, a #)
readArray# MutableArray# RealWorld [FastString]
buckets# Int#
j#
where
!(FastStringTable IORef Int
_ IORef Int
_ Array# (IORef FastStringTableSegment)
segments#) = FastStringTable
stringTable
getFastStringZEncCounter :: IO Int
getFastStringZEncCounter :: IO Int
getFastStringZEncCounter = IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef IORef Int
n_zencs
where
!(FastStringTable IORef Int
_ IORef Int
n_zencs Array# (IORef FastStringTableSegment)
_) = FastStringTable
stringTable
hPutFS :: Handle -> FastString -> IO ()
hPutFS :: Handle -> FastString -> IO ()
hPutFS Handle
handle FastString
fs = Handle -> ByteString -> IO ()
BS.hPut Handle
handle (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$ FastString -> ByteString
bytesFS FastString
fs
data PtrString = PtrString !(Ptr Word8) !Int
mkPtrString# :: Addr# -> PtrString
mkPtrString# :: Addr# -> PtrString
mkPtrString# Addr#
a# = Ptr Word8 -> Int -> PtrString
PtrString (Addr# -> Ptr Word8
forall a. Addr# -> Ptr a
Ptr Addr#
a#) (CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Ptr Word8 -> CSize
ptrStrLength (Addr# -> Ptr Word8
forall a. Addr# -> Ptr a
Ptr Addr#
a#)))
{-# INLINE mkPtrString #-}
mkPtrString :: String -> PtrString
mkPtrString :: String -> PtrString
mkPtrString String
s =
IO PtrString -> PtrString
forall a. IO a -> a
unsafePerformIO (do
let len :: Int
len = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s
Ptr Word8
p <- Int -> IO (Ptr Word8)
forall a. Int -> IO (Ptr a)
mallocBytes Int
len
let
loop :: Int -> String -> IO ()
loop :: Int -> String -> IO ()
loop !Int
_ [] = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
loop Int
n (Char
c:String
cs) = do
Ptr Word8 -> Int -> Word8 -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff Ptr Word8
p Int
n (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord Char
c) :: Word8)
Int -> String -> IO ()
loop (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
n) String
cs
Int -> String -> IO ()
loop Int
0 String
s
PtrString -> IO PtrString
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8 -> Int -> PtrString
PtrString Ptr Word8
p Int
len)
)
unpackPtrString :: PtrString -> String
unpackPtrString :: PtrString -> String
unpackPtrString (PtrString (Ptr Addr#
p#) (I# Int#
n#)) = Addr# -> Int# -> String
unpackNBytes# Addr#
p# Int#
n#
lengthPS :: PtrString -> Int
lengthPS :: PtrString -> Int
lengthPS (PtrString Ptr Word8
_ Int
n) = Int
n
foreign import ccall unsafe "strlen"
ptrStrLength :: Ptr Word8 -> CSize
{-# NOINLINE sLit #-}
sLit :: String -> PtrString
sLit :: String -> PtrString
sLit String
x = String -> PtrString
mkPtrString String
x
{-# NOINLINE fsLit #-}
fsLit :: String -> FastString
fsLit :: String -> FastString
fsLit String
x = String -> FastString
mkFastString String
x
{-# RULES "slit"
forall x . sLit (unpackCString# x) = mkPtrString# x #-}
{-# RULES "fslit"
forall x . fsLit (unpackCString# x) = mkFastString# x #-}