Author: rfm
Date: Tue Apr 28 18:47:54 2015
New Revision: 38465

URL: http://svn.gna.org/viewcvs/gnustep?rev=38465&view=rev
Log:
Improve absolute time zone caching

Modified:
    libs/base/trunk/ChangeLog
    libs/base/trunk/Source/NSTimeZone.m

Modified: libs/base/trunk/ChangeLog
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/ChangeLog?rev=38465&r1=38464&r2=38465&view=diff
==============================================================================
--- libs/base/trunk/ChangeLog   (original)
+++ libs/base/trunk/ChangeLog   Tue Apr 28 18:47:54 2015
@@ -1,3 +1,7 @@
+2015-04-20  Richard Frith-Macdonald <[email protected]>
+
+       * Source/NSTimeZone.m: Improve caching of common absolute timezones.
+
 2015-04-28  Niels Grewe <[email protected]>
 
        * Source/NSDateFormatter.m: Properly reinstall the user provided

Modified: libs/base/trunk/Source/NSTimeZone.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/NSTimeZone.m?rev=38465&r1=38464&r2=38465&view=diff
==============================================================================
--- libs/base/trunk/Source/NSTimeZone.m (original)
+++ libs/base/trunk/Source/NSTimeZone.m Tue Apr 28 18:47:54 2015
@@ -222,6 +222,7 @@
  */
 static GSPlaceholderTimeZone   *defaultPlaceholderTimeZone;
 static NSMapTable              *placeholderMap;
+static GSAbsTimeZone            *commonAbsolutes[145] = { 0 };
 
 /*
  * Temporary structure for holding time zone details.
@@ -703,6 +704,17 @@
     }
   anOffset *= sign;
 
+  if (anOffset % 900 == 0)
+    {
+      z = commonAbsolutes[anOffset/900 + 72];
+      if (z != nil)
+        {
+          IF_NO_GC(RETAIN(z));
+          DESTROY(self);
+          return z;
+        }
+    }
+
   if (zone_mutex != nil)
     {
       [zone_mutex lock];
@@ -747,6 +759,15 @@
       z = self;
       NSMapInsert(absolutes, (void*)(uintptr_t)anOffset, (void*)z);
       [zoneDictionary setObject: self forKey: (NSString*)name];
+    }
+  if (anOffset % 900 == 0)
+    {
+      int       index = anOffset/900 + 72;
+
+      if (nil == commonAbsolutes[index])
+        {
+          commonAbsolutes[index] = RETAIN(self);
+        }
     }
   if (zone_mutex != nil)
     {
@@ -1927,9 +1948,50 @@
 + (NSTimeZone*) timeZoneForSecondsFromGMT: (NSInteger)seconds
 {
   NSTimeZone   *zone;
-
-  zone = [[GSAbsTimeZone alloc] initWithOffset: seconds name: nil];
-  return AUTORELEASE(zone);
+  int          sign = seconds >= 0 ? 1 : -1;
+  int           extra;
+
+  /*
+   * Round the offset to the nearest minute, (for MacOS-X compatibility)
+   * and ensure it is no more than 18 hours.
+   */
+  seconds *= sign;
+  extra = seconds % 60;
+  if (extra < 30)
+    {
+      seconds -= extra;
+    }
+  else
+    {
+      seconds += 60 - extra;
+    }
+  if (seconds > 64800)
+    {
+      return nil;
+    }
+  seconds *= sign;
+  if (seconds % 900 == 0)
+    {
+      zone = commonAbsolutes[seconds/900 + 72];
+    }
+  else
+    {
+      if (zone_mutex != nil)
+        {
+          [zone_mutex lock];
+        }
+      zone = (NSTimeZone*)NSMapGet(absolutes, (void*)(uintptr_t)seconds);
+      if (zone_mutex != nil)
+        {
+          [zone_mutex unlock];
+        }
+    }
+  if (nil == zone)
+    {
+      zone = [[GSAbsTimeZone alloc] initWithOffset: seconds name: nil];
+      zone = AUTORELEASE(zone);
+    }
+  return zone;
 }
 
 /**


_______________________________________________
Gnustep-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to