From e2378e0eeac048b653bedeafc58e4d4cd0e616a3 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Wed, 3 Feb 2021 13:18:26 +0200 Subject: [PATCH] Core, Remote: map -> fmap Allow not only lists. This for example should allow NonEmpty lists at least. Also do not think I do this things on a whim. I did some research on: is there a reason to use map over fmap, and are the performance reasons? In short - there is 0 infornamation on why some people use `map` over `fmap`, there are no reports of performance reasons. Well, I know that there is a possibility of a minor type class interface compilation & runtime use cost. But I think GHC is good enough to infer zero cost for the concrete list type for which `map` gets used. And overall we not did thorough profiling/perfommans walkthrough so far. I am sure that use of standard fmap for code flexibility is not a bottleneck in the design, I've seen some performance problems design has. And we not even did the profiling to do inlining and specialize work yet. It is more effective to keep using `fmap`, and supply specialization, which allows to keep the code polymorhic, portable to write for and effective in performance. --- hnix-store-core/src/System/Nix/Internal/Base32.hs | 2 +- hnix-store-core/src/System/Nix/Internal/Hash.hs | 2 +- hnix-store-core/src/System/Nix/ReadonlyStore.hs | 2 +- hnix-store-remote/src/System/Nix/Store/Remote/Util.hs | 4 ++-- hnix-store-remote/tests/NixDaemon.hs | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/hnix-store-core/src/System/Nix/Internal/Base32.hs b/hnix-store-core/src/System/Nix/Internal/Base32.hs index 6e0bcd7..1129238 100644 --- a/hnix-store-core/src/System/Nix/Internal/Base32.hs +++ b/hnix-store-core/src/System/Nix/Internal/Base32.hs @@ -22,7 +22,7 @@ digits32 = Vector.fromList "0123456789abcdfghijklmnpqrsvwxyz" -- | Encode a 'BS.ByteString' in Nix's base32 encoding encode :: ByteString -> Text -encode c = Data.Text.pack $ map char32 [nChar - 1, nChar - 2 .. 0] +encode c = Data.Text.pack $ fmap char32 [nChar - 1, nChar - 2 .. 0] where -- Each base32 character gives us 5 bits of information, while -- each byte gives is 8. Because 'div' rounds down, we need to add diff --git a/hnix-store-core/src/System/Nix/Internal/Hash.hs b/hnix-store-core/src/System/Nix/Internal/Hash.hs index f9e4729..74952ba 100644 --- a/hnix-store-core/src/System/Nix/Internal/Hash.hs +++ b/hnix-store-core/src/System/Nix/Internal/Hash.hs @@ -218,7 +218,7 @@ instance (ValidAlgo a, KnownNat n) => ValidAlgo ('Truncated n a) where truncateDigest :: forall n a.(KnownNat n) => Digest a -> Digest ('Truncated n a) truncateDigest (Digest c) = - Digest $ BS.pack $ map truncOutputByte [0.. n-1] + Digest $ BS.pack $ fmap truncOutputByte [0.. n-1] where n = fromIntegral $ natVal (Proxy @n) diff --git a/hnix-store-core/src/System/Nix/ReadonlyStore.hs b/hnix-store-core/src/System/Nix/ReadonlyStore.hs index a7d0c34..0b4df3d 100644 --- a/hnix-store-core/src/System/Nix/ReadonlyStore.hs +++ b/hnix-store-core/src/System/Nix/ReadonlyStore.hs @@ -41,7 +41,7 @@ makeTextPath makeTextPath fp nm h refs = makeStorePath fp ty h nm where ty = - BS.intercalate ":" ("text" : map storePathToRawFilePath (HS.toList refs)) + BS.intercalate ":" ("text" : fmap storePathToRawFilePath (HS.toList refs)) makeFixedOutputPath :: forall hashAlgo diff --git a/hnix-store-remote/src/System/Nix/Store/Remote/Util.hs b/hnix-store-remote/src/System/Nix/Store/Remote/Util.hs index 66b6d22..26df79d 100644 --- a/hnix-store-remote/src/System/Nix/Store/Remote/Util.hs +++ b/hnix-store-remote/src/System/Nix/Store/Remote/Util.hs @@ -110,14 +110,14 @@ putText :: Text -> Put putText = putByteStringLen . textToBSL putTexts :: [Text] -> Put -putTexts = putByteStrings . map textToBSL +putTexts = putByteStrings . fmap textToBSL getPath :: FilePath -> Get (Either String StorePath) getPath sd = parsePath sd <$> getByteStringLen getPaths :: FilePath -> Get StorePathSet getPaths sd = - Data.HashSet.fromList . rights . map (parsePath sd) <$> getByteStrings + Data.HashSet.fromList . rights . fmap (parsePath sd) <$> getByteStrings putPath :: StorePath -> Put putPath = putByteStringLen . BSL.fromStrict . storePathToRawFilePath diff --git a/hnix-store-remote/tests/NixDaemon.hs b/hnix-store-remote/tests/NixDaemon.hs index 8824ecd..f98b3ba 100644 --- a/hnix-store-remote/tests/NixDaemon.hs +++ b/hnix-store-remote/tests/NixDaemon.hs @@ -108,9 +108,9 @@ enterNamespaces = do gid <- getEffectiveGroupID unshare [User, Network, Mount] - -- map our (parent) uid to root + -- fmap our (parent) uid to root writeUserMappings Nothing [UserMapping 0 uid 1] - -- map our (parent) gid to root group + -- fmap our (parent) gid to root group writeGroupMappings Nothing [GroupMapping 0 gid 1] True withNixDaemon