From 53fb449b008e9b6aed9877b9d33f4026e454e0f9 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Thu, 17 Feb 2022 00:41:36 +0100 Subject: sprinkle some NFData everywhere (also some evaluateNF, leading to slightly less memory usage) --- lib/Tiled.hs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'lib/Tiled.hs') diff --git a/lib/Tiled.hs b/lib/Tiled.hs index bca5f1a..ab7d4f4 100644 --- a/lib/Tiled.hs +++ b/lib/Tiled.hs @@ -1,5 +1,7 @@ {-# LANGUAGE AllowAmbiguousTypes #-} +{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} @@ -15,6 +17,7 @@ -- http://doc.mapeditor.org/en/latest/reference/tmx-map-format/ module Tiled where +import Control.DeepSeq (NFData) import Control.Exception (try) import Control.Exception.Base (SomeException) import Data.Aeson hiding (Object) @@ -43,26 +46,26 @@ aesonOptions l = defaultOptions -- | A globally indexed identifier. newtype GlobalId = GlobalId { unGlobalId :: Int } - deriving (Ord, Eq, Enum, Num, Generic, Show, FromJSON, ToJSON, FromJSONKey, ToJSONKey) + deriving newtype (Ord, Eq, Enum, Num, Show, FromJSON, ToJSON, FromJSONKey, ToJSONKey, NFData) mkTiledId :: Int -> GlobalId mkTiledId i = GlobalId { unGlobalId = i } -- | A locally indexed identifier. newtype LocalId = LocalId { unLocalId :: Int } - deriving (Ord, Eq, Enum, Num, Generic, Show, FromJSON, ToJSON, FromJSONKey, ToJSONKey) + deriving newtype (Ord, Eq, Enum, Num, Show, FromJSON, ToJSON, FromJSONKey, ToJSONKey, NFData) type Color = Text -- | A custom tiled property, which just has a name and a value. data Property = Property Text PropertyValue - deriving (Eq, Generic, Show) + deriving (Eq, Generic, Show, NFData) -- | The value of a custom tiled property. -- It is strongly typed via a tag in the json representation, -- and needs a custom ToJSON and FromJSON instance because of that. data PropertyValue = StrProp Text | BoolProp Bool | IntProp Int | FloatProp Float - deriving (Eq, Generic, Show) + deriving (Eq, Generic, Show, NFData) instance IsString PropertyValue where fromString s = StrProp (T.pack s) @@ -111,7 +114,7 @@ instance ToJSON Property where data Point = Point { pointX :: Double , pointY :: Double - } deriving (Eq, Generic, Show) + } deriving (Eq, Generic, Show, NFData) instance FromJSON Point where parseJSON = genericParseJSON (aesonOptions 5) @@ -178,7 +181,7 @@ data Object = ObjectRectangle , objectHeight :: Maybe Double , objectEllipse :: Maybe Bool , objectType :: Text - } deriving (Eq, Generic, Show) + } deriving (Eq, Generic, Show, NFData) @@ -226,7 +229,7 @@ data Layer = Layer { layerWidth :: Maybe Double , layerStartX :: Maybe Int , layerStartY :: Maybe Int , layerColor :: Maybe Color - } deriving (Eq, Generic, Show) + } deriving (Eq, Generic, Show, NFData) instance FromJSON Layer where parseJSON = genericParseJSON (aesonOptions 5) @@ -238,7 +241,7 @@ data Terrain = Terrain { terrainName :: String -- ^ Name of terrain , terrainTile :: LocalId -- ^ Local ID of tile representing terrain - } deriving (Eq, Generic, Show) + } deriving (Eq, Generic, Show, NFData) instance FromJSON Terrain where parseJSON (A.Object o) = Terrain <$> o .: "name" @@ -254,7 +257,7 @@ instance ToJSON Terrain where data Frame = Frame { frameDuration :: Int , frameTileId :: LocalId - } deriving (Eq, Generic, Show) + } deriving (Eq, Generic, Show, NFData) instance FromJSON Frame where parseJSON (A.Object o) = Frame <$> o .: "duration" @@ -277,7 +280,7 @@ data Tile = Tile { tileId :: Int , tileProbability :: Maybe Float , tileType :: Maybe Text , tileTerrain :: Maybe [Int] - } deriving (Eq, Generic, Show) + } deriving (Eq, Generic, Show, NFData) instance FromJSON Tile where parseJSON = genericParseJSON (aesonOptions 4) @@ -331,10 +334,10 @@ data Tileset = Tileset { tilesetFirstgid :: GlobalId , tilesetWangsets :: Maybe Value , tilesetType :: Maybe Text , tilesetFileName :: Maybe Text - } deriving (Eq, Generic, Show) + } deriving (Eq, Generic, Show, NFData) newtype TransitiveTilesetMap = TransitiveTilesetMap (Map LocalId Value) - deriving (Show, Eq, Generic, FromJSON) + deriving newtype (Show, Eq, FromJSON) instance FromJSON Tileset where parseJSON = genericParseJSON (aesonOptions 7) @@ -378,7 +381,7 @@ data Tiledmap = Tiledmap { tiledmapVersion :: Value , tiledmapStaggerindex :: Maybe String , tiledmapType :: String , tiledmapEditorsettings :: Maybe Value - } deriving (Eq, Generic, Show) + } deriving (Eq, Generic, Show, NFData) instance FromJSON Tiledmap where parseJSON = genericParseJSON (aesonOptions 8) -- cgit v1.2.3