Giter Site home page Giter Site logo

UTCTime serialisation slow about cborg HOT 16 CLOSED

well-typed avatar well-typed commented on August 16, 2024
UTCTime serialisation slow

from cborg.

Comments (16)

teh avatar teh commented on August 16, 2024

The actual format is part of the standard: https://tools.ietf.org/html/rfc7049#section-2.4.1 so there's probably no wiggle room in changing that to the nicer UTCTime day/time format. I tried writing a faster encoder but time conversions are slow. Will probably have to newtype with custom serialise :/

from cborg.

arianvp avatar arianvp commented on August 16, 2024

You could newtype it and use the same serialisation format as the binary package?

from cborg.

thoughtpolice avatar thoughtpolice commented on August 16, 2024

It'd be interesting to see where you got your Binary instances from - you took an Orphan I imagine (or newtyped it) something like this I assume?

instance Binary UTCTime where
  put (UTCTime a b) = put a >> put b
  get = UTCTime <$> get <*> get

instance Binary Day where
  put (ModifiedJulianDay d) = put d
  get = ModifiedJulianDay <$> get

instance Binary DiffTime where
  put = put . fromEnum
  get = toEnum <$> get

...

or whatever? I can definitely believe that the time formatting functions are simply slow no matter what, since they have to roundtrip through String and probably a trillion other things.

Second, if you can file a synthetic benchmark or something with our criterion setup, that'd be really nice! It'd be interesting to investigate this (it's a bit hard to say exactly what could be improved without looking at a profile).

If this is an insurmountable problem, it would probably be OK to provide another function like reallyFastUTCEncoding :: UTCTime -> Encoding that does something similar, and just mention it very prominently in the tutorial and documentation that any time a UTCTime is needed, you should carefully consider which one you need. This is a really horrible workaround, though, and gets very unusable if you ever want some function overloaded on Serialise (you can't just call encode, so you'd have to take an Encoding instead or something). FWIW: A literal translation of any binary instance should always be faster, basically without exception - but this is a case where one of our instances has to be different from upstream for the stated behavior.

That said, providing the canonical instance is, IMO correct (we don't want orphans for this). It sucks that it's so slow, though.

from cborg.

teh avatar teh commented on August 16, 2024

@thoughtpolice - yea, that's pretty close: https://hackage.haskell.org/package/binary-orphans-0.1.4.0/docs/src/Data.Binary.Orphans.html

I'm trying to add a UTCTime benchmark but can't get the benchmark to compile. GHC (7.10.3) has been stuck for over an hour now compiling PkgAesonGeneric - have you seen this?

[16 of 28] Compiling Macro.PkgAesonGeneric ( bench/Macro/PkgAesonGeneric.hs, dist/build/vs-other-libs/vs-other-libs-tmp/Macro/PkgAesonGeneric.o )
23247 me       20   0 4878848 3.542g  22980 S 310.3 46.1  15:06.19 ghc                                                                                       

from cborg.

teh avatar teh commented on August 16, 2024

Removed that benchmark for now while testing. See #52. I'm totally unclear on whether that benchmark actually does what I want (with force) ..

from cborg.

thoughtpolice avatar thoughtpolice commented on August 16, 2024

The benchmark compilation performance is a known problem - see #33.

I'll take a closer look at #52. Ugh, I need to get our 32 bit support online...

from cborg.

thoughtpolice avatar thoughtpolice commented on August 16, 2024

Oh yes, and Generic might be especially bad due to some GHC bugs. I'll test for a bit.

from cborg.

dcoutts avatar dcoutts commented on August 16, 2024

Sigh. It's unfortunate that it's so slow. So we have two choices:

  1. use the standard CBOR representation for greater compatibility (e.g.cbor2json tools will know how to render them) and try and make the parser/printer faster, or
  2. go for a different representation in terms of the day + time of day. Of course we only have to handle the one standard date/time format, unlike the time lib with its general format string interpretation.

from cborg.

adamgundry avatar adamgundry commented on August 16, 2024

I suspect this is a problem for our application as well, which serializes large datasets full of UTCTimes. Would it be worth working on a higher-performance RFC 3339 parser/printer (if such a thing doesn't exist already)? That might be a nice little project that could be useful in other contexts.

Alternatively, I notice that the CBOR standard permits POSIX-style timestamps as well (tag 1). Though those don't seem to be supported by the Serialise UTCTime instance...

from cborg.

adamgundry avatar adamgundry commented on August 16, 2024

Looks like aeson might have some useful prior art.

from cborg.

thoughtpolice avatar thoughtpolice commented on August 16, 2024

Right, so Aeson uses ISO-8601 for encoding UTCTime, which is also exactly what CBOR uses. The writer bit should be pretty easy to steal, since it already uses a Builder to write out the encoded format. But the parser will be tricker as we'll have to eliminate the attoparsec dependency, I think. But the initial results of reusing that code seem promising at a glance.

from cborg.

bgamari avatar bgamari commented on August 16, 2024

We are going to move to a new, more reasonable encoding for the UTCTime instance for the release. However, this can be done in a backwards compatible way as it is tagged.

One possible encoding is two integers: days since the 0 epoch and picoseconds.

from cborg.

thoughtpolice avatar thoughtpolice commented on August 16, 2024

It looks like this will go into 0.2 regardless at this rate, so I'm queuing this for the initial release.

from cborg.

bgamari avatar bgamari commented on August 16, 2024

I have submitted an IANA request for the new tags but it will take some time for the process to take it course. I don't think we should hold the release for this.

from cborg.

thoughtpolice avatar thoughtpolice commented on August 16, 2024

Fixed by d093bb6.

from cborg.

thoughtpolice avatar thoughtpolice commented on August 16, 2024

Errr, actually fixed by cad4c72! (Ben accidentally force pushed over the last commit)

from cborg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.