Skip to content

Commit

Permalink
Merge request haskell-nix#890 Swtitch to relude; some String -> Text …
Browse files Browse the repository at this point in the history
…migration

Relates to the discussion: haskell-nix#889 and the report: haskell-nix#897.

Wanted to practically look at how it is to switch the project, and how much it would help to organize textual data types in the project.
Results are very surprising, migration went just as official documentation stated and without hiccups, and easiness to switch things to Text started to reveal itself.
Migration to text, basic exception handling functions use `Text` - this alone should save a lot of RAM in runtime comparing to `base prelude` with String, since, as far as I know, some exception handling function get evaluated eagerly in place and stored in RAM - that I touched upon in our performance thread.

And migration is smooth. In one day managed to migrate the whole project.

---

There are many more improvements possible, more imports and qualified imports can be cleaned-up, and more String to Text can be switched, but these kind of processes are long multi-stage processes.

The prelude update went even without any type signature anywhere, so it has happened without API changes (so according to Haskell seems like there are no API changes), but of course it is safe to mention these type of change in the ChangeLog, and mention that something may start to return Text or NonEmpty if that happens. And I plan to go and myself help downstream projects with migration to future release anyway, since it is big, I suspect 2-3 projects we have downstream use if even 0.5-3% of our API, if not just `parseNixFile/Text` & `normalForm`.


---

Please note, that this is a draft, and this change makes many improvements possible to make in the code.

Migration went smooth, and `relude` forces builds to bark on the `undefined` - those are not mine, they were there before for months 👐.

---

  * ✔️  project already works.
  * ✔️  done backward compatibility for GHC 8.6.
  * ✔️ clean-up.

Done basic integration clean-up after the migration.

The next patches as an effect of `relude` present, would clean-up and refactor the code.
  • Loading branch information
Anton-Latukha authored Mar 21, 2021
2 parents 7ac94bb + cd02c6d commit 50e63f8
Show file tree
Hide file tree
Showing 57 changed files with 353 additions and 579 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/Optional-Nix-dev-env-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ jobs:


build20:
name: "GHC 8.10, strict quality build, SDist, Optimizations, Benchmark, Haddock, Completions"
# name: "GHC 8.10, strict quality build, SDist, Optimizations, Benchmark, Haddock, Completions"
name: "GHC 8.10, SDist, Optimizations, Benchmark, Haddock, Completions"
runs-on: ubuntu-latest
continue-on-error: true
steps:
Expand All @@ -104,7 +105,7 @@ jobs:
env:
compiler: "ghc8103"
buildFromSdist: "true"
buildStrictly: "true"
buildStrictly: "false"
linkWithGold: "true"
doHaddock: "true"
doHyperlinkSource: "true"
Expand Down
32 changes: 30 additions & 2 deletions hnix.cabal
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cabal-version: 2.2
name: hnix
version: 0.12.0.1
synopsis: Haskell implementation of the Nix language
Expand All @@ -7,10 +8,9 @@ homepage: https://github.com/haskell-nix/hnix#readme
bug-reports: https://github.com/haskell-nix/hnix/issues
author: John Wiegley
maintainer: [email protected]
license: BSD3
license: BSD-3-Clause
license-file: License
build-type: Simple
cabal-version: >= 1.10
data-dir: data/
data-files:
nix/corepkgs/buildenv.nix
Expand Down Expand Up @@ -393,6 +393,10 @@ library
Paths_hnix
hs-source-dirs:
src
mixins:
base hiding (Prelude)
, relude
, relude (Relude as Prelude)
ghc-options: -Wall -fprint-potential-instances
build-depends:
aeson >= 1.4.2 && < 1.6
Expand Down Expand Up @@ -432,6 +436,7 @@ library
, process >= 1.6.3 && < 1.7
, ref-tf >= 0.4.0 && <= 0.4.0.2
, regex-tdfa >= 1.2.3 && < 1.4
, relude
, scientific >= 0.3.6 && < 0.4
, semialign >= 1 && < 1.2
, semialign-indexed >= 1 && < 1.2
Expand All @@ -448,6 +453,8 @@ library
, unordered-containers >= 0.2.9 && < 0.3
, vector >= 0.12.0 && < 0.13
, xml >= 1.3.14 && < 1.4
default-extensions:
OverloadedStrings
if flag(optimize)
default-extensions:
ApplicativeDo
Expand Down Expand Up @@ -491,13 +498,20 @@ executable hnix
, pretty-show
, prettyprinter
, ref-tf
, relude
, repline >= 0.4.0.0 && < 0.5
, serialise
, template-haskell
, text
, time
, transformers
, unordered-containers
mixins:
base hiding (Prelude)
, relude
, relude (Relude as Prelude)
default-extensions:
OverloadedStrings
if flag(optimize)
default-extensions:
ApplicativeDo
Expand All @@ -522,6 +536,10 @@ test-suite hnix-tests
ReduceExprTests
TestCommon
Paths_hnix
mixins:
base hiding (Prelude)
, relude
, relude (Relude as Prelude)
hs-source-dirs:
tests
ghc-options: -Wall -threaded
Expand All @@ -546,6 +564,7 @@ test-suite hnix-tests
, pretty-show
, prettyprinter
, process
, relude
, split
, tasty
, tasty-hedgehog
Expand All @@ -558,6 +577,8 @@ test-suite hnix-tests
, transformers
, unix
, unordered-containers
default-extensions:
OverloadedStrings
if flag(optimize)
default-extensions:
ApplicativeDo
Expand All @@ -574,6 +595,10 @@ benchmark hnix-benchmarks
Paths_hnix
hs-source-dirs:
benchmarks
mixins:
base hiding (Prelude)
, relude
, relude (Relude as Prelude)
ghc-options: -Wall
build-depends:
base
Expand All @@ -588,12 +613,15 @@ benchmark hnix-benchmarks
, hnix
, mtl
, optparse-applicative
, relude
, serialise
, template-haskell
, text
, time
, transformers
, unordered-containers
default-extensions:
OverloadedStrings
if flag(optimize)
default-extensions:
ApplicativeDo
Expand Down
12 changes: 5 additions & 7 deletions main/Main.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
Expand All @@ -10,17 +9,18 @@

module Main where

import Nix.Utils
import Control.Comonad ( extract )
import qualified Control.DeepSeq as Deep
import qualified Control.Exception as Exc
import Control.Monad
import GHC.Err ( errorWithoutStackTrace )
import Control.Monad.Catch
import System.IO ( hPutStrLn, getContents )
import Control.Monad.Free
import Control.Monad.IO.Class
import qualified Data.HashMap.Lazy as M
import qualified Data.Map as Map
import Data.List ( sortOn )
import Data.Maybe ( fromJust )
import qualified Data.String as String
import Data.Time
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
Expand All @@ -34,15 +34,13 @@ import Nix.Standard
import Nix.Thunk.Basic
import qualified Nix.Type.Env as Env
import qualified Nix.Type.Infer as HM
import Nix.Utils
import Nix.Var
import Nix.Value.Monad
import Options.Applicative hiding ( ParserResult(..) )
import Prettyprinter hiding ( list )
import Prettyprinter.Render.Text
import qualified Repl
import System.FilePath
import System.IO
import qualified Text.Show.Pretty as PS

main :: IO ()
Expand All @@ -67,7 +65,7 @@ main = do
)
(\ x ->
-- We can start use Text as in the base case, requires changing FilePath -> Text
traverse_ (processFile opts) . lines =<< liftIO
traverse_ (processFile opts) . String.lines =<< liftIO
(case x of
"-" -> getContents -- get user input
_path -> readFile _path
Expand Down
14 changes: 5 additions & 9 deletions main/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
Expand All @@ -32,16 +31,13 @@ import qualified Data.Maybe as Maybe
import qualified Data.HashMap.Lazy
import Data.Char ( isSpace )
import Data.List ( dropWhileEnd )
import Data.Text ( Text )
import qualified Data.String as String
import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
import Data.Version ( showVersion )
import Paths_hnix ( version )

import Control.Monad.Catch
import Control.Monad.Identity
import Control.Monad.Reader
import Control.Monad.State.Strict

import Prettyprinter ( Doc
, space
Expand Down Expand Up @@ -116,11 +112,11 @@ main' iniVal =
(\case
((prefix:command) : xs) | prefix == commandPrefix ->
do
let arguments = unwords xs
let arguments = String.unwords xs
optMatcher command options arguments
x -> cmd $ unwords x
x -> cmd $ String.unwords x
)
(words . Text.unpack <$> Text.lines f)
(String.words . Text.unpack <$> Text.lines f)

handleMissing e
| Error.isDoesNotExistError e = pure ""
Expand Down Expand Up @@ -338,7 +334,7 @@ quit _ = liftIO Exit.exitSuccess

-- | @:set@ command
setConfig :: (MonadNix e t f m, MonadIO m) => String -> Repl e t f m ()
setConfig args = case words args of
setConfig args = case String.words args of
[] -> liftIO $ putStrLn "No option to set specified"
(x:_xs) ->
case filter ((==x) . helpSetOptionName) helpSetOptions of
Expand Down
9 changes: 2 additions & 7 deletions src/Nix.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}

Expand Down Expand Up @@ -28,12 +27,8 @@ module Nix
)
where

import Control.Applicative ( Alternative )
import Control.Arrow ( second )
import Control.Monad.Reader ( MonadIO
, asks
, (<=<)
)
import Relude.Unsafe ( (!!) )
import GHC.Err ( errorWithoutStackTrace )
import Data.Fix ( Fix )
import qualified Data.HashMap.Lazy as M
import qualified Data.Text as Text
Expand Down
10 changes: 2 additions & 8 deletions src/Nix/Atoms.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

module Nix.Atoms where

#ifdef MIN_VERSION_serialise
import Codec.Serialise ( Serialise )
#endif

import Control.DeepSeq ( NFData )
import Data.Data ( Typeable
, Data)
import Data.Data ( Data)
import Data.Fixed ( mod' )
import Data.Hashable ( Hashable )
import Data.Text ( Text
, pack
import Data.Text ( pack
)
import GHC.Generics ( Generic )
import Data.Binary ( Binary )
import Data.Aeson.Types ( FromJSON
, ToJSON
Expand Down
Loading

0 comments on commit 50e63f8

Please sign in to comment.