Skip to content

Commit 6399f29

Browse files
committed
Use StrictByteString and LazyByteString instead of ByteString in a few modules
1 parent f26c81a commit 6399f29

9 files changed

Lines changed: 45 additions & 45 deletions

File tree

hackage-server.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ common defaults
149149
, array >= 0.5 && < 0.6
150150
, base >= 4.18 && < 4.22
151151
, binary >= 0.8 && < 0.9
152-
, bytestring >= 0.10 && < 0.13
152+
, bytestring >= 0.11.2 && < 0.13
153153
, containers >= 0.6.0 && < 0.9
154154
, deepseq >= 1.4 && < 1.6
155155
, directory >= 1.3 && < 1.4

src/Distribution/Server/Features/Core.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import qualified Codec.Compression.GZip as GZip
2727
import Data.Aeson (Value (..), toJSON)
2828
import qualified Data.Aeson.Key as Key
2929
import qualified Data.Aeson.KeyMap as KeyMap
30-
import Data.ByteString.Lazy (ByteString)
30+
import Data.ByteString.Lazy (LazyByteString)
3131
import qualified Data.Foldable as Foldable
3232
import qualified Data.Text as Text
3333
import Data.Time.Clock (UTCTime, getCurrentTime)
@@ -130,7 +130,7 @@ data CoreFeature = CoreFeature {
130130
-- modification time for the tar entry.
131131
--
132132
-- This runs a `PackageChangeIndexExtra` hook when done.
133-
updateArchiveIndexEntry :: forall m. MonadIO m => FilePath -> ByteString -> UTCTime -> m (),
133+
updateArchiveIndexEntry :: forall m. MonadIO m => FilePath -> LazyByteString -> UTCTime -> m (),
134134

135135
-- | Notification of package or index changes.
136136
packageChangeHook :: Hook PackageChange (),
@@ -175,7 +175,7 @@ data PackageChange
175175
| PackageChangeInfo PackageUpdate PkgInfo PkgInfo
176176
-- | A file has changed in the package index tar not covered by any of the
177177
-- other change types.
178-
| PackageChangeIndexExtra String ByteString UTCTime
178+
| PackageChangeIndexExtra String LazyByteString UTCTime
179179

180180
-- | A predicate to use with `packageChangeHook` and `registerHookJust` for
181181
-- keeping other features synchronized with the main package index.
@@ -212,7 +212,7 @@ isPackageDeleteVersion :: Maybe PackageId,
212212
isPackageChangeCabalFile :: Maybe (PackageId, CabalFileText),
213213
isPackageChangeCabalFileUploadInfo :: Maybe (PackageId, UploadInfo),
214214
isPackageChangeTarball :: Maybe (PackageId, PkgTarball),
215-
isPackageIndexExtraChange :: Maybe (String, ByteString, UTCTime)
215+
isPackageIndexExtraChange :: Maybe (String, LazyByteString, UTCTime)
216216
-}
217217

218218
data CoreResource = CoreResource {
@@ -591,7 +591,7 @@ coreFeature ServerEnv{serverBlobStore = store} UserFeature{..}
591591
runHook_ packageChangeHook (PackageChangeInfo PackageUpdatedUploadTime oldpkginfo newpkginfo)
592592
return True
593593

594-
updateArchiveIndexEntry :: MonadIO m => FilePath -> ByteString -> UTCTime -> m ()
594+
updateArchiveIndexEntry :: MonadIO m => FilePath -> LazyByteString -> UTCTime -> m ()
595595
updateArchiveIndexEntry entryName entryData entryTime = logTiming maxBound ("updateArchiveIndexEntry " ++ show entryName) $ do
596596
updateState packagesState $
597597
AddOtherIndexEntry $ ExtraEntry entryName entryData entryTime

src/Distribution/Server/Features/EditCabalFiles.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Distribution.Server.Util.CabalRevisions
2323
(Change(..), diffCabalRevisions, insertRevisionField)
2424
import Text.StringTemplate.Classes (SElem(SM))
2525

26-
import Data.ByteString.Lazy (ByteString)
26+
import Data.ByteString.Lazy (LazyByteString)
2727
import qualified Data.ByteString.Lazy as BS.L
2828
import qualified Data.Map as Map
2929
import Data.Time (getCurrentTime)
@@ -126,7 +126,7 @@ editCabalFilesFeature _env templates
126126
(look "publish" >> return True)
127127

128128
responseTemplate :: ([TemplateAttr] -> Template) -> PackageId
129-
-> ByteString -> Bool -> [String] -> [Change]
129+
-> LazyByteString -> Bool -> [String] -> [Change]
130130
-> ServerPartE Response
131131
responseTemplate template pkgid cabalFile publish errors changes =
132132
ok $ toResponse $ template
@@ -139,9 +139,9 @@ editCabalFilesFeature _env templates
139139

140140

141141
-- | Wrapper around 'diffCabalRevisions' which operates on
142-
-- 'ByteString' decoded with lenient UTF8 and with any leading BOM
142+
-- 'LazyByteString' decoded with lenient UTF8 and with any leading BOM
143143
-- stripped.
144-
diffCabalRevisionsByteString :: ByteString -> ByteString -> Either String [Change]
144+
diffCabalRevisionsByteString :: LazyByteString -> LazyByteString -> Either String [Change]
145145
diffCabalRevisionsByteString oldRevision newRevision =
146146
maybe (diffCabalRevisions (BS.L.toStrict oldRevision) (BS.L.toStrict newRevision))
147147
Left

src/Distribution/Server/Features/Html.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ import qualified Data.Map as Map
6868
import qualified Data.Set as Set
6969
import qualified Data.Vector as Vec
7070
import qualified Data.Text as T
71-
import qualified Data.ByteString.Lazy.Char8 as BS (ByteString)
71+
import qualified Data.ByteString.Lazy as BS (LazyByteString)
7272
import qualified Network.URI as URI
7373

7474
import Text.XHtml.Strict
@@ -849,7 +849,7 @@ mkHtmlCore ServerEnv{serverBaseURI, serverBlobStore}
849849

850850

851851
-- | Common helper used by 'serveCandidatePage' and 'servePackagePage'
852-
makeReadme :: MonadIO m => PackageRender -> m (Maybe BS.ByteString)
852+
makeReadme :: MonadIO m => PackageRender -> m (Maybe BS.LazyByteString)
853853
makeReadme render = case rendReadme render of
854854
Just (tarfile, _, offset, _) ->
855855
either (\_err -> return Nothing) (return . Just . snd) =<<

src/Distribution/Server/Features/Upload.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Data.Maybe (fromMaybe)
2929
import Data.List (dropWhileEnd, intersperse)
3030
import Data.Time.Clock (getCurrentTime)
3131
import Data.Function (fix)
32-
import Data.ByteString.Lazy (ByteString)
32+
import Data.ByteString.Lazy (LazyByteString)
3333

3434
import Distribution.Package
3535
import Distribution.PackageDescription (GenericPackageDescription)
@@ -97,7 +97,7 @@ data UploadResult = UploadResult {
9797
-- The parsed Cabal file.
9898
uploadDesc :: !GenericPackageDescription,
9999
-- The text of the Cabal file.
100-
uploadCabal :: !ByteString,
100+
uploadCabal :: !LazyByteString,
101101
-- Any warnings from unpacking the tarball.
102102
uploadWarnings :: ![String]
103103
}
@@ -417,7 +417,7 @@ uploadFeature ServerEnv{serverBlobStore = store}
417417
--FIXME: this should have been covered earlier
418418
uid <- guardAuthenticated
419419
now <- liftIO getCurrentTime
420-
let processPackage :: ByteString -> IO (Either ErrorResponse (UploadResult, BlobStorage.BlobId))
420+
let processPackage :: LazyByteString -> IO (Either ErrorResponse (UploadResult, BlobStorage.BlobId))
421421
processPackage content' = do
422422
-- as much as it would be nice to do requirePackageAuth in here,
423423
-- processPackage is run in a handle bracket

src/Distribution/Server/Packages/Index.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import Data.SafeCopy (base, deriveSafeCopy)
4343
import Data.Map (Map)
4444
import qualified Data.Map as Map
4545
import qualified Data.Vector as Vec
46-
import Data.ByteString.Lazy (ByteString)
46+
import Data.ByteString.Lazy (LazyByteString)
4747
import System.FilePath.Posix
4848
import Data.Maybe (mapMaybe)
4949

@@ -76,7 +76,7 @@ data TarIndexEntry =
7676
-- | Additional entries that we add to the tarball
7777
--
7878
-- This is currently used for @preferred-versions@.
79-
| ExtraEntry !FilePath !ByteString !UTCTime
79+
| ExtraEntry !FilePath !LazyByteString !UTCTime
8080
deriving (Eq, Show)
8181

8282
type RevisionNo = Int
@@ -92,7 +92,7 @@ deriveSafeCopy 0 'base ''TarIndexEntry
9292
-- a package index, an index tarball. This tarball has the modification times
9393
-- and uploading users built-in.
9494

95-
writeIncremental :: PackageIndex PkgInfo -> [TarIndexEntry] -> ByteString
95+
writeIncremental :: PackageIndex PkgInfo -> [TarIndexEntry] -> LazyByteString
9696
writeIncremental pkgs =
9797
Tar.write . mapMaybe mkTarEntry
9898
where
@@ -144,14 +144,14 @@ utcToUnixTime :: UTCTime -> Int64
144144
utcToUnixTime = truncate . utcTimeToPOSIXSeconds
145145

146146
-- | Extract legacy entries
147-
legacyExtras :: [TarIndexEntry] -> Map String (ByteString, UTCTime)
147+
legacyExtras :: [TarIndexEntry] -> Map String (LazyByteString, UTCTime)
148148
legacyExtras = go Map.empty
149149
where
150150
-- Later entries in the update log will override earlier ones. This is
151151
-- intentional.
152-
go :: Map String (ByteString, UTCTime)
152+
go :: Map String (LazyByteString, UTCTime)
153153
-> [TarIndexEntry]
154-
-> Map String (ByteString, UTCTime)
154+
-> Map String (LazyByteString, UTCTime)
155155
go acc [] = acc
156156
go acc (ExtraEntry fp bs time : es) =
157157
let acc' = Map.insert fp (bs, time) acc
@@ -173,7 +173,7 @@ legacyExtras = go Map.empty
173173
-- compression), contains at most one preferred-version per package (important
174174
-- because of a bug in cabal which would otherwise merge all preferred-versions
175175
-- files for a package), and does not contain the TUF files.
176-
writeLegacy :: Users -> Map String (ByteString, UTCTime) -> PackageIndex PkgInfo -> ByteString
176+
writeLegacy :: Users -> Map String (LazyByteString, UTCTime) -> PackageIndex PkgInfo -> LazyByteString
177177
writeLegacy users =
178178
writeLegacyAux (cabalFileByteString . pkgLatestCabalFileText) setModTime
179179
. extraEntries
@@ -192,25 +192,25 @@ writeLegacy users =
192192

193193
userName = display . userIdToName users
194194

195-
extraEntries :: Map FilePath (ByteString, UTCTime) -> [Tar.Entry]
195+
extraEntries :: Map FilePath (LazyByteString, UTCTime) -> [Tar.Entry]
196196
extraEntries emap = do
197197
(path, (entry, mtime)) <- Map.toList emap
198198
Right tarPath <- return $ Tar.toTarPath False path
199199
return $ (Tar.fileEntry tarPath entry) { Tar.entryTime = utcToUnixTime mtime }
200200

201-
-- | Create an uncompressed tar repository index file as a 'ByteString'.
201+
-- | Create an uncompressed tar repository index file as a 'LazyByteString'.
202202
--
203203
-- Takes a couple functions to turn a package into a tar entry. Extra
204204
-- entries are also accepted.
205205
--
206206
-- This used to live in Distribution.Server.Util.Index.
207207
--
208208
writeLegacyAux :: Package pkg
209-
=> (pkg -> ByteString)
209+
=> (pkg -> LazyByteString)
210210
-> (pkg -> Tar.Entry -> Tar.Entry)
211211
-> [Tar.Entry]
212212
-> PackageIndex pkg
213-
-> ByteString
213+
-> LazyByteString
214214
writeLegacyAux externalPackageRep updateEntry extras =
215215
Tar.write . (extras++) . map entry . PackageIndex.allPackages
216216
where

src/Distribution/Server/Packages/Types.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Distribution.PackageDescription.Parsec
3535
( parseGenericPackageDescription, runParseResult )
3636

3737
import Data.Serialize (Serialize)
38-
import Data.ByteString.Lazy (ByteString)
38+
import Data.ByteString.Lazy (LazyByteString)
3939
import Data.Time.Clock (UTCTime(..))
4040
import Data.Time.Calendar (Day(..))
4141
import Data.SafeCopy
@@ -47,7 +47,7 @@ import qualified Data.Vector as Vec
4747
Datatypes
4848
-------------------------------------------------------------------------------}
4949

50-
newtype CabalFileText = CabalFileText { cabalFileByteString :: ByteString }
50+
newtype CabalFileText = CabalFileText { cabalFileByteString :: LazyByteString }
5151
deriving (Eq, MemSize)
5252

5353
-- | The information we keep about a particular version of a package.
@@ -227,7 +227,7 @@ pkgDescMaybe pkgInfo =
227227
(_, Right x) -> Just x
228228

229229

230-
blobInfoFromBS :: BlobId -> ByteString -> BlobInfo
230+
blobInfoFromBS :: BlobId -> LazyByteString -> BlobInfo
231231
blobInfoFromBS blobId bs = BlobInfo {
232232
blobInfoId = blobId
233233
, blobInfoLength = fromIntegral $ BS.L.length bs

src/Distribution/Server/Util/Parse.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Distribution.Server.Util.Parse (
66
import qualified Text.ParserCombinators.ReadP as Parse
77

88
import qualified Data.Char as Char
9-
import Data.ByteString.Lazy (ByteString)
9+
import Data.ByteString.Lazy (LazyByteString)
1010
import qualified Data.Text.Lazy as Text
1111
import qualified Data.Text.Lazy.Encoding as Text
1212
import qualified Data.Text.Encoding.Error as Text
@@ -28,8 +28,8 @@ ignoreBOM :: String -> String
2828
ignoreBOM ('\xFEFF':string) = string
2929
ignoreBOM string = string
3030

31-
unpackUTF8 :: ByteString -> String
31+
unpackUTF8 :: LazyByteString -> String
3232
unpackUTF8 = ignoreBOM . Text.unpack . Text.decodeUtf8With Text.lenientDecode
3333

34-
packUTF8 :: String -> ByteString
34+
packUTF8 :: String -> LazyByteString
3535
packUTF8 = Text.encodeUtf8 . Text.pack

src/Distribution/Server/Util/ParseSpecVer.hs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Distribution.Server.Util.ParseSpecVer
1212

1313
import Distribution.Server.Prelude
1414

15-
import Data.ByteString (ByteString)
15+
import Data.ByteString (StrictByteString)
1616
import qualified Data.ByteString as BS
1717
import qualified Data.ByteString.Char8 as BC8
1818
import qualified Data.ByteString.Lazy as BSL
@@ -47,10 +47,10 @@ import Distribution.PackageDescription.Parsec ( runParseResult, parseG
4747
-- @.cabal@ files are accepted which support the heuristic parsing.
4848
--
4949
-- If no valid version field can be found, @Nothing@ is returned.
50-
parseSpecVer :: ByteString -> Maybe CabalSpecVersion
50+
parseSpecVer :: StrictByteString -> Maybe CabalSpecVersion
5151
parseSpecVer = findCabVer
5252

53-
parseSpecVerLazy :: BSL.ByteString -> Maybe CabalSpecVersion
53+
parseSpecVerLazy :: BSL.LazyByteString -> Maybe CabalSpecVersion
5454
parseSpecVerLazy = parseSpecVer . BSL.toStrict
5555

5656
versionToCabalSpecVersion :: Version -> Maybe CabalSpecVersion
@@ -59,11 +59,11 @@ versionToCabalSpecVersion = cabalSpecFromVersionDigits . versionNumbers
5959
isWS :: Word8 -> Bool
6060
isWS = (`elem` [0x20,0x09])
6161

62-
eatWS :: ByteString -> ByteString
62+
eatWS :: StrictByteString -> StrictByteString
6363
eatWS = BS.dropWhile isWS
6464

6565
-- | Try to heuristically locate & parse a 'cabal-version' field
66-
findCabVer :: ByteString -> Maybe CabalSpecVersion
66+
findCabVer :: StrictByteString -> Maybe CabalSpecVersion
6767
findCabVer raw = msum [ versionToCabalSpecVersion =<< decodeVer y | (_,_,y) <- findCabVers raw ]
6868

6969
-- | Return list of @cabal-version@ candidates as 3-tuples of
@@ -81,7 +81,7 @@ findCabVer raw = msum [ versionToCabalSpecVersion =<< decodeVer y | (_,_,y) <- f
8181
--
8282
-- NB: Later occurrences of @cabal-version@ override earlier ones. In
8383
-- future @cabal-versions@ it will be disallowed.
84-
findCabVers :: ByteString -> [(ByteString,Int,[ByteString])]
84+
findCabVers :: StrictByteString -> [(StrictByteString, Int, [StrictByteString])]
8585
findCabVers buf0 = mapMaybe go ixs
8686
where
8787
go i
@@ -105,7 +105,7 @@ findCabVers buf0 = mapMaybe go ixs
105105
map getInd l'
106106

107107
-- split off indentation for single line
108-
getInd :: ByteString -> (Int,ByteString)
108+
getInd :: StrictByteString -> (Int, StrictByteString)
109109
getInd x = case BS.span isWS x of (i,r) -> (BS.length i,r)
110110

111111
isNonComment = not . BS.isPrefixOf "--"
@@ -114,11 +114,11 @@ findCabVers buf0 = mapMaybe go ixs
114114
ixs = strCaseStrAll buf0 "cabal-version"
115115

116116
-- | Lookup-table mapping "x.y.z" strings to 'Version'
117-
verDictV :: Map.HashMap ByteString Version
117+
verDictV :: Map.HashMap StrictByteString Version
118118
verDictV = Map.fromList [ (BC8.pack (prettyShow v), v) | v <- knownVers ]
119119

120120
-- | Lookup-table mapping ">=x.y.z" strings to 'Version'
121-
verDictRg :: Map.HashMap ByteString Version
121+
verDictRg :: Map.HashMap StrictByteString Version
122122
verDictRg = Map.fromList [ (">=" <> BC8.pack (prettyShow v), v) | v <- knownVers ]
123123

124124
-- | List of cabal-version values contained in Hackage's package index as of 2017-07
@@ -224,7 +224,7 @@ knownVers = map mkVersion
224224
]
225225

226226
-- | Fast decoder
227-
decodeVer :: [ByteString] -> Maybe Version
227+
decodeVer :: [StrictByteString] -> Maybe Version
228228
decodeVer ws = case ws of
229229
[">=",v] -> Map.lookup v verDictV -- most common case
230230
[v] -> Map.lookup v verDictRg <|> -- most common case
@@ -233,7 +233,7 @@ decodeVer ws = case ws of
233233
_ -> decodeVerFallback (mconcat ws)
234234

235235
-- | Fallback parser for when lookup-table based parsing fails
236-
decodeVerFallback :: ByteString -> Maybe Version
236+
decodeVerFallback :: StrictByteString -> Maybe Version
237237
decodeVerFallback v0 = simpleParse v <|> parseSpecVR
238238
where
239239
parseSpecVR = do
@@ -250,7 +250,7 @@ foreign import ccall unsafe "string.h strcasestr" c_strcasestr :: Ptr CChar -> P
250250
-- | Find indices (in reverse order) of all non-overlapping
251251
-- case-insensitive occurrences of s2 in s1
252252
{-# NOINLINE strCaseStrAll #-}
253-
strCaseStrAll :: ByteString -> ByteString -> [Int]
253+
strCaseStrAll :: StrictByteString -> StrictByteString -> [Int]
254254
strCaseStrAll s1 s2
255255
| BS.null s1 || BS.null s2 = []
256256
| BS.elem 0 s1 || BS.elem 0 s2 = undefined
@@ -286,7 +286,7 @@ scanSpecVersionLazy bs = do
286286
-- * Starting with cabal-version:2.2 'scanSpecVersionLazy' must succeed
287287
--
288288
-- 'True' is returned in the first element if sanity checks passes.
289-
parseGenericPackageDescriptionChecked :: BSL.ByteString -> (Bool, [PWarning], Either (Maybe Version, [PError]) GenericPackageDescription)
289+
parseGenericPackageDescriptionChecked :: BSL.LazyByteString -> (Bool, [PWarning], Either (Maybe Version, [PError]) GenericPackageDescription)
290290
parseGenericPackageDescriptionChecked bs = case parseGenericPackageDescription' bs of
291291
(warns, Left pe) -> (False, warns, Left $ fmap toList pe)
292292
(warns, Right gpd) -> (isOk (specVersion (packageDescription gpd)), warns, Right gpd)

0 commit comments

Comments
 (0)