From d618ef416bd9d9a2862c1bf89b5fdc391aa3713c Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Fri, 12 Jul 2024 15:40:52 -0500 Subject: [PATCH 1/3] Use toEncoding for toPersistValueJSON This should give us a performance benefit for free. Instead of creating a bunch of intermediate values and serializing them, we can just go straight to serialization with toEncoding. No new constraints are needed because ToJSON provides a default toEncoding. --- persistent/Database/Persist/Class/PersistEntity.hs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/persistent/Database/Persist/Class/PersistEntity.hs b/persistent/Database/Persist/Class/PersistEntity.hs index 81d019339..294479c8e 100644 --- a/persistent/Database/Persist/Class/PersistEntity.hs +++ b/persistent/Database/Persist/Class/PersistEntity.hs @@ -53,9 +53,10 @@ import Data.Aeson , (.=) ) import qualified Data.Aeson.Parser as AP -import Data.Aeson.Text (encodeToTextBuilder) +import Data.Aeson.Encoding (encodingToLazyByteString) import Data.Aeson.Types (Parser, Result(Error, Success)) import Data.Attoparsec.ByteString (parseOnly) +import qualified Data.ByteString as B import Data.Functor.Identity import Web.PathPieces (PathMultiPiece(..), PathPiece(..)) @@ -71,8 +72,6 @@ import Data.Maybe (isJust) import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.Encoding as TE -import qualified Data.Text.Lazy as LT -import qualified Data.Text.Lazy.Builder as TB import GHC.Generics import GHC.OverloadedLabels import GHC.TypeLits @@ -452,7 +451,7 @@ idField = "_id" -- toPersistValue = toPersistValueJSON -- @ toPersistValueJSON :: ToJSON a => a -> PersistValue -toPersistValueJSON = PersistText . LT.toStrict . TB.toLazyText . encodeToTextBuilder . toJSON +toPersistValueJSON = PersistByteString . B.toStrict . encodingToLazyByteString . toEncoding -- | Convenience function for getting a free 'PersistField' instance -- from a type with JSON instances. The JSON parser used will accept JSON From 078d80ad92a939e1aadb09b89dcb710a57fb4c53 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Fri, 12 Jul 2024 15:46:03 -0500 Subject: [PATCH 2/3] Add changelog --- persistent/ChangeLog.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/persistent/ChangeLog.md b/persistent/ChangeLog.md index ceb25f41a..7e3d9902c 100644 --- a/persistent/ChangeLog.md +++ b/persistent/ChangeLog.md @@ -1,5 +1,12 @@ # Changelog for persistent +## Unreleased + +* [#1541](https://github.com/yesodweb/persistent/pull/1541) + * `toPersistValueJSON` now uses `ToJSON`’s `toEncoding` instead of + `toJSON`. This provides a performance benefit because Aeson no + longer needs to construct intermediate `Value`s. + ## 2.14.6.1 * [#1528](https://github.com/yesodweb/persistent/pull/1528) From cb292f1c562396009b940c23bf6d817731d4dddf Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Fri, 12 Jul 2024 21:42:20 -0500 Subject: [PATCH 3/3] Update persistent/Database/Persist/Class/PersistEntity.hs Co-authored-by: Matt Parsons --- persistent/Database/Persist/Class/PersistEntity.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistent/Database/Persist/Class/PersistEntity.hs b/persistent/Database/Persist/Class/PersistEntity.hs index 294479c8e..58f47deb9 100644 --- a/persistent/Database/Persist/Class/PersistEntity.hs +++ b/persistent/Database/Persist/Class/PersistEntity.hs @@ -451,7 +451,7 @@ idField = "_id" -- toPersistValue = toPersistValueJSON -- @ toPersistValueJSON :: ToJSON a => a -> PersistValue -toPersistValueJSON = PersistByteString . B.toStrict . encodingToLazyByteString . toEncoding +toPersistValueJSON = PersistText . LT.toStrict . encodeToLazyText -- | Convenience function for getting a free 'PersistField' instance -- from a type with JSON instances. The JSON parser used will accept JSON