Menu

#128 DateTime conversion to java.sql types is slow

v1.3
open
nobody
performance (1)
3
2014-10-02
2014-10-01
No

Methods on net.sourceforge.jtds.jdbc.DateTime class such as toDate(), toTime() and toTimestamp() create new instance of the java.util.GregorianCalendar. But creating java.util.GregorianCalendar is very costly operation that translates into poor performance when unpacking dates from ResultSet.
Moreover the DateTime class creates GregorianCalendar in other places such as constructors and packTime() method.

To solve the problem I suggest to re-use Calendar instance. The easiest is to use ThreadLocal to store GregorianCalendar. And that's exactly what is done in the attached patch. Another option would be to pass GregorianCalendar to DateTime when it is created but that would require to update all places where DateTime is used.
NOTE: the patch does not cleanup ThreadLocal but one can easily add a removeCalendar static method which can be called for example when underlying ResultSet is closed and will remove calendar instance from ThreadLocal.

I'm also attaching the DateTimeBenchmark class which is a micro-benchmark written using JMH framework. The results of running benchmark on original code and code with the patch included are shown below (time is nanoseconds per operation):
[-img src=DateTime_benchmark_results.png alt=Benchmark results: missing =-]

1 Attachments

Discussion

  • Anonymous

    Anonymous - 2014-10-01

    Patch and benchmark results attached

     
  • Anonymous

    Anonymous - 2014-10-01

    Benchmark results:

     
  • Anonymous

    Anonymous - 2014-10-01

    The patch was created against DateTime class from version 1.3.1.

     
  • Anonymous

    Anonymous - 2014-10-02

    Attached GregorianCalendar.patch that replaces all occurrences of the new GregorianCalendar calls inside jTDS 1.3.1 code, which is only DateTime and Support classes.

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.