cpplib: double speed of comment skipping

Neil Booth NeilB@earthling.net
Fri Apr 7 16:42:00 GMT 2000


This patch speeds up comment skipping by reducing the number of local
variables, and a simpler end-of-buffer test.  I don't think any code
clarity is sacrificed.

On my AMD K6-2 350, with -O2, this takes 55-60% off the skip time
depending upon the length of the comment.

Zack - I think we can achieve speedups in many places, though not of
the same magnitude, with a similar technique.

Neil.

	* cpplex.c (skip_block_comment): replace GETC() with pointer
	indirection and arithmetic.

Index: cpplex.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cpplex.c,v
retrieving revision 1.8
diff -u -p -r1.8 cpplex.c
--- cpplex.c	2000/04/06 07:56:14	1.8
+++ cpplex.c	2000/04/07 23:35:36
@@ -247,33 +247,37 @@ static void
 skip_block_comment (pfile)
      cpp_reader *pfile;
 {
-  int c, prev_c = -1;
   long line, col;
+  const U_CHAR *limit, *cur;
 
   FORWARD(1);
   cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
-  for (;;)
+  limit = CPP_BUFFER (pfile)->rlimit;
+  cur = CPP_BUFFER (pfile)->cur;
+
+  while (cur < limit)
     {
-      c = GETC ();
-      if (c == EOF)
-	{
-	  cpp_error_with_line (pfile, line, col, "unterminated comment");
-	  return;
-	}
-      else if (c == '\n' || c == '\r')
+      char c = *cur++;
+      if (c == '\n' || c == '\r')
 	{
 	  /* \r cannot be a macro escape marker here. */
 	  if (!ACTIVE_MARK_P (pfile))
 	    CPP_BUMP_LINE (pfile);
 	}
-      else if (c == '/' && prev_c == '*')
-	return;
-      else if (c == '*' && prev_c == '/'
+      else if (c == '/')
+	{
+	  /* Avoid false positives for the comment's initial '*'.  */
+	  if (cur[-2] == '*' && cur - 2 > CPP_BUFFER (pfile)->cur)
+	    goto out;
+	}
+      else if (c == '*' && cur[-2] == '/'
 	       && CPP_OPTION (pfile, warn_comments))
 	cpp_warning (pfile, "`/*' within comment");
-
-      prev_c = c;
     }
+
+  cpp_error_with_line (pfile, line, col, "unterminated comment");
+ out:
+  CPP_BUFFER (pfile)->cur = cur;
 }
 
 /* Skip a C++/Chill line comment.  We know it's a comment, and point


More information about the Gcc-patches mailing list