Uploaded image for project: 'Lucene.Net'
  1. Lucene.Net
  2. LUCENENET-495

Use of DateTime.Now causes huge amount of System.Globalization.DaylightTime object allocations



    • Type: Bug
    • Status: Resolved
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: Lucene.Net 2.9.4, Lucene.Net 3.0.3
    • Fix Version/s: Lucene.Net 3.0.3
    • Component/s: Lucene.Net Core
    • Labels:


      This issue mostly just affects RAMDirectory. However, RAMFile and RAMOutputStream are used in other (all?) directory implementations, including FSDirectory types.

      In RAMOutputStream, the file last modified property for the RAMFile is updated when the stream is flushed. It's calculated using DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond. I've read before that Microsoft has regretted making DateTime.Now a property instead of a method, and after seeing what it's doing, I'm starting to understand why. DateTime.Now is returning local time. In order for it to calculate that, it has to get the utf offset for the machine, which requires the creation of a class, System.Globalization.DaylightTime. This is bad for performance.

      Using code to write 10,000 small documents to an index (4kb sizes), it created 1,570,157 of these DaylightTime classes, a total of 62MB of extra memory...clearly RAMOutputStream.Flush() is called a lot.

      A fix I'd like to propose is to change the RAMFile from storing the LastModified date to UTC instead of local. DateTime.UtcNow doesn't create any additional objects and is very fast. For this small benchmark, the performance increase is 31%.

      I've set it to convert to local-time, when RAMDirectory.LastModified(string name) is called to make sure it has the same behavior (tests fail otherwise). Are there any other side-effects to making this change?




            • Assignee:
              ccurrens Christopher Calvin Currens
              ccurrens Christopher Calvin Currens
            • Votes:
              0 Vote for this issue
              2 Start watching this issue


              • Created: