Author: rfm
Date: Wed Jul 15 16:48:19 2015
New Revision: 38800

URL: http://svn.gna.org/viewcvs/gnustep?rev=38800&view=rev
Log:
experimental changes to memory usagfe accounting

Modified:
    libs/performance/trunk/ChangeLog
    libs/performance/trunk/GSCache.h
    libs/performance/trunk/GSCache.m

Modified: libs/performance/trunk/ChangeLog
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/performance/trunk/ChangeLog?rev=38800&r1=38799&r2=38800&view=diff
==============================================================================
--- libs/performance/trunk/ChangeLog    (original)
+++ libs/performance/trunk/ChangeLog    Wed Jul 15 16:48:19 2015
@@ -1,3 +1,9 @@
+2015-07-15 Richard Frith-Macdonald  <[email protected]>
+
+       * GSCache.m: Experimental change (use in conjunction with gnustep-base
+       from svn 38799) to put object memory footprint interrogation into the
+       base library and make it more accurate and efficient.
+
 2015-05-05 Niels Grewe <[email protected]>
 
        * GSFIFO.m: Fix a potential race condition in -peekObject.

Modified: libs/performance/trunk/GSCache.h
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/performance/trunk/GSCache.h?rev=38800&r1=38799&r2=38800&view=diff
==============================================================================
--- libs/performance/trunk/GSCache.h    (original)
+++ libs/performance/trunk/GSCache.h    Wed Jul 15 16:48:19 2015
@@ -263,56 +263,11 @@
 @end
 
 /**
- * <p>This category declares the -sizeInBytes: method which is used by
- * [GSCache] to ask objects for their size when adding them to a cache
- * which has a limited size in bytes.
- * </p>
- * <p>The [NSObject] implementation of this method is intended to
- * provide a rough estimnate of object size which subclasses may refine
- * in order to provide more accurate sizing information.<br />
- * Subclasses should call the superclass implementation and use the
- * resulting size information (if it is zero then the object is in the
- * exclude set and the subclass implementation should also return zero)
- * as a starting point on which to add the sizes of any objects contained.
- * </p>
- * For example:
- * <example>
- * - (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
- * {
- *   NSUInteger size = [super sizeInBytes: exclude];
- *   if (size > 0)
- *     {
- *       size += [myInstanceVariable sizeInBytes: exclude];
- *     }
- *   return size;
- * }
- * </example>
- * <p>The performance library contains implementations giving reasonable size
- * estimates for several common classes:
- * </p>
- * <list>
- *   <item>NSArray</item>
- *   <item>NSData</item>
- *   <item>NSDictionary</item>
- *   <item>NSSet</item>
- *   <item>NSString</item>
- *   <item>GSMimeDocument (if built with the GNUstep base library)</item>
- * </list>
- * <p>The default ([NSObject]) implementation provides a reasonable size
- * for any class whose instance variables do not include other objects
- * or pointers to memory allocated on the heap.
- * </p>
- */
-@interface     NSObject (GSCacheSizeInBytes)
-/**
- * If the receiver is a member of the exclude set, this method simply
- * returns zero.  Otherwise, the receiver adds itsself to the exclude
- * set and returns its own size in bytes (the size of the memory used
- * to hold all the instance variables defined for the receiver's class
- * including all superclasses).
- */
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude;
-@end
+ * The -sizeInBytes: method is now declared in the GNUstep-base additions
+ * header as follows:
+ *
+ * - (NSUInteger) sizeInBytes: (NSHashTable*)exclude;
+ */
 
 #endif
 

Modified: libs/performance/trunk/GSCache.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/performance/trunk/GSCache.m?rev=38800&r1=38799&r2=38800&view=diff
==============================================================================
--- libs/performance/trunk/GSCache.m    (original)
+++ libs/performance/trunk/GSCache.m    Wed Jul 15 16:48:19 2015
@@ -39,7 +39,6 @@
 #import        <Foundation/NSLock.h>
 #import        <Foundation/NSMapTable.h>
 #import        <Foundation/NSNotification.h>
-#import        <Foundation/NSSet.h>
 #import        <Foundation/NSString.h>
 #import        <Foundation/NSThread.h>
 #import        <Foundation/NSValue.h>
@@ -89,6 +88,17 @@
   [key release];
   [object release];
   [super dealloc];
+}
+- (NSUInteger) sizeInBytes: (NSHashTable*)exclude
+{
+  NSUInteger    size = [super sizeInBytes: exclude];
+
+  if (size > 0)
+    {
+      size += [key sizeInBytes: exclude];
+      size += [object sizeInBytes: exclude];
+    }
+  return size;
 }
 @end
 
@@ -113,7 +123,7 @@
   NSMapTable   *contents;
   GSCacheItem  *first;
   NSString     *name;
-  NSMutableSet *exclude;
+  NSHashTable  *exclude;
   NSRecursiveLock      *lock;
 } Item;
 #define        my      ((Item*)((void*)self + itemOffset))
@@ -234,7 +244,36 @@
 
 - (NSUInteger) currentSize
 {
-  return my->currentSize;
+  NSUInteger            size = 0;
+  NSMapEnumerator      e;
+  GSCacheItem          *i;
+  id                   k;
+
+  [my->lock lock];
+  if (nil == my->exclude)
+    {
+      my->exclude = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
+    }
+  else
+    {
+      [my->exclude removeAllObjects];
+    }
+  e = NSEnumerateMapTable(my->contents);
+  while (NSNextMapEnumeratorPair(&e, (void**)&k, (void**)&i) != 0)
+    {
+      size += [i->object sizeInBytes: my->exclude];
+    }
+  NSEndMapTableEnumeration(&e);
+  if (my->maxSize > 0)
+    {
+      my->currentSize = size;
+    }
+  else
+    {
+      DESTROY(my->exclude);
+    }
+  [my->lock unlock];
+  return size;
 }
 
 - (void) dealloc
@@ -582,7 +621,8 @@
 
       if (my->exclude == nil)
        {
-         my->exclude = [NSMutableSet new];
+         my->exclude
+            = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
        }
       while (NSNextMapEnumeratorPair(&e, (void**)&k, (void**)&i) != 0)
        {
@@ -663,10 +703,11 @@
     {
       if (maxSize > 0)
        {
-         if (my->exclude == nil)
-           {
-             my->exclude = [NSMutableSet new];
-           }
+          if (my->exclude == nil)
+            {
+              my->exclude
+                = NSCreateHashTable(NSNonOwnedPointerHashCallBacks, 0);
+            }
          [my->exclude removeAllObjects];
          addSize = [anObject sizeInBytes: my->exclude];
          if (addSize > maxSize)
@@ -763,6 +804,22 @@
     }
   [my->lock unlock];
 }
+
+- (NSUInteger) sizeInBytes: (NSHashTable*)exclude
+{
+  NSUInteger    size = [super sizeInBytes: exclude];
+
+  if (size > 0)
+    {
+      size += sizeof(Item)
+        + [my->contents sizeInBytes: exclude]
+        + [my->exclude sizeInBytes: exclude]
+        + [my->name sizeInBytes: exclude]
+        + [my->lock sizeInBytes: exclude];
+    }
+  return size;
+}
+
 @end
 @implementation        GSCache (Threading)
 + (void) _becomeThreaded: (NSNotification*)n
@@ -786,174 +843,4 @@
 }
 @end
 
-@implementation        NSArray (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  NSUInteger   size = [super sizeInBytes: exclude];
-
-  if (size > 0)
-    {
-      NSUInteger       count = [self count];
-
-      size += count*sizeof(void*);
-      while (count-- > 0)
-       {
-         size += [[self objectAtIndex: count] sizeInBytes: exclude];
-       }
-    }
-  return size;
-}
-@end
-
-@implementation        NSData (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  NSUInteger   size = [super sizeInBytes: exclude];
-
-  if (size > 0)
-    {
-      size += [self length];
-    }
-  return size;
-}
-@end
-
-@implementation        NSDictionary (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  NSUInteger   size = [super sizeInBytes: exclude];
-
-  if (size > 0)
-    {
-      NSUInteger       count = [self count];
-
-      size += 3 * sizeof(void*) * count;
-      if (count > 0)
-        {
-         NSAutoreleasePool     *pool = [NSAutoreleasePool new];
-         NSEnumerator          *enumerator = [self keyEnumerator];
-         NSObject              *k;
-
-         while ((k = [enumerator nextObject]) != nil)
-           {
-             NSObject  *o = [self objectForKey: k];
-
-             size += [k sizeInBytes: exclude] + [o sizeInBytes: exclude];
-           }
-         [pool release];
-       }
-    }
-  return size;
-}
-@end
-
-@implementation        NSObject (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  if ([exclude member: self] != nil)
-    {
-      return 0;
-    }
-  [exclude addObject: self];
-
-  return class_getInstanceSize(object_getClass(self)); 
-}
-@end
-
-@implementation        NSSet (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  NSUInteger   size = [super sizeInBytes: exclude];
-
-  if (size > 0)
-    {
-      NSUInteger       count = [self count];
-
-      size += 3 * sizeof(void*) * count;
-      if (count > 0)
-        {
-         NSAutoreleasePool     *pool = [NSAutoreleasePool new];
-         NSEnumerator          *enumerator = [self objectEnumerator];
-         NSObject              *o;
-
-         while ((o = [enumerator nextObject]) != nil)
-           {
-             size += [o sizeInBytes: exclude];
-           }
-         [pool release];
-       }
-    }
-  return size;
-}
-@end
-
-@implementation        NSString (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  if ([exclude member: self] != nil)
-    {
-      return 0;
-    }
-  else
-    {
-      return [super sizeInBytes: exclude] + sizeof(unichar) * [self length];
-    }
-}
-@end
-
-#if    defined(GNUSTEP_BASE_LIBRARY)
-
-// FIXME ... this should be moved to base additions.
-#import        <GNUstepBase/GSMime.h>
-
-#if    GS_NONFRAGILE
-/* When using the non-fragile ABI, base does not expose the mime class
- * instance variables, so we use the raw data size as a rough approximation.
- * The actual memory usage will be larger than this of course.
- */
-@implementation        GSMimeDocument (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  return [[self rawMimeData] sizeInBytes: exclude];
-}
-@end
-
-@implementation        GSMimeHeader (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  return [[self rawMimeData] sizeInBytes: exclude];
-}
-@end
-#else
-@implementation        GSMimeDocument (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  NSUInteger   size = [super sizeInBytes: exclude];
-
-  if (size > 0)
-    {
-      size += [content sizeInBytes: exclude] + [headers sizeInBytes: exclude];
-    }
-  return size;
-}
-@end
-
-@implementation        GSMimeHeader (GSCacheSizeInBytes)
-- (NSUInteger) sizeInBytes: (NSMutableSet*)exclude
-{
-  NSUInteger   size = [super sizeInBytes: exclude];
-
-  if (size > 0)
-    {
-      size += [name sizeInBytes: exclude]
-        + [value sizeInBytes: exclude]
-        + [objects sizeInBytes: exclude]
-        + [params sizeInBytes: exclude];
-    }
-  return size;
-}
-@end
-#endif
-
-#endif
-
+


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

Reply via email to