2 % (c) The GRASP/AQUA Project, Glasgow University, 1997-1998
6 Compact representations of character strings with
7 unique identifiers (hash-cons'ish).
12 FastString(..), -- not abstract, for now.
14 mkFastString, -- :: String -> FastString
15 mkFastStringNarrow, -- :: String -> FastString
16 mkFastSubString, -- :: Addr -> Int -> Int -> FastString
18 mkFastString#, -- :: Addr# -> FastString
19 mkFastSubStringBA#, -- :: ByteArray# -> Int# -> Int# -> FastString
21 mkFastStringInt, -- :: [Int] -> FastString
23 uniqueOfFS, -- :: FastString -> Int#
24 lengthFS, -- :: FastString -> Int
25 nullFastString, -- :: FastString -> Bool
27 unpackFS, -- :: FastString -> String
28 unpackIntFS, -- :: FastString -> [Int]
29 appendFS, -- :: FastString -> FastString -> FastString
30 headFS, -- :: FastString -> Char
31 headIntFS, -- :: FastString -> Int
32 tailFS, -- :: FastString -> FastString
33 concatFS, -- :: [FastString] -> FastString
34 consFS, -- :: Char -> FastString -> FastString
35 indexFS, -- :: FastString -> Int -> Char
36 nilFS, -- :: FastString
38 hPutFS, -- :: Handle -> FastString -> IO ()
41 mkLitString# -- :: Addr# -> Addr
44 -- This #define suppresses the "import FastString" that
45 -- HsVersions otherwise produces
46 #define COMPILING_FAST_STRING
47 #include "HsVersions.h"
49 #if __GLASGOW_HASKELL__ < 503
51 import PrelIOBase ( IO(..) )
54 import GHC.IOBase ( IO(..) )
59 #if __GLASGOW_HASKELL__ < 411
60 import PrelAddr ( Addr(..) )
62 import Addr ( Addr(..) )
64 #if __GLASGOW_HASKELL__ < 503
65 import PrelArr ( STArray(..), newSTArray )
66 import IOExts ( hPutBufBAFull )
68 import GHC.Arr ( STArray(..), newSTArray )
69 import IOExts ( hPutBufBA )
70 import CString ( unpackNBytesBA# )
73 import IOExts ( IORef, newIORef, readIORef, writeIORef )
75 import Char ( chr, ord )
77 #define hASH_TBL_SIZE 993
79 #if __GLASGOW_HASKELL__ < 503
80 hPutBufBA = hPutBufBAFull
84 @FastString@s are packed representations of strings
85 with a unique id for fast comparisons. The unique id
86 is assigned when creating the @FastString@, using
87 a hash table to map from the character string representation
92 = FastString -- packed repr. on the heap.
94 -- 0 => string literal, comparison
99 | UnicodeStr -- if contains characters outside '\1'..'\xFF'
101 [Int] -- character numbers
103 instance Eq FastString where
104 -- shortcut for real FastStrings
105 (FastString u1 _ _) == (FastString u2 _ _) = u1 ==# u2
106 a == b = case cmpFS a b of { LT -> False; EQ -> True; GT -> False }
108 (FastString u1 _ _) /= (FastString u2 _ _) = u1 /=# u2
109 a /= b = case cmpFS a b of { LT -> True; EQ -> False; GT -> True }
111 instance Ord FastString where
112 a <= b = case cmpFS a b of { LT -> True; EQ -> True; GT -> False }
113 a < b = case cmpFS a b of { LT -> True; EQ -> False; GT -> False }
114 a >= b = case cmpFS a b of { LT -> False; EQ -> True; GT -> True }
115 a > b = case cmpFS a b of { LT -> False; EQ -> False; GT -> True }
120 compare a b = cmpFS a b
122 lengthFS :: FastString -> Int
123 lengthFS (FastString _ l# _) = I# l#
124 lengthFS (UnicodeStr _ s) = length s
126 nullFastString :: FastString -> Bool
127 nullFastString (FastString _ l# _) = l# ==# 0#
128 nullFastString (UnicodeStr _ []) = True
129 nullFastString (UnicodeStr _ (_:_)) = False
131 unpackFS :: FastString -> String
132 unpackFS (FastString _ l# ba#) = unpackNBytesBA# ba# l#
133 unpackFS (UnicodeStr _ s) = map chr s
135 unpackIntFS :: FastString -> [Int]
136 unpackIntFS (UnicodeStr _ s) = s
137 unpackIntFS fs = map ord (unpackFS fs)
139 appendFS :: FastString -> FastString -> FastString
140 appendFS fs1 fs2 = mkFastStringInt (unpackIntFS fs1 ++ unpackIntFS fs2)
142 concatFS :: [FastString] -> FastString
143 concatFS ls = mkFastStringInt (concat (map unpackIntFS ls)) -- ToDo: do better
145 headFS :: FastString -> Char
146 headFS (FastString _ l# ba#) =
147 if l# ># 0# then C# (indexCharArray# ba# 0#) else error ("headFS: empty FS")
148 headFS (UnicodeStr _ (c:_)) = chr c
149 headFS (UnicodeStr _ []) = error ("headFS: empty FS")
151 headIntFS :: FastString -> Int
152 headIntFS (UnicodeStr _ (c:_)) = c
153 headIntFS fs = ord (headFS fs)
155 indexFS :: FastString -> Int -> Char
156 indexFS f i@(I# i#) =
159 | l# ># 0# && l# ># i# -> C# (indexCharArray# ba# i#)
160 | otherwise -> error (msg (I# l#))
161 UnicodeStr _ s -> chr (s!!i)
163 msg l = "indexFS: out of range: " ++ show (l,i)
165 tailFS :: FastString -> FastString
166 tailFS (FastString _ l# ba#) = mkFastSubStringBA# ba# 1# (l# -# 1#)
167 tailFS fs = mkFastStringInt (tail (unpackIntFS fs))
169 consFS :: Char -> FastString -> FastString
170 consFS c fs = mkFastStringInt (ord c : unpackIntFS fs)
172 uniqueOfFS :: FastString -> Int#
173 uniqueOfFS (FastString u# _ _) = u#
174 uniqueOfFS (UnicodeStr u# _) = u#
176 nilFS = mkFastString ""
179 Internally, the compiler will maintain a fast string symbol
180 table, providing sharing and fast comparison. Creation of
181 new @FastString@s then covertly does a lookup, re-using the
182 @FastString@ if there was a hit.
184 Caution: mkFastStringUnicode assumes that if the string is in the
185 table, it sits under the UnicodeStr constructor. Other mkFastString
186 variants analogously assume the FastString constructor.
189 data FastStringTable =
192 (MutableArray# RealWorld [FastString])
194 type FastStringTableVar = IORef FastStringTable
196 string_table :: FastStringTableVar
199 stToIO (newSTArray (0::Int,hASH_TBL_SIZE) [])
200 >>= \ (STArray _ _ arr#) ->
201 newIORef (FastStringTable 0# arr#))
203 lookupTbl :: FastStringTable -> Int# -> IO [FastString]
204 lookupTbl (FastStringTable _ arr#) i# =
206 readArray# arr# i# s#)
208 updTbl :: FastStringTableVar -> FastStringTable -> Int# -> [FastString] -> IO ()
209 updTbl fs_table_var (FastStringTable uid# arr#) i# ls =
210 IO (\ s# -> case writeArray# arr# i# ls s# of { s2# ->
212 writeIORef fs_table_var (FastStringTable (uid# +# 1#) arr#)
214 mkFastString# :: Addr# -> FastString
216 case strLength (A# a#) of { (I# len#) -> mkFastStringLen# a# len# }
218 mkFastStringLen# :: Addr# -> Int# -> FastString
219 mkFastStringLen# a# len# =
221 readIORef string_table >>= \ ft@(FastStringTable uid# tbl#) ->
225 -- _trace ("hashed: "++show (I# h)) $
226 lookupTbl ft h >>= \ lookup_result ->
227 case lookup_result of
229 -- no match, add it to table by copying out the
230 -- the string into a ByteArray
231 -- _trace "empty bucket" $
232 case copyPrefixStr (A# a#) (I# len#) of
233 (ByteArray _ _ barr#) ->
234 let f_str = FastString uid# len# barr# in
235 updTbl string_table ft h [f_str] >>
236 ({- _trace ("new: " ++ show f_str) $ -} return f_str)
238 -- non-empty `bucket', scan the list looking
239 -- entry with same length and compare byte by byte.
240 -- _trace ("non-empty bucket"++show ls) $
241 case bucket_match ls len# a# of
243 case copyPrefixStr (A# a#) (I# len#) of
244 (ByteArray _ _ barr#) ->
245 let f_str = FastString uid# len# barr# in
246 updTbl string_table ft h (f_str:ls) >>
247 ( {- _trace ("new: " ++ show f_str) $ -} return f_str)
248 Just v -> {- _trace ("re-use: "++show v) $ -} return v)
250 bucket_match [] _ _ = Nothing
251 bucket_match (v@(FastString _ l# ba#):ls) len# a# =
252 if len# ==# l# && eqStrPrefix a# ba# l# then
255 bucket_match ls len# a#
256 bucket_match (UnicodeStr _ _ : ls) len# a# =
257 bucket_match ls len# a#
259 mkFastSubStringBA# :: ByteArray# -> Int# -> Int# -> FastString
260 mkFastSubStringBA# barr# start# len# =
262 readIORef string_table >>= \ ft@(FastStringTable uid# tbl#) ->
264 h = hashSubStrBA barr# start# len#
266 -- _trace ("hashed(b): "++show (I# h)) $
267 lookupTbl ft h >>= \ lookup_result ->
268 case lookup_result of
270 -- no match, add it to table by copying out the
271 -- the string into a ByteArray
272 -- _trace "empty bucket(b)" $
273 case copySubStrBA (ByteArray btm btm barr#) (I# start#) (I# len#) of
274 (ByteArray _ _ ba#) ->
275 let f_str = FastString uid# len# ba# in
276 updTbl string_table ft h [f_str] >>
277 -- _trace ("new(b): " ++ show f_str) $
280 -- non-empty `bucket', scan the list looking
281 -- entry with same length and compare byte by byte.
282 -- _trace ("non-empty bucket(b)"++show ls) $
283 case bucket_match ls start# len# barr# of
285 case copySubStrBA (ByteArray btm btm barr#) (I# start#) (I# len#) of
286 (ByteArray _ _ ba#) ->
287 let f_str = FastString uid# len# ba# in
288 updTbl string_table ft h (f_str:ls) >>
289 -- _trace ("new(b): " ++ show f_str) $
292 -- _trace ("re-use(b): "++show v) $
298 bucket_match [] _ _ _ = Nothing
299 bucket_match (v:ls) start# len# ba# =
301 FastString _ l# barr# ->
302 if len# ==# l# && eqStrPrefixBA barr# ba# start# len# then
305 bucket_match ls start# len# ba#
306 UnicodeStr _ _ -> bucket_match ls start# len# ba#
308 mkFastStringUnicode :: [Int] -> FastString
309 mkFastStringUnicode s =
311 readIORef string_table >>= \ ft@(FastStringTable uid# tbl#) ->
315 -- _trace ("hashed(b): "++show (I# h)) $
316 lookupTbl ft h >>= \ lookup_result ->
317 case lookup_result of
319 -- no match, add it to table by copying out the
320 -- the string into a [Int]
321 let f_str = UnicodeStr uid# s in
322 updTbl string_table ft h [f_str] >>
323 -- _trace ("new(b): " ++ show f_str) $
326 -- non-empty `bucket', scan the list looking
327 -- entry with same length and compare byte by byte.
328 -- _trace ("non-empty bucket(b)"++show ls) $
329 case bucket_match ls of
331 let f_str = UnicodeStr uid# s in
332 updTbl string_table ft h (f_str:ls) >>
333 -- _trace ("new(b): " ++ show f_str) $
336 -- _trace ("re-use(b): "++show v) $
340 bucket_match [] = Nothing
341 bucket_match (v@(UnicodeStr _ s'):ls) =
342 if s' == s then Just v else bucket_match ls
343 bucket_match (FastString _ _ _ : ls) = bucket_match ls
345 mkFastStringNarrow :: String -> FastString
346 mkFastStringNarrow str =
347 case packString str of
348 (ByteArray _ (I# len#) frozen#) ->
349 mkFastSubStringBA# frozen# 0# len#
350 {- 0-indexed array, len# == index to one beyond end of string,
351 i.e., (0,1) => empty string. -}
353 mkFastString :: String -> FastString
354 mkFastString str = if all good str
355 then mkFastStringNarrow str
356 else mkFastStringUnicode (map ord str)
358 good c = c >= '\1' && c <= '\xFF'
360 mkFastStringInt :: [Int] -> FastString
361 mkFastStringInt str = if all good str
362 then mkFastStringNarrow (map chr str)
363 else mkFastStringUnicode str
365 good c = c >= 1 && c <= 0xFF
367 mkFastSubString :: Addr -> Int -> Int -> FastString
368 mkFastSubString (A# a#) (I# start#) (I# len#) =
369 mkFastStringLen# (addrOffset# a# start#) len#
373 hashStr :: Addr# -> Int# -> Int#
374 -- use the Addr to produce a hash value between 0 & m (inclusive)
378 1# -> ((ord# c0 *# 631#) +# len#) `remInt#` hASH_TBL_SIZE#
379 2# -> ((ord# c0 *# 631#) +# (ord# c1 *# 217#) +# len#) `remInt#` hASH_TBL_SIZE#
380 _ -> ((ord# c0 *# 631#) +# (ord# c1 *# 217#) +# (ord# c2 *# 43#) +# len#) `remInt#` hASH_TBL_SIZE#
382 c0 = indexCharOffAddr# a# 0#
383 c1 = indexCharOffAddr# a# (len# `quotInt#` 2# -# 1#)
384 c2 = indexCharOffAddr# a# (len# -# 1#)
386 c1 = indexCharOffAddr# a# 1#
387 c2 = indexCharOffAddr# a# 2#
390 hashSubStrBA :: ByteArray# -> Int# -> Int# -> Int#
391 -- use the byte array to produce a hash value between 0 & m (inclusive)
392 hashSubStrBA ba# start# len# =
395 1# -> ((ord# c0 *# 631#) +# len#) `remInt#` hASH_TBL_SIZE#
396 2# -> ((ord# c0 *# 631#) +# (ord# c1 *# 217#) +# len#) `remInt#` hASH_TBL_SIZE#
397 _ -> ((ord# c0 *# 631#) +# (ord# c1 *# 217#) +# (ord# c2 *# 43#) +# len#) `remInt#` hASH_TBL_SIZE#
399 c0 = indexCharArray# ba# 0#
400 c1 = indexCharArray# ba# (len# `quotInt#` 2# -# 1#)
401 c2 = indexCharArray# ba# (len# -# 1#)
403 -- c1 = indexCharArray# ba# 1#
404 -- c2 = indexCharArray# ba# 2#
406 hashUnicode :: [Int] -> Int#
407 -- use the Addr to produce a hash value between 0 & m (inclusive)
409 hashUnicode [I# c0] = ((c0 *# 631#) +# 1#) `remInt#` hASH_TBL_SIZE#
410 hashUnicode [I# c0, I# c1] = ((c0 *# 631#) +# (c1 *# 217#) +# 2#) `remInt#` hASH_TBL_SIZE#
411 hashUnicode s = ((c0 *# 631#) +# (c1 *# 217#) +# (c2 *# 43#) +# len#) `remInt#` hASH_TBL_SIZE#
415 I# c1 = s !! (I# (len# `quotInt#` 2# -# 1#))
416 I# c2 = s !! (I# (len# -# 1#))
421 cmpFS :: FastString -> FastString -> Ordering
422 cmpFS (UnicodeStr u1# s1) (UnicodeStr u2# s2) = if u1# ==# u2# then EQ
424 cmpFS (UnicodeStr _ s1) s2 = compare s1 (unpackIntFS s2)
425 cmpFS s1 (UnicodeStr _ s2) = compare (unpackIntFS s1) s2
426 cmpFS (FastString u1# _ b1#) (FastString u2# _ b2#) = -- assume non-null chars
431 _ccall_ strcmp (ByteArray bot bot b1#) (ByteArray bot bot b2#) >>= \ (I# res) ->
434 else if res ==# 0# then EQ
442 Outputting @FastString@s is quick, just block copying the chunk (using
446 hPutFS :: Handle -> FastString -> IO ()
447 hPutFS handle (FastString _ l# ba#)
448 | l# ==# 0# = return ()
449 | otherwise = do mba <- stToIO $ unsafeThawByteArray (ByteArray (bot::Int) bot ba#)
450 hPutBufBA handle mba (I# l#)
452 bot = error "hPutFS.ba"
454 -- ONLY here for debugging the NCG (so -ddump-stix works for string
455 -- literals); no idea if this is really necessary. JRS, 010131
456 hPutFS handle (UnicodeStr _ is)
457 = hPutStr handle ("(UnicodeStr " ++ show is ++ ")")
460 Here for convenience only.
463 type LitString = Addr
464 -- ToDo: make it a Ptr when we don't have to support 4.08 any more
466 mkLitString# :: Addr# -> LitString
467 mkLitString# a# = A# a#