Bug 849785 - Avoid repeated computation of local standard time vs. UTC time difference. r=waldo, a=lsblakk
authorBrian Hackett <bhackett1024@gmail.com>
Mon, 11 Mar 2013 19:59:58 -0600
changeset 132379 a64ef5e8030c528d57b917aa7b1bcbe84d5492ef
parent 132378 333a93611b4aabe1ef52ffb5abc72d68d42c0a9c
child 132380 3c95ca69e1d7a24a7dba7dfff4f3b79b7f386117
push id2323
push userbbajaj@mozilla.com
push dateMon, 01 Apr 2013 19:47:02 +0000
treeherdermozilla-beta@7712be144d91 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo, lsblakk
bugs849785
milestone21.0a2
Bug 849785 - Avoid repeated computation of local standard time vs. UTC time difference. r=waldo, a=lsblakk
js/src/vm/DateTime.cpp
js/src/vm/DateTime.h
--- a/js/src/vm/DateTime.cpp
+++ b/js/src/vm/DateTime.cpp
@@ -122,17 +122,23 @@ UTCToLocalStandardOffsetSeconds()
     // Otherwise we have more local seconds, so move the UTC seconds into the
     // local seconds' frame of reference and then subtract.
     return local_secs - (utc_secs + SecondsPerDay);
 }
 
 void
 js::DateTimeInfo::updateTimeZoneAdjustment()
 {
-    double newTZA = UTCToLocalStandardOffsetSeconds() * msPerSecond;
+    /*
+     * The difference between local standard time and UTC will never change for
+     * a given time zone.
+     */
+    utcToLocalStandardOffsetSeconds = UTCToLocalStandardOffsetSeconds();
+
+    double newTZA = utcToLocalStandardOffsetSeconds * msPerSecond;
     if (newTZA == localTZA_)
         return;
 
     localTZA_ = newTZA;
 
     /*
      * The initial range values are carefully chosen to result in a cache miss
      * on first use given the range of possible values.  Be careful to keep
@@ -172,17 +178,17 @@ js::DateTimeInfo::computeDSTOffsetMillis
     // systems without this call.
     _tzset();
 #endif
 
     struct tm tm;
     if (!ComputeLocalTime(static_cast<time_t>(utcSeconds), &tm))
         return 0;
 
-    int32_t dayoff = int32_t((utcSeconds + UTCToLocalStandardOffsetSeconds()) % SecondsPerDay);
+    int32_t dayoff = int32_t((utcSeconds + utcToLocalStandardOffsetSeconds) % SecondsPerDay);
     int32_t tmoff = tm.tm_sec + (tm.tm_min * SecondsPerMinute) + (tm.tm_hour * SecondsPerHour);
 
     int32_t diff = tmoff - dayoff;
 
     if (diff < 0)
         diff += SecondsPerDay;
 
     return diff * msPerSecond;
--- a/js/src/vm/DateTime.h
+++ b/js/src/vm/DateTime.h
@@ -134,16 +134,22 @@ class DateTimeInfo
     int64_t computeDSTOffsetMilliseconds(int64_t utcSeconds);
 
     int64_t offsetMilliseconds;
     int64_t rangeStartSeconds, rangeEndSeconds; // UTC-based
 
     int64_t oldOffsetMilliseconds;
     int64_t oldRangeStartSeconds, oldRangeEndSeconds; // UTC-based
 
+    /*
+     * Cached offset in seconds from the current UTC time to the current
+     * local standard time (i.e. not including any offset due to DST).
+     */
+    int32_t utcToLocalStandardOffsetSeconds;
+
     static const int64_t MaxUnixTimeT = 2145859200; /* time_t 12/31/2037 */
 
     static const int64_t RangeExpansionAmount = 30 * SecondsPerDay;
 
     void sanityCheck();
 };
 
 }  /* namespace js */