discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSZone pointers


From: David Chisnall
Subject: NSZone pointers
Date: Thu, 21 Jul 2011 13:01:56 +0100

Hello the list.

I said a little while ago that I was uncertain about the utility of the zone 
pointer for every object, because in most cases it would be wasting one word 
per object to store a value that is the same for every single object.

I decided to test this, with the attached diff.  Please test it and see what 
you find.  On exit, it will log the number of times zone lookup was performed 
and the number of times that it returned a zone other than the default malloc() 
zone.

In my testing, Gorm's memory usage dropped by about 2MB and it used a 
non-default zone precisely zero times.  If this is representative, then it 
would make sense to commit the NSObject portion of this diff, so we can avoid 
burning RAM for no benefit.  If, on the other hand, people are actually using 
zones a lot, then this will make their code slower and so is a bad idea.

David
--
This email complies with ISO 3103

Index: Source/NSObject.m
===================================================================
--- Source/NSObject.m   (revision 33594)
+++ Source/NSObject.m   (working copy)
@@ -409,7 +409,6 @@
  *     (before the start) in each object.
  */
 typedef struct obj_layout_unpadded {
-    NSZone     *zone;
     NSUInteger retained;
 } unp;
 #define        UNP sizeof(unp)
@@ -420,7 +419,6 @@
  *     structure correct.
  */
 struct obj_layout {
-    NSZone     *zone;
     NSUInteger retained;
     char       padding[ALIGN - ((UNP % ALIGN) ? (UNP % ALIGN) : ALIGN)];
 };
@@ -770,7 +768,7 @@
   GSOnceFLog(@"GSObjCZone() is deprecated ... use -zone instead");
   if (object_getClass(object) == NSConstantStringClass)
     return NSDefaultMallocZone();
-  return ((obj)object)[-1].zone;
+  return NSZoneFromPointer(object);
 }
 #endif
 
@@ -818,7 +816,6 @@
   if (new != nil)
     {
       memset (new, 0, size);
-      ((obj)new)->zone = zone;
       new = (id)&((obj)new)[1];
       object_setClass(new, aClass);
       AADD(aClass, new);
@@ -861,7 +858,7 @@
   if ((anObject != nil) && !class_isMetaClass(aClass))
     {
       obj      o = &((obj)anObject)[-1];
-      NSZone   *z = o->zone;
+      NSZone   *z = NSZoneFromPointer(o);
 
       /* Call the default finalizer to handle C++ destructors.
        */
@@ -2160,7 +2157,7 @@
    */
   return NSDefaultMallocZone();
 #else
-  return (((obj)self)[-1]).zone;
+  return NSZoneFromPointer(self);
 #endif
 }
 
Index: Source/NSZone.m
===================================================================
--- Source/NSZone.m     (revision 33594)
+++ Source/NSZone.m     (working copy)
@@ -1932,14 +1932,26 @@
 }
 
 static void rnfree (NSZone *zone, void *ptr);
+static long long defaultZoneLookups;
+static long long otherZoneLookups;
 
+__attribute__((destructor))
+static void logStats(void)
+{
+    fprintf(stderr, "NSZoneFromPointer() called %lld times.  %lld non-default 
zones returned\n", defaultZoneLookups + otherZoneLookups, otherZoneLookups);
+}
+
 GS_DECLARE NSZone*
 NSZoneFromPointer(void *ptr)
 {
   NSZone       *zone;
 
   if (ptr == 0) return 0;
-  if (zone_list == 0) return &default_zone;
+  if (zone_list == 0)
+    {
+      defaultZoneLookups++;
+      return &default_zone;
+    }
 
   /*
    *   See if we can find the zone in our list of all zones.
@@ -1953,6 +1965,16 @@
        }
     }
   [gnustep_global_lock unlock];
+  if (0 != zone)
+    {
+      otherZoneLookups++;
+      NSLog(@"Non-default zone found"); 
+    }
+  else
+    {
+      defaultZoneLookups++;
+    }
+
   return (zone == 0) ? &default_zone : zone;
 }
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]