From ad598e75dd26bdcf3db91912e285e829d7485c32 Mon Sep 17 00:00:00 2001 From: adiR28 Date: Thu, 20 Jul 2023 12:24:38 +0530 Subject: [PATCH 1/2] flatparse version update --- src/Time.hs | 12 +++++------ src/Time/Parser.hs | 52 +++++++++++++++++++++++----------------------- stack.yaml | 4 +++- stack.yaml.lock | 17 ++++++++++----- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/Time.hs b/src/Time.hs index b74fefe..739f54c 100644 --- a/src/Time.hs +++ b/src/Time.hs @@ -5,14 +5,14 @@ module Time , parseTime , parseLTime ) where - + import Data.Text (Text) import Data.Time (UTCTime, LocalTime) -import FlatParse.Basic (Result(..), runParserS) +import qualified FlatParse.Basic as FB import Time.Parser as X parseTime :: Text -> String -> Maybe UTCTime -parseTime format stringifiedDate = go $ runParserS parser stringifiedDate +parseTime format stringifiedDate = go $ FB.runParser parser $ FB.strToUtf8 stringifiedDate where parser | format == "%Y-%m-%dT%k:%M:%SZ" || @@ -24,15 +24,15 @@ parseTime format stringifiedDate = go $ runParserS parser stringifiedDate dayParser | format == "%d%m%Y" = dayParser' | otherwise = parserUTCTime - go (OK r _) = Just r + go (FB.OK r _) = Just r go _ = Nothing parseLTime :: Text -> String -> Maybe LocalTime -parseLTime format stringifiedDate = go $ runParserS parser stringifiedDate +parseLTime format stringifiedDate = go $ FB.runParser parser $ FB.strToUtf8 stringifiedDate where parser | format == "%Y-%m-%dT%k:%M:%S%Q%Ez" = parserLocalTime | otherwise = parserLocalTime - go (OK r _) = Just r + go (FB.OK r _) = Just r go _ = Nothing diff --git a/src/Time/Parser.hs b/src/Time/Parser.hs index dd368e9..76bfa01 100644 --- a/src/Time/Parser.hs +++ b/src/Time/Parser.hs @@ -7,7 +7,7 @@ module Time.Parser ) where import Data.Time -import FlatParse.Basic +import qualified FlatParse.Basic as FB import Data.Fixed (Pico) -- | Custom parser for the format @@ -15,7 +15,7 @@ import Data.Fixed (Pico) -- %Y-%m-%dT%k:%M:%S%QZ -- %Y-%m-%dT%k:%M:%S%Q%Ez -parserLocalTime :: Parser e LocalTime +parserLocalTime :: FB.Parser e LocalTime parserLocalTime = do (year, month, day, hr, minutes, sec, msec, curLen) <- basicParserUtil let curLen' = if curLen < 0 then 0 @@ -26,7 +26,7 @@ parserLocalTime = do (fromGregorian year month day) (TimeOfDay (fromIntegral hr :: Int) (fromIntegral minutes :: Int) sec') -parserUTCTime :: Parser e UTCTime +parserUTCTime :: FB.Parser e UTCTime parserUTCTime = do (year, month, day, hr, minutes, sec, msec, curLen) <- basicParserUtil pure $ @@ -34,7 +34,7 @@ parserUTCTime = do (fromGregorian year month day) (picosecondsToDiffTime (((hr * 60 * 60) + (minutes * 60) + sec) * (10 ^ 12) + (msec * (10 ^ (12 - curLen))))) -dayParser :: Parser e UTCTime +dayParser :: FB.Parser e UTCTime dayParser = do (year, month, day) <- dateParser pure $ @@ -43,39 +43,39 @@ dayParser = do (secondsToDiffTime 0) -- Added parser for handling date for this format --> "%d%m%Y" -dayParser' :: Parser e UTCTime +dayParser' :: FB.Parser e UTCTime dayParser' = do - day <- isolate 2 readInt - month <- isolate 2 readInt - year <- isolate 4 readInteger + day <- FB.isolate 2 FB.anyAsciiDecimalInt + month <- FB.isolate 2 FB.anyAsciiDecimalInt + year <- FB.isolate 4 FB.anyAsciiDecimalInteger pure $ UTCTime (fromGregorian year month day) (secondsToDiffTime 0) -- | utils -dateParser :: Parser e (Integer, Int, Int) +dateParser :: FB.Parser e (Integer, Int, Int) dateParser = do - year <- readInteger - satisfy_ (\x -> x == '-' || x == ' ' || x == '/') - month <- readInt - satisfy_ (\x -> x == '-' || x == ' ' || x == '/') - day <- readInt + year <- FB.anyAsciiDecimalInteger + _ <- FB.satisfy (\x -> x == '-' || x == ' ' || x == '/') + month <- FB.anyAsciiDecimalInt + _ <- FB.satisfy (\x -> x == '-' || x == ' ' || x == '/') + day <- FB.anyAsciiDecimalInt pure (year, month, day) -- common function for parsing time -basicParserUtil :: Parser e (Integer, Int, Int, Integer, Integer, Integer, Integer, Int) +basicParserUtil :: FB.Parser e (Integer, Int, Int, Integer, Integer, Integer, Integer, Int) basicParserUtil = do (year, month, day) <- dateParser - satisfy_ (\x -> x == '-' || x == ' ' || x == 'T') - many_ $(char ' ') - hr <- readInteger - $(char ':') - minutes <- readInteger - $(char ':') - sec <- readInteger - Pos curS <- getPos - msec <- ($(char '.') *> readInteger) <|> pure 0 - Pos curE <- getPos - try $ $(char 'Z') <|> pure () + _ <- FB.satisfy (\x -> x == '-' || x == ' ' || x == 'T') + _ <- FB.many $(FB.char ' ') + hr <- FB.anyAsciiDecimalInteger + $(FB.char ':') + minutes <- FB.anyAsciiDecimalInteger + $(FB.char ':') + sec <- FB.anyAsciiDecimalInteger + FB.Pos curS <- FB.getPos + msec <- ($(FB.char '.') *> FB.anyAsciiDecimalInteger) FB.<|> pure 0 + FB.Pos curE <- FB.getPos + FB.try $ $(FB.char 'Z') FB.<|> pure () pure (year, month, day, hr, minutes, sec, msec, curS - curE -1) diff --git a/stack.yaml b/stack.yaml index 188f23c..1005b01 100644 --- a/stack.yaml +++ b/stack.yaml @@ -35,7 +35,9 @@ packages: # forks / in-progress versions pinned to a git hash. For example: # extra-deps: - - flatparse-0.3.5.1@sha256:f10369df044cf9782dfd1ce7bd865f2d3e2ea7d71cc0040df62b3ac921021a45,3936 + # - flatparse-0.3.5.1@sha256:f10369df044cf9782dfd1ce7bd865f2d3e2ea7d71cc0040df62b3ac921021a45,3936 +- flatparse-0.4.1.0 +- utf8-string-1.0.2@sha256:79416292186feeaf1f60e49ac5a1ffae9bf1b120e040a74bf0e81ca7f1d31d3f,1538 # - acme-missiles-0.3 # - git: https://github.com/commercialhaskell/stack.git # commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a diff --git a/stack.yaml.lock b/stack.yaml.lock index 98b06d8..e83bb4c 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -5,15 +5,22 @@ packages: - completed: - hackage: flatparse-0.3.5.1@sha256:f10369df044cf9782dfd1ce7bd865f2d3e2ea7d71cc0040df62b3ac921021a45,3936 + hackage: flatparse-0.4.1.0@sha256:b3cc2e58a63d9010ceb34596c97ff1e5efd40d9d429d16a1673be92212903b15,4537 pantry-tree: - size: 1062 - sha256: e0d48ff528737de7351bd3d491ad6913bbd4aca0b9957386512820a0f09eedd5 + sha256: 281402986d640a07f104f017bf3b87ffa38548a69e2e481c8fcac638fe107d06 + size: 2341 original: - hackage: flatparse-0.3.5.1@sha256:f10369df044cf9782dfd1ce7bd865f2d3e2ea7d71cc0040df62b3ac921021a45,3936 + hackage: flatparse-0.4.1.0 +- completed: + hackage: utf8-string-1.0.2@sha256:79416292186feeaf1f60e49ac5a1ffae9bf1b120e040a74bf0e81ca7f1d31d3f,1538 + pantry-tree: + sha256: 0863c3c9b6ee24dd38873c62e38ac46dd50aff2ea6b362beadf7f9b4a273c75f + size: 601 + original: + hackage: utf8-string-1.0.2@sha256:79416292186feeaf1f60e49ac5a1ffae9bf1b120e040a74bf0e81ca7f1d31d3f,1538 snapshots: - completed: + sha256: d6b004b095fe2a0b8b14fbc30014ee97e58843b9c9362ddb9244273dda62649e size: 532380 url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/16.yaml - sha256: d6b004b095fe2a0b8b14fbc30014ee97e58843b9c9362ddb9244273dda62649e original: lts-16.16 From 3c0b4ad5b165a7734a6f9b3b872993c799e91182 Mon Sep 17 00:00:00 2001 From: adiR28 Date: Fri, 21 Jul 2023 11:49:15 +0530 Subject: [PATCH 2/2] update readme with benchmark results --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 14caae9..d09b1f6 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,9 @@ | Benchmark Name | Description | Data.Time Library | Fast-time | | ----- | ----- | ----- | ----- | -| UTCTime | Converts the text time to UTCTime format. | 8.382 μs (8.359 μs .. 8.425 μs) | 734.4 ns (731.7 ns .. 736.4 ns) | -| UTCTime Milliseconds | Converts the text time which includes millisecond to UTCTime format. | 11.28 μs (11.24 μs .. 11.33 μs) | 806.6 ns (804.4 ns .. 808.8 ns) | -| LocalTime | Converts the text time to Local time covering case of millisecond parsing as well. | 11.67 μs (11.53 μs .. 11.81 μs | 1.200 μs (1.180 μs .. 1.220 μs) | -| Date Parsing | Converts the text date in the given format. | 4.710 μs (4.296 μs .. 5.211 μs) | 440.3 ns (437.4 ns .. 443.6 ns) | -| Date Parsing ("%d%m%Y") | Converts the text date to the given format using isolate method of flatparse library. | 12.08 μs (11.81 μs .. 12.29 μs) | 368.1 ns (359.7 ns .. 378.0 ns) | +| UTCTime | Converts the text time to UTCTime format. | 8.382 μs (8.359 μs .. 8.425 μs) | 648.4 ns (647.7 ns .. 649.4 ns) | +| UTCTime Milliseconds | Converts the text time which includes millisecond to UTCTime format. | 12.89 μs (12.88 μs .. 12.93 μs) | 669.4 ns (668.8 ns .. 670.5 ns) | +| LocalTime | Converts the text time to Local time covering case of millisecond parsing as well. | 12.72 μs (12.71 μs .. 12.74 μs) | 1.027 μs (1.026 μs .. 1.027 μs) | +| Date Parsing | Converts the text date in the given format. | 8.511 μs (8.501 μs .. 8.535 μs) | 409.1 ns (408.8 ns .. 410.5 ns) | +| Date Parsing ("%d%m%Y") | Converts the text date to the given format using isolate method of flatparse library. | 14.78 μs (14.77 μs .. 14.80 μs) | 371.9 ns (371.8 ns .. 372.1 ns) |