Author: rfm
Date: Mon May 25 13:47:13 2015
New Revision: 38540

URL: http://svn.gna.org/viewcvs/gnustep?rev=38540&view=rev
Log:
Another small optimisation for string creation ...

Modified:
    libs/base/trunk/Source/GSString.m
    libs/base/trunk/Source/NSString.m

Modified: libs/base/trunk/Source/GSString.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/GSString.m?rev=38540&r1=38539&r2=38540&view=diff
==============================================================================
--- libs/base/trunk/Source/GSString.m   (original)
+++ libs/base/trunk/Source/GSString.m   Mon May 25 13:47:13 2015
@@ -458,6 +458,7 @@
  */
 @interface GSPlaceholderString : NSString
 {
+  NSZone        *myZone;
 }
 @end
 
@@ -1125,6 +1126,13 @@
  * on the initialisation method used.
  */
 @implementation GSPlaceholderString
++ (id) allocWithZone: (NSZone*)z
+{
+  GSPlaceholderString   *o = NSAllocateObject(self, 0, z);
+  o->myZone = z;
+  return o;
+}
+
 + (void) initialize
 {
   setup(NO);
@@ -1327,7 +1335,7 @@
 #if    GS_WITH_GC
       chars = NSAllocateCollectable(length, 0);
 #else
-      chars = NSZoneMalloc([self zone], length);
+      chars = NSZoneMalloc(myZone, length);
 #endif
       memcpy(chars, bytes, length);
     }
@@ -1413,13 +1421,13 @@
        */
       if (GSPrivateIsCollectable(chars.c) == NO)
        {
-          me = newCInline(length, [self zone]);
+          me = newCInline(length, myZone);
          memcpy(me->_contents.c, chars.c, length);
          NSZoneFree(NSZoneFromPointer(chars.c), chars.c);
           return (id)me;
        }
 #endif
-      me = (GSStr)NSAllocateObject(GSCBufferStringClass, 0, [self zone]);
+      me = (GSStr)NSAllocateObject(GSCBufferStringClass, 0, myZone);
       me->_contents.c = chars.c;
       me->_count = length;
       me->_flags.wide = 0;
@@ -1444,8 +1452,7 @@
           return nil;  // Invalid encoding
         }
 
-      if (GSToUnicode(&u, &l, chars.c, length, encoding,
-       [self zone], 0) == NO)
+      if (GSToUnicode(&u, &l, chars.c, length, encoding, myZone, 0) == NO)
        {
          if (flag == YES && chars.c != 0)
            {
@@ -1475,7 +1482,7 @@
   if (isASCII == YES
     || (internalEncoding == NSISOLatin1StringEncoding && isLatin1 == YES))
     {
-      me = (GSStr)newCInline(length, [self zone]);
+      me = (GSStr)newCInline(length, myZone);
       while (length-- > 0)
         {
          me->_contents.c[length] = chars.u[length];
@@ -1493,14 +1500,13 @@
        */
       if (GSPrivateIsCollectable(chars.u) == NO)
        {
-          me = newUInline(length, [self zone]);
+          me = newUInline(length, myZone);
          memcpy(me->_contents.u, chars.u, length * sizeof(unichar));
          NSZoneFree(NSZoneFromPointer(chars.u), chars.u);
           return (id)me;
        }
 #endif
-      me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass,
-       0, [self zone]);
+      me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass, 0, myZone);
       me->_contents.u = chars.u;
       me->_count = length;
       me->_flags.wide = 1;
@@ -1598,12 +1604,12 @@
    */
   if (f->_flags.wide == 1)
     {
-      me = (GSStr)newUInline(f->_count, [self zone]);
+      me = (GSStr)newUInline(f->_count, myZone);
       memcpy(me->_contents.u, f->_contents.u, f->_count*sizeof(unichar));
     }
   else
     {
-      me = (GSStr)newCInline(f->_count, [self zone]);
+      me = (GSStr)newCInline(f->_count, myZone);
       memcpy(me->_contents.c, f->_contents.c, f->_count);
     }
 
@@ -1645,7 +1651,7 @@
        * For a GSCString subclass, or an 8-bit GSMutableString,
        * we can copy the bytes directly into an inline string.
        */
-      me = (GSStr)newCInline(length, [self zone]);
+      me = (GSStr)newCInline(length, myZone);
       memcpy(me->_contents.c, ((GSStr)string)->_contents.c, length);
     }
   else if (GSObjCIsKindOf(c, GSUnicodeStringClass) == YES
@@ -1655,7 +1661,7 @@
        * For a GSUnicodeString subclass, or a 16-bit GSMutableString,
        * we can copy the bytes directly into an inline string.
        */
-      me = (GSStr)newUInline(length, [self zone]);
+      me = (GSStr)newUInline(length, myZone);
       memcpy(me->_contents.u, ((GSStr)string)->_contents.u,
        length*sizeof(unichar));
     }
@@ -1665,7 +1671,7 @@
        * For a string with an unknown class, we can initialise by
        * having the string copy its content directly into our buffer.
        */
-      me = (GSStr)newUInline(length, [self zone]);
+      me = (GSStr)newUInline(length, myZone);
       [string getCharacters: me->_contents.u];
     }
   return (id)me;
@@ -1676,6 +1682,7 @@
   BOOL         ascii = YES;
   NSUInteger    length;
   GSStr                me;
+  char          c;
 
   if (0 == bytes)
     {
@@ -1687,37 +1694,39 @@
     {
       bytes = &bytes[3];
     }
-  if (0 == bytes[0])
-    {
-      return (id)@"";
-    }
-
-  for (length = 0; bytes[length]; length++)
-    {
-      if (bytes[length] > 127)
+
+  length = 0;
+  while ((c = bytes[length]))
+    {
+      length++;
+      if (c > 127)
         {
           ascii = NO;
+          while (bytes[length])
+            {
+              length++;
+            }
+          break;
         }
     }
 
   if (YES == ascii)
     {
-      me = (GSStr)newCInline(length, [self zone]);
+      me = (GSStr)newCInline(length, myZone);
       memcpy(me->_contents.c, bytes, length);
       return (id)me;
     }
   else
     {
       const unsigned char       *b = (const unsigned char*)bytes;
-      NSZone                    *z = [self zone];
       unichar                  *u = 0;
       unsigned                 l = 0;
 
-      if (GSToUnicode(&u, &l, b, length, NSUTF8StringEncoding, z, 0) == NO)
+      if (GSToUnicode(&u, &l, b, length, NSUTF8StringEncoding, myZone, 0) == 
NO)
        {
          return nil;   // Invalid data
        }
-      me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass, 0, z);
+      me = (GSStr)NSAllocateObject(GSUnicodeBufferStringClass, 0, myZone);
       me->_contents.u = u;
       me->_count = l;
       me->_flags.wide = 1;

Modified: libs/base/trunk/Source/NSString.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/NSString.m?rev=38540&r1=38539&r2=38540&view=diff
==============================================================================
--- libs/base/trunk/Source/NSString.m   (original)
+++ libs/base/trunk/Source/NSString.m   Mon May 25 13:47:13 2015
@@ -790,7 +790,7 @@
        * Set up infrastructure for placeholder strings.
        */
       defaultPlaceholderString = (GSPlaceholderString*)
-       NSAllocateObject(GSPlaceholderStringClass, 0, NSDefaultMallocZone());
+       [GSPlaceholderStringClass allocWithZone: NSDefaultMallocZone()];
       placeholderMap = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks,
        NSNonRetainedObjectMapValueCallBacks, 0);
       placeholderLock = [NSLock new];
@@ -852,7 +852,7 @@
               * There is no placeholder object for this zone, so we
               * create a new one and use that.
               */
-             obj = (id)NSAllocateObject(GSPlaceholderStringClass, 0, z);
+             obj = (id)[GSPlaceholderStringClass allocWithZone: z];
              NSMapInsert(placeholderMap, (void*)z, (void*)obj);
            }
          [placeholderLock unlock];


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

Reply via email to