Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decimal is quadratic #217

Open
phadej opened this issue Jun 9, 2023 · 2 comments
Open

decimal is quadratic #217

phadej opened this issue Jun 9, 2023 · 2 comments

Comments

@phadej
Copy link
Contributor

phadej commented Jun 9, 2023

Prelude Data.Attoparsec.Text Data.Text> fmap signum (parseOnly decimal (Data.Text.replicate 100000 "9") :: Either String Integer)
Right 1
(0.32 secs, 4,286,165,576 bytes)
Prelude Data.Attoparsec.Text Data.Text> fmap signum (parseOnly decimal (Data.Text.replicate 200000 "9") :: Either String Integer)
Right 1
(1.19 secs, 17,092,657,496 bytes)
Prelude Data.Attoparsec.Text Data.Text> fmap signum (parseOnly decimal (Data.Text.replicate 400000 "9") :: Either String Integer)
Right 1
(4.83 secs, 68,266,651,512 bytes)
Prelude Data.Attoparsec.Text Data.Text> fmap signum (parseOnly decimal (Data.Text.replicate 800000 "9") :: Either String Integer)
Right 1
(18.86 secs, 272,858,846,272 bytes)

when doubling the size of input the time quadruples.

Tested using cabal build --write-ghc-environment-files=always, and then loading the library into repl. So the important parts are not interpreted.

In comparison, read is almost instant

Prelude Data.Attoparsec.Text Data.Text> signum (read (Prelude.replicate 800000 '9') :: Integer)
1
(0.21 secs, 333,060,168 bytes)
phadej added a commit to haskell/aeson that referenced this issue Jun 9, 2023
More fore: Drop `attoparsec` dependency alltogether.

We parse scientific from Text manually now.
Notice, `scientific` parser in `attoparsec` is quadratic (uses
`decimal`).
haskell/attoparsec#217
phadej added a commit to haskell/aeson that referenced this issue Jun 9, 2023
More fore: Drop `attoparsec` dependency alltogether.

We parse scientific from Text manually now.
Notice, `scientific` parser in `attoparsec` is quadratic (uses
`decimal`).
haskell/attoparsec#217
phadej added a commit to haskell/aeson that referenced this issue Jun 9, 2023
More fore: Drop `attoparsec` dependency alltogether.

We parse scientific from Text manually now.
Notice, `scientific` parser in `attoparsec` is quadratic (uses
`decimal`).
haskell/attoparsec#217
phadej added a commit to haskell/aeson that referenced this issue Jun 9, 2023
More fore: Drop `attoparsec` dependency alltogether.

We parse scientific from Text manually now.
Notice, `scientific` parser in `attoparsec` is quadratic (uses
`decimal`).
haskell/attoparsec#217
@andreasabel
Copy link
Member

I suppose this is this function?

-- | Parse and decode an unsigned decimal number.
decimal :: Integral a => Parser a
decimal = T.foldl' step 0 `fmap` takeWhile1 isDecimal
where step a c = a * 10 + fromIntegral (ord c - 48)

@phadej
Copy link
Contributor Author

phadej commented Jul 12, 2023

@andreasabel yes, but also other versions in attoparsec.

JonathanLorimer pushed a commit to JonathanLorimer/aeson that referenced this issue Aug 7, 2023
More fore: Drop `attoparsec` dependency alltogether.

We parse scientific from Text manually now.
Notice, `scientific` parser in `attoparsec` is quadratic (uses
`decimal`).
haskell/attoparsec#217
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants