-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the lower-level array-construction primitives to avoid intermediate allocations and perform much better at deserialisation. On Cabal (which uses tar for the hackage index), we observed: * Deserialisation of IntTries go from 1.5s to 200ms, with 10GB of allocations going down to roughly 600MB. * StringTable deserialization go from 700ms to 50ms, with 4GB of allocations going down to 80MB.
- Loading branch information
Showing
5 changed files
with
70 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Codec.Archive.Tar.Index.Utils where | ||
|
||
import Data.ByteString as BS | ||
import Control.Exception (assert) | ||
|
||
import Data.ByteString.Internal (ByteString(..), unsafeWithForeignPtr, accursedUnutterablePerformIO) | ||
import GHC.Int (Int(..), Int32) | ||
import GHC.Word (Word32(..), byteSwap32) | ||
import Foreign.Storable (peek) | ||
import GHC.Ptr (castPtr, plusPtr, Ptr) | ||
import Data.Array.Base | ||
import Data.Array.IO.Internals (unsafeFreezeIOUArray) | ||
|
||
-- | Construct a `UArray Word32 Word32` from a ByteString of 32bit big endian | ||
-- words. | ||
beToLe :: Word32 | ||
-- ^ The total array length (the number of 32bit words in the array) | ||
-> BS.ByteString | ||
-- ^ The bytestring from which the UArray is constructed. | ||
-- The content must start in the first byte! (i.e. the meta-data words | ||
-- that shouldn't be part of the array must have been dropped already) | ||
-> IO (UArray Word32 Word32) | ||
beToLe lenArr (BS fptr _) = unsafeWithForeignPtr fptr $ \ptr -> do | ||
let ptr' = castPtr ptr :: Ptr Word32 | ||
unsafeFreezeIOUArray =<< | ||
newGenArray (0, lenArr - 1) (\offset -> do | ||
Check failure on line 26 in Codec/Archive/Tar/Index/Utils.hs GitHub Actions / build (windows-latest, latest)
Check failure on line 26 in Codec/Archive/Tar/Index/Utils.hs GitHub Actions / build (windows-latest, latest)
Check failure on line 26 in Codec/Archive/Tar/Index/Utils.hs GitHub Actions / build (windows-latest, latest)
Check failure on line 26 in Codec/Archive/Tar/Index/Utils.hs GitHub Actions / build (macOS-latest, latest)
Check failure on line 26 in Codec/Archive/Tar/Index/Utils.hs GitHub Actions / build (macOS-latest, latest)
|
||
byteSwap32 <$> peek (ptr' `plusPtr` (fromIntegral offset * 4))) | ||
|
||
readInt32BE :: BS.ByteString -> Int -> Int32 | ||
readInt32BE bs i = fromIntegral (readWord32BE bs i) | ||
{-# INLINE readInt32BE #-} | ||
|
||
readWord32BE :: BS.ByteString -> Int -> Word32 | ||
readWord32BE (BS fptr len) i = | ||
assert (i >= 0 && i+3 <= len - 1) $ | ||
accursedUnutterablePerformIO $ | ||
unsafeWithForeignPtr fptr $ \ptr -> do | ||
byteSwap32 <$> peek (castPtr (ptr `plusPtr` i)) | ||
{-# INLINE readWord32BE #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters