SmallInteger>>printString now twice as fast (for andi and olbi)
authorClaus Gittinger <cg@exept.de>
Fri, 31 Jul 1998 21:56:46 +0200
changeset 3704 4dd3cb7ae956
parent 3703 9ef3fd9febd3
child 3705 f4e7b2db4d4b
SmallInteger>>printString now twice as fast (for andi and olbi)
SmallInt.st
SmallInteger.st
--- a/SmallInt.st	Fri Jul 31 19:37:00 1998 +0200
+++ b/SmallInt.st	Fri Jul 31 21:56:46 1998 +0200
@@ -2431,35 +2431,65 @@
 printString
     "return my printstring (base 10)"
 
-    "since printf knows pretty good how to do this,
+    "since this was heavily used in some applications,
      here is an exception to the rule of basing printString
      upon the printOn: method."
 
 %{  /* NOCONTEXT */
 
     char buffer[30];
+    char *cp;
     OBJ newString;
-    /*
-     * actually only needed on sparc: since thisContext is
-     * in a global register, which gets destroyed by printf,
-     * manually save it here - very stupid ...
+    INT myValue;
+    int negative = 0;
+
+#ifdef SLOW_CODE
+    /* 
+     * this takes twice as long as the code below ...
+     * (printf is soooo slow)
      */
-    __BEGIN_PROTECT_REGISTERS__
-
-#ifdef alpha64
+# ifdef alpha64
     sprintf(buffer, "%ld", __intVal(self));
+# else
+    sprintf(buffer, "%d", __intVal(self));
+# endif
+    newString = __MKSTRING(buffer COMMA_SND);
+
 #else
-    sprintf(buffer, "%d", __intVal(self));
+    myValue = __intVal(self);
+    if (myValue == 0) {
+        RETURN (__MKSTRING_L("0", 1));
+    }
+    if (myValue < 0) {
+        negative = 1;
+        myValue = -myValue;
+    }
+    cp = buffer + sizeof(buffer) - 1;
+    *cp-- = '\0';
+    while (myValue != 0) {
+        *cp = '0' + (myValue % 10);
+        myValue = myValue / 10;
+        cp--;
+    }
+    if (negative) {
+        *cp-- = '-';
+    }
+    newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp));
 #endif
-
-    __END_PROTECT_REGISTERS__
-
-    newString = __MKSTRING(buffer COMMA_SND);
     if (newString != nil) {
-	RETURN (newString);
+        RETURN (newString);
     }
 %}.
+    "/ only arrive here,
+    "/  when having memory problems (i.e. no space for string) ...
     ^ super printString
+
+    "
+     1234 printString   
+     0    printString      
+     -100 printString   
+    "
+
 !
 
 printStringRadix:base
@@ -2714,5 +2744,5 @@
 !SmallInteger class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Attic/SmallInt.st,v 1.100 1998-07-28 18:32:07 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Attic/SmallInt.st,v 1.101 1998-07-31 19:56:46 cg Exp $'
 ! !
--- a/SmallInteger.st	Fri Jul 31 19:37:00 1998 +0200
+++ b/SmallInteger.st	Fri Jul 31 21:56:46 1998 +0200
@@ -2431,35 +2431,65 @@
 printString
     "return my printstring (base 10)"
 
-    "since printf knows pretty good how to do this,
+    "since this was heavily used in some applications,
      here is an exception to the rule of basing printString
      upon the printOn: method."
 
 %{  /* NOCONTEXT */
 
     char buffer[30];
+    char *cp;
     OBJ newString;
-    /*
-     * actually only needed on sparc: since thisContext is
-     * in a global register, which gets destroyed by printf,
-     * manually save it here - very stupid ...
+    INT myValue;
+    int negative = 0;
+
+#ifdef SLOW_CODE
+    /* 
+     * this takes twice as long as the code below ...
+     * (printf is soooo slow)
      */
-    __BEGIN_PROTECT_REGISTERS__
-
-#ifdef alpha64
+# ifdef alpha64
     sprintf(buffer, "%ld", __intVal(self));
+# else
+    sprintf(buffer, "%d", __intVal(self));
+# endif
+    newString = __MKSTRING(buffer COMMA_SND);
+
 #else
-    sprintf(buffer, "%d", __intVal(self));
+    myValue = __intVal(self);
+    if (myValue == 0) {
+        RETURN (__MKSTRING_L("0", 1));
+    }
+    if (myValue < 0) {
+        negative = 1;
+        myValue = -myValue;
+    }
+    cp = buffer + sizeof(buffer) - 1;
+    *cp-- = '\0';
+    while (myValue != 0) {
+        *cp = '0' + (myValue % 10);
+        myValue = myValue / 10;
+        cp--;
+    }
+    if (negative) {
+        *cp-- = '-';
+    }
+    newString = __MKSTRING_L(cp+1, (buffer + sizeof(buffer) - 2 - cp));
 #endif
-
-    __END_PROTECT_REGISTERS__
-
-    newString = __MKSTRING(buffer COMMA_SND);
     if (newString != nil) {
-	RETURN (newString);
+        RETURN (newString);
     }
 %}.
+    "/ only arrive here,
+    "/  when having memory problems (i.e. no space for string) ...
     ^ super printString
+
+    "
+     1234 printString   
+     0    printString      
+     -100 printString   
+    "
+
 !
 
 printStringRadix:base
@@ -2714,5 +2744,5 @@
 !SmallInteger class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.100 1998-07-28 18:32:07 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.101 1998-07-31 19:56:46 cg Exp $'
 ! !