Use Builder internally for representing Text

This is more efficient since the only thing we ever do is concatenate text,
which `Builder` is optimized for.
This commit is contained in:
Gabriel Gonzalez 2016-10-11 09:18:52 -07:00
parent a49bad9a58
commit 91a933ba9a
3 changed files with 12 additions and 6 deletions

View File

@ -325,6 +325,7 @@ import Prelude hiding (maybe)
import qualified Control.Exception
import qualified Data.Map
import qualified Data.Text.Lazy
import qualified Data.Text.Lazy.Builder
import qualified Data.Vector
import qualified Dhall.Core
import qualified Dhall.Import
@ -448,7 +449,7 @@ double = Type {..}
text :: Type Text
text = Type {..}
where
extract (TextLit t) = pure t
extract (TextLit t) = pure (Data.Text.Lazy.Builder.toLazyText t)
extract _ = empty
expected = Text

View File

@ -225,7 +225,7 @@ data Expr a
-- | > Text ~ Text
| Text
-- | > TextLit t ~ t
| TextLit Text
| TextLit Builder
-- | > TextAppend x y ~ x <> y
| TextAppend (Expr a) (Expr a)
-- | > TextConcat ~ Text/concat
@ -1954,7 +1954,7 @@ normalize e = case e of
App TextConcat (ListLit _ xs)
| Data.Vector.all isText xs ->
case traverse extract xs of
Just ys -> normalize (TextLit (Text.concat (toList ys)))
Just ys -> normalize (TextLit (mconcat (toList ys)))
_ -> error "normalize: Malformed `Text/concat`"
where
isText (TextLit _) = True

View File

@ -22,6 +22,7 @@ import Data.ByteString.Lazy (ByteString)
import Data.Monoid ((<>))
import Data.Text.Buildable (Buildable(..))
import Data.Text.Lazy (Text)
import Data.Text.Lazy.Builder (Builder)
import Filesystem.Path (FilePath)
import Numeric.Natural (Natural)
import Prelude hiding (FilePath)
@ -33,6 +34,7 @@ import qualified Data.Text
import qualified Data.Text.Buildable
import qualified Data.Text.Lazy
import qualified Data.Text.Lazy.Encoding
import qualified Data.Text.Lazy.Builder
import qualified Filesystem.Path.CurrentOS
import qualified NeatInterpolation
}
@ -165,8 +167,11 @@ toText :: ByteString -> Text
toText = Data.Text.Lazy.Encoding.decodeUtf8
-- TODO: Properly handle errors here
str :: ByteString -> Text
str = read . Data.Text.Lazy.unpack . Data.Text.Lazy.Encoding.decodeUtf8
str :: ByteString -> Builder
str = Data.Text.Lazy.Builder.fromLazyText
. read
. Data.Text.Lazy.unpack
. Data.Text.Lazy.Encoding.decodeUtf8
-- | `Alex` action for reading the next token
lexer :: (Token -> Alex a) -> Alex a
@ -275,7 +280,7 @@ data Token
| ListConcat
| Maybe
| MaybeFold
| TextLit Text
| TextLit Builder
| TextConcat
| Absurd
| Label Text