haskell / attoparsec

A fast Haskell library for parsing ByteStrings
http://hackage.haskell.org/package/attoparsec
Other
513 stars 93 forks source link

decimal is quadratic #217

Open phadej opened 1 year ago

phadej commented 1 year ago
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)
andreasabel commented 1 year ago

I suppose this is this function? https://github.com/haskell/attoparsec/blob/c7ffdbe4e3e92c5c04bfc3a1467d1e2f00668e7b/Data/Attoparsec/Text.hs#L312-L315

phadej commented 1 year ago

@andreasabel yes, but also other versions in attoparsec.