Skip to content

Possible performance issue with Instant.FromUnixTimeSeconds #837

@willdean

Description

@willdean

(Using NodaTime 2.0.2 in a release-built app on netfx 4.6.1 w. VS2017)

I've been profiling an app which reads log files containing about 100000 records that include a time_t-style timestamp.

Since upgrading to NodaTime2 we've been converting these timestamps to Instant using Instant.FromUnixTimeSeconds.

It appears that this function is actually quite time-consuming, at least when it chooses to use BigInteger internally - reverting to something like _epoch.PlusNanoseconds(time_t * 1000000000L) (where _epoch is a static Instant in 1970) has just given us an enormous speed-up on the app (roughly 2:1 on the whole file processing routine, which was a considerable surprise).

It seems like Duration operator * has some logic to try and avoid BigInteger if it can, but it would appear in our case that BigInteger can be avoided but Duration has not chosen to do so.

I have not yet tried to calculate all the limits here, but is the very poor performance of BigInteger just a fact of life, and are there situations where could be avoided but isn't?

Here's a bit of DotTrace which might be useful.

nodatime

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions