m4-patches
[Top][All Lists]
Advanced

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

01-obstack-speedup


From: David Sterba
Subject: 01-obstack-speedup
Date: Thu, 4 Oct 2001 16:11:09 +0200 (CEST)

Hi,

some obstack speedup and code update.

I cant find lib/* files in checkout now but I could get them few days ago.

Dave

changelog:
2001-10-01  David Sterba  <address@hidden>

        * m4/builtin.c (m4_esyscmd): Read from pipe per 32B blocks.

        Obstack speedup:
        * m4/m4private.h (check_and_1grow_fast): New macro.
        * m4/debug.c (trace_format): Use it.
        * m4/input.c (next_token): Likewise.
        * m4/utility.c (expand_ranges): Likewise.
        * modules/format.c (format): Likewise.
        * modules/gnu.c (substitute): Likewise.
        * modules/m4.c (translit): Likewise.

        * m4/output.c (mkstemp): Simplify code, add parens for clear.

        * src/freeze.c (produce_frozen_state): Put fprintf/fputs/fputc into
        one fprintf.

patch:
? patch
? lib/copying.lib
? lib/debug.c
? lib/error.c
? lib/error.h
? lib/gnu-obstack.h
? lib/hash.c
? lib/hash.h
? lib/macro.c
? lib/module.c
? lib/obstack.c
? lib/regex.c
? lib/regex.h
? lib/xmalloc.c
? modules/readme
Index: m4/debug.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/debug.c,v
retrieving revision 1.9
diff -u -r1.9 debug.c
--- m4/debug.c  2001/10/01 07:38:48     1.9
+++ m4/debug.c  2001/10/04 14:01:52
@@ -228,6 +228,7 @@
   const char *s;
   int slen;
   int maxlen;
+  int obs_room;

 #if (defined __STDC__ && __STDC__) || defined PROTOTYPES
   va_start (args, fmt);
@@ -238,8 +239,9 @@

   while (TRUE)
     {
+      obs_room = obstack_room (&trace);
       while ((ch = *fmt++) != '\0' && ch != '%')
-       obstack_1grow (&trace, ch);
+        check_and_1grow_fast (obs_room, &trace, ch);

       if (ch == '\0')
        break;
Index: m4/input.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/input.c,v
retrieving revision 1.13
diff -u -r1.13 input.c
--- m4/input.c  2001/09/30 22:26:57     1.13
+++ m4/input.c  2001/10/04 14:01:53
@@ -1013,6 +1013,7 @@
 {
   int ch;
   int quote_level;
+  int obs_room;
   m4_token_t type;

   do {
@@ -1039,21 +1040,24 @@
        return M4_TOKEN_MACDEF;
       }

+    obs_room = obstack_room (&token_stack);
     (void) next_char ();
     if (M4_IS_BCOMM(ch))                       /* COMMENT, SHORT DELIM */
       {
-       obstack_1grow (&token_stack, ch);
+       check_and_1grow_fast (obs_room, &token_stack, ch);
        while ((ch = next_char ()) != CHAR_EOF && !M4_IS_ECOMM(ch))
-         obstack_1grow (&token_stack, ch);
+         check_and_1grow_fast (obs_room, &token_stack, ch);
        if (ch != CHAR_EOF)
-         obstack_1grow (&token_stack, ch);
+         check_and_1grow_fast (obs_room, &token_stack, ch);
        type = discard_comments ? M4_TOKEN_NONE : M4_TOKEN_STRING;
       }
                                        /* COMMENT, LONGER DELIM */
     else if (!single_comments && MATCH (ch, bcomm.string))
       {
        obstack_grow (&token_stack, bcomm.string, bcomm.length);
+       obs_room = obstack_room (&token_stack);
        while ((ch = next_char ()) != CHAR_EOF && !MATCH (ch, ecomm.string))
+         check_and_1grow_fast (obs_room, &token_stack, ch);
          obstack_1grow (&token_stack, ch);
        if (ch != CHAR_EOF)
          obstack_grow (&token_stack, ecomm.string, ecomm.length);
@@ -1061,15 +1065,15 @@
       }
     else if (M4_IS_ESCAPE(ch))         /* ESCAPED WORD */
       {
-       obstack_1grow (&token_stack, ch);
+       check_and_1grow_fast (obs_room, &token_stack, ch);
        if ((ch = next_char ()) != CHAR_EOF)
          {
            if (M4_IS_ALPHA(ch))
              {
-               obstack_1grow (&token_stack, ch);
+               check_and_1grow_fast (obs_room, &token_stack, ch);
                while ((ch = next_char ()) != CHAR_EOF && (M4_IS_ALNUM(ch)))
                  {
-                   obstack_1grow (&token_stack, ch);
+                   check_and_1grow_fast (obs_room, &token_stack, ch);
                  }

                if (ch != CHAR_EOF)
@@ -1077,7 +1081,7 @@
              }
            else
              {
-               obstack_1grow (&token_stack, ch);
+               check_and_1grow_fast (obs_room, &token_stack, ch);
              }

            type = M4_TOKEN_WORD;
@@ -1089,10 +1093,10 @@
       }
     else if (M4_IS_ALPHA (ch))
       {
-       obstack_1grow (&token_stack, ch);
+       check_and_1grow_fast (obs_room, &token_stack, ch);
        while ((ch = next_char ()) != CHAR_EOF && (M4_IS_ALNUM(ch)))
          {
-           obstack_1grow (&token_stack, ch);
+           check_and_1grow_fast (obs_room, &token_stack, ch);
          }
        if (ch != CHAR_EOF)
          unget_input(ch);
@@ -1113,15 +1117,10 @@
              {
                if (--quote_level == 0)
                  break;
-               obstack_1grow (&token_stack, ch);
              }
            else if (M4_IS_LQUOTE(ch))
-             {
                quote_level++;
-               obstack_1grow (&token_stack, ch);
-             }
-           else
-             obstack_1grow (&token_stack, ch);
+           check_and_1grow_fast (obs_room, &token_stack, ch);
          }
        type = M4_TOKEN_STRING;
       }
@@ -1154,13 +1153,13 @@
       }
     else if (single_quotes && single_comments) /* EVERYTHING ELSE */
       {
-       obstack_1grow (&token_stack, ch);
+       check_and_1grow_fast (obs_room, &token_stack, ch);

        if (M4_IS_OTHER(ch) || M4_IS_NUM(ch))
          {
            while ((ch = next_char()) != CHAR_EOF
                   && (M4_IS_OTHER(ch) || M4_IS_NUM(ch)))
-             obstack_1grow (&token_stack, ch);
+             check_and_1grow_fast (obs_room, &token_stack, ch);

            if (ch != CHAR_EOF)
              unget_input(ch);
@@ -1171,7 +1170,7 @@
            if (!interactive)
              {
                while ((ch = next_char()) != CHAR_EOF && M4_IS_SPACE(ch))
-                 obstack_1grow (&token_stack, ch);
+                 check_and_1grow_fast (obs_room, &token_stack, ch);

                if (ch != CHAR_EOF)
                  unget_input(ch);
@@ -1185,7 +1184,7 @@
       }
     else                               /* EVERYTHING ELSE */
       {
-       obstack_1grow (&token_stack, ch);
+       check_and_1grow_fast (obs_room, &token_stack, ch);

        if (M4_IS_OTHER(ch) || M4_IS_NUM(ch))
          type = M4_TOKEN_STRING;
@@ -1198,7 +1197,7 @@
       }
   } while (type == M4_TOKEN_NONE);

-  obstack_1grow (&token_stack, '\0');
+  check_and_1grow_fast (obs_room, &token_stack, '\0');

   M4_TOKEN_DATA_TYPE (td) = M4_TOKEN_TEXT;
   M4_TOKEN_DATA_TEXT (td) = obstack_finish (&token_stack);
Index: m4/m4private.h
===================================================================
RCS file: /cvsroot/m4/m4/m4/m4private.h,v
retrieving revision 1.12
diff -u -r1.12 m4private.h
--- m4/m4private.h      2001/09/30 22:26:57     1.12
+++ m4/m4private.h      2001/10/04 14:01:53
@@ -75,6 +75,25 @@
 #define M4_SYMBOL_FUNC(S)         (M4_TOKEN_DATA_FUNC (M4_SYMBOL_DATA(S)))
 #define M4_SYMBOL_HANDLE(S)       (M4_TOKEN_DATA_HANDLE (M4_SYMBOL_DATA(S)))

+/* Fast obstack growing. */
+/* obs_room:   variable which holds obstack room
+ * obs:                the obstack
+ * ch:         char to write
+ */
+/* obs_room should be set to obstack_room (obs) before use or after any
+ * call of obstack_grow ()
+ */
+#define check_and_1grow_fast(obs_room,obs,ch)          \
+       do {                                            \
+         if (obs_room-- > 0)                           \
+           obstack_1grow_fast (obs, ch);               \
+         else                                          \
+           {                                           \
+             obstack_1grow (obs, ch);                  \
+             obs_room = obstack_room (obs);            \
+           }                                           \
+       } while(0)
+

 /* Debugging the memory allocator.  */

Index: m4/output.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/output.c,v
retrieving revision 1.8
diff -u -r1.8 output.c
--- m4/output.c 2001/09/30 22:26:57     1.8
+++ m4/output.c 2001/10/04 14:01:53
@@ -167,7 +167,7 @@
     return -1;
   }

-  pid = getpid(); if (pid > 99999) { pid = pid % 100000; }
+  pid = getpid() % 100000;
   uniq = 'a';

   while (1) {
@@ -178,14 +178,13 @@
     }

     /* this will ensure we cover a-zA-Z0-9 before giving up */
-    if (uniq == 'z') {
-      uniq = 'A';
-    } else if (uniq == 'Z') {
-      uniq = '0';
-    } else if (uniq == '9') {
+    switch (uniq) {
+    case 'z': uniq = 'A';break;
+    case 'Z': uniq = '0';break;
+    case '9':
       errno = EEXIST; /* couldn't find one */
       return -1;
-    } else {
+    default: uniq++;
       uniq++;
     }
   }
@@ -248,7 +247,7 @@

   for (wanted_size = output_diversion->size;
        wanted_size < output_diversion->used + length;
-       wanted_size = wanted_size == 0 ? INITIAL_BUFFER_SIZE : wanted_size * 2)
+       wanted_size = (wanted_size == 0 ? INITIAL_BUFFER_SIZE : wanted_size * 
2))
     ;

   /* Check if we are exceeding the maximum amount of buffer memory.  */
Index: m4/utility.c
===================================================================
RCS file: /cvsroot/m4/m4/m4/utility.c,v
retrieving revision 1.16
diff -u -r1.16 utility.c
--- m4/utility.c        2001/10/01 07:38:48     1.16
+++ m4/utility.c        2001/10/04 14:01:53
@@ -184,7 +184,9 @@
 {
   char from;
   char to;
+  int obs_room;

+  obs_room = obstack_room (obs);
   for (from = '\0'; *s != '\0'; from = *s++)
     {
       if (*s == '-' && from != '\0')
@@ -193,24 +195,24 @@
          if (to == '\0')
            {
               /* trailing dash */
-              obstack_1grow (obs, '-');
+             check_and_1grow_fast (obs_room, obs, '-');
               break;
            }
          else if (from <= to)
            {
              while (from++ < to)
-               obstack_1grow (obs, from);
+               check_and_1grow_fast (obs_room, obs, from);
            }
          else
            {
              while (--from >= to)
-               obstack_1grow (obs, from);
+               check_and_1grow_fast (obs_room, obs, from);
            }
        }
       else
-       obstack_1grow (obs, *s);
+       check_and_1grow_fast (obs_room, obs, *s);
     }
-  obstack_1grow (obs, '\0');
+  check_and_1grow_fast (obs_room, obs, '\0');
   return obstack_finish (obs);
 }

Index: modules/format.c
===================================================================
RCS file: /cvsroot/m4/m4/modules/format.c,v
retrieving revision 1.12
diff -u -r1.12 format.c
--- modules/format.c    2001/10/01 07:38:48     1.12
+++ modules/format.c    2001/10/04 14:01:53
@@ -58,6 +58,7 @@
   char *fmt;                   /* format control string */
   const char *fstart;          /* beginning of current format spec */
   int c;                       /* a simple character */
+  int obs_room;                        /* room in output obstack */

   /* Flags.  */
   char flags;                  /* 1 iff treating flags */
@@ -75,18 +76,19 @@
   fmt = ARG_STR (argc, argv);
   for (;;)
     {
+      obs_room = obstack_room (obs);
       while ((c = *fmt++) != '%')
        {
          if (c == 0)
            return;
-         obstack_1grow (obs, c);
+         check_and_1grow_fast (obs_room, obs, c);
        }

       fstart = fmt - 1;

       if (*fmt == '%')
        {
-         obstack_1grow (obs, '%');
+         check_and_1grow_fast (obs_room, obs, '%');
          fmt++;
          continue;
        }
Index: modules/gnu.c
===================================================================
RCS file: /cvsroot/m4/m4/modules/gnu.c,v
retrieving revision 1.13
diff -u -r1.13 gnu.c
--- modules/gnu.c       2001/10/01 07:38:48     1.13
+++ modules/gnu.c       2001/10/04 14:01:53
@@ -478,7 +478,8 @@
 M4BUILTIN_HANDLER (esyscmd)
 {
   FILE *pin;
-  int ch;
+  int rd;
+  char write_buf[32];                  /* 32 seems to be optimal */

   if (m4_bad_argc (argv[0], argc, 2, 2))
     return;
@@ -493,8 +494,10 @@
     }
   else
     {
-      while ((ch = getc (pin)) != EOF)
-       obstack_1grow (obs, (char) ch);
+      while ( (rd = fread (write_buf, 1, 32, pin)) == 32)
+       obstack_grow (obs, write_buf, 32);
+      if( rd > 0)
+       obstack_grow (obs, write_buf, rd);
       m4_sysval = pclose (pin);
     }
 }
@@ -546,14 +549,16 @@
            struct re_registers *regs)
 {
   register unsigned int ch;
+  int obs_room;

   for (;;)
     {
+      obs_room = obstack_room (obs);
       while ((ch = *repl++) != '\\')
        {
          if (ch == '\0')
            return;
-         obstack_1grow (obs, ch);
+         check_and_1grow_fast (obs_room, obs, ch);
        }

       switch ((ch = *repl++))
@@ -581,7 +586,7 @@
          break;

        default:
-         obstack_1grow (obs, ch);
+         check_room_and_1grow_fast (obs_room, obs, ch);
          break;
        }
     }
Index: modules/m4.c
===================================================================
RCS file: /cvsroot/m4/m4/modules/m4.c,v
retrieving revision 1.25
diff -u -r1.25 m4.c
--- modules/m4.c        2001/10/01 07:38:48     1.25
+++ modules/m4.c        2001/10/04 14:01:54
@@ -778,6 +778,7 @@
   register const char *data, *tmp;
   const char *from, *to;
   int tolen;
+  int obs_room;

   if (m4_bad_argc (argv[0], argc, 3, 4))
     return;
@@ -804,18 +805,20 @@
     to = "";

   tolen = strlen (to);
+  obs_room = obstack_room (obs);

   for (data = M4ARG (1); *data; data++)
     {
       tmp = strchr (from, *data);
       if (tmp == NULL)
        {
-         obstack_1grow (obs, *data);
+
+         check_and_1grow_fast (obs_room, obs, *data);
        }
       else
        {
          if (tmp - from < tolen)
-           obstack_1grow (obs, *(to + (tmp - from)));
+           check_and_1grow_fast (obs_room, obs, *(to + (tmp - from)));
        }
     }
 }
Index: src/freeze.c
===================================================================
RCS file: /cvsroot/m4/m4/src/freeze.c,v
retrieving revision 1.19
diff -u -r1.19 freeze.c
--- src/freeze.c        2001/09/30 22:26:57     1.19
+++ src/freeze.c        2001/10/04 14:01:54
@@ -193,7 +193,8 @@
 {
   FILE *file;

-  if (file = fopen (name, "w"), !file)
+  file = fopen (name, "w");
+  if (file == NULL)
     {
       M4ERROR ((warning_status, errno, name));
       return;
@@ -203,31 +204,29 @@

   fprintf (file, "# This is a frozen state file generated by GNU %s %s\n",
           PACKAGE, VERSION);
-  fprintf (file, "V2\n");
+  fputs (file, "V2\n");

   /* Dump quote delimiters.  */

   if (strcmp (lquote.string, DEF_LQUOTE)
       || strcmp (rquote.string, DEF_RQUOTE))
     {
-      fprintf (file, "Q%lu,%lu\n",
+      fprintf (file, "Q%lu,%lu\n%s%s\n",
               (unsigned long) lquote.length,
-              (unsigned long) rquote.length);
-      fputs (lquote.string, file);
-      fputs (rquote.string, file);
-      fputc ('\n', file);
+              (unsigned long) rquote.length,
+               lquote.string,
+              rquote.string);
     }

   /* Dump comment delimiters.  */

   if (strcmp (bcomm.string, DEF_BCOMM) || strcmp (ecomm.string, DEF_ECOMM))
     {
-      fprintf (file, "C%lu,%lu\n",
-              (unsigned long) bcomm.length,
-              (unsigned long) ecomm.length);
-      fputs (bcomm.string, file);
-      fputs (ecomm.string, file);
-      fputc ('\n', file);
+      fprintf (file, "C%lu,%lu\n%s%s\n",
+              (unsigned long) bcomm.length,
+              (unsigned long) ecomm.length,
+              bcomm.string,
+              ecomm.string);
     }

   /* Dump syntax table. */




reply via email to

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