[net.sources] code for rn speedup by buffering

dave@lsuc.UUCP (David Sherman) (03/17/85)

Since I've had a large number of requests, I'm posting the
code to speed up rn by buffering output. Buffering is basically
done at every \n during article output, and at the end of every
printf. In other places I've added fflush'es where appropriate.
The changes below do the following:
	- redefine printf and fputs to versions which fflush (Dprintf, Dfputs)
	- setbuf on stdout in main()
	- redefine putchar to call fflush, and change instances
		of putchar where flushing isn't needed to putc on stdout

********** WARNING *************
The implementation of Dprintf is not portable to all machines.
You will have to check that it works. A simple test is the program
	#include <stdio.h>
	main()
	{
		try("testing %d %d %d... %s!\n", 1, 2, 3, "it works");
	}
	try(str, a,b,c,d,e,f,g,h,i,j,k,l,m)
		register char *str;
	{
		printf(str, a,b,c,d,e,f,g,h,i,j,k,l,m);
	}
If this doesn't work (it should work on all PDP-11s and VAXen,
and definitely works on Perkin-Elmers), then you'll need to add
fflush'es after many printf calls and remove the Dprintf stuff.
*********************************

Also, if you don't really want to implement the putchar->putc
changes all over the place, you can just do the ones in art.c
which involve spitting out the article itself. That's where
the speedup is most important, since most of the volume rn
produces is the text of articles.

Now, here goes with diff -c output. The files which have changed
are common.h, term.c, art.c, rn.c and Makefile. There is also a new file,
speedup.c, which appears first (IF YOU ARE RUNNING THIS THROUGH PATCH,
IT WILL NOT CATCH THIS!).

----------- speedup.c ----------------

#include <stdio.h>

/*
 *	code to speed up output of rn by buffering it
 *
 *	David Sherman, lsuc!dave, February 1985
 *
 *	Basic approach: setbuf on stdout; change all calls
 * 	to printf etc. to call this version, then fflush;
 *	add fflush'es where appropriate after putchars.
 */

Dprintf(str, a,b,c,d,e,f,g,h,i,j,k,l,m)
	register char *str;
	register a,b,c,d,e,f,g,h,i,j,k,l,m;
{
#undef printf	/* since it's compiled with -Dprintf=Dprintf */
	printf(str, a,b,c,d,e,f,g,h,i,j,k,l,m);
	fflush(stdout);
}

Dfputs(str, where)
	register char *str;
	register FILE *where;
{
#undef fputs
	fputs(str, where);
	fflush(where);
}
------------ end of speedup.c ----------------
---------------------------------------------
diff -c ./Makefile old/Makefile
*** ./Makefile	Sun Mar 17 08:04:35 1985
--- old/Makefile	Sun Mar 17 08:06:39 1985
***************
*** 23,29
  rnbin = /usr/bin
  rnlib = /usr/lib/news/rn
  mansrc = /usr/man/man1
- # additions for speedup added by David Sherman, Feb 1985
  CFLAGS =  -O -m -DNO_SCCS_ID
  LDFLAGS =  -k 12k -n
  

--- 23,28 -----
  rnbin = /usr/bin
  rnlib = /usr/lib/news/rn
  mansrc = /usr/man/man1
  CFLAGS =  -O -m -DNO_SCCS_ID
  LDFLAGS =  -k 12k -n
  
***************
*** 42,48
  c1 = addng.c art.c artio.c artsrch.c backpage.c bits.c cheat.c
  c2 = final.c head.c help.c init.c intrp.c kfile.c last.c ndir.c ng.c
  c3 = ngdata.c ngsrch.c ngstuff.c only.c rcln.c rcstuff.c
! c4 = respond.c rn.c search.c sw.c term.c util.c sysname.c speedup.c
  
  c = $(c1) $(c2) $(c3) $(c4)
  

--- 41,47 -----
  c1 = addng.c art.c artio.c artsrch.c backpage.c bits.c cheat.c
  c2 = final.c head.c help.c init.c intrp.c kfile.c last.c ndir.c ng.c
  c3 = ngdata.c ngsrch.c ngstuff.c only.c rcln.c rcstuff.c
! c4 = respond.c rn.c search.c sw.c term.c util.c sysname.c
  
  c = $(c1) $(c2) $(c3) $(c4)
  
***************
*** 49,55
  obj1 = addng.o art.o artio.o artsrch.o backpage.o bits.o cheat.o
  obj2 = final.o head.o help.o init.o intrp.o kfile.o last.o ndir.o ng.o
  obj3 = ngdata.o ngsrch.o ngstuff.o only.o rcln.o rcstuff.o
! obj4 = respond.o rn.o search.o sw.o term.o util.o sysname.o speedup.o
  
  obj = $(obj1) $(obj2) $(obj3) $(obj4)
  

--- 48,54 -----
  obj1 = addng.o art.o artio.o artsrch.o backpage.o bits.o cheat.o
  obj2 = final.o head.o help.o init.o intrp.o kfile.o last.o ndir.o ng.o
  obj3 = ngdata.o ngsrch.o ngstuff.o only.o rcln.o rcstuff.o
! obj4 = respond.o rn.o search.o sw.o term.o util.o sysname.o
  
  obj = $(obj1) $(obj2) $(obj3) $(obj4)
  
diff -c ./art.c old/art.c
*** ./art.c	Sun Mar 17 08:04:32 1985
--- old/art.c	Sun Mar 17 08:06:42 1985
***************
*** 139,145
  		    printf(" (%ld more",(long)i);
  		    if (dmcount)
  			printf(" + %ld Marked to return)",(long)dmcount);
! 		    putc(')', stdout);
  		}
  #else
  		if (i)

--- 139,145 -----
  		    printf(" (%ld more",(long)i);
  		    if (dmcount)
  			printf(" + %ld Marked to return)",(long)dmcount);
! 		    putchar(')');
  		}
  #else
  		if (i)
***************
*** 164,170
  	  linenum<(firstpage?initlines:(special?slines:LINES)) );
  	  linenum++) {		/* for each line on page */
  	    if (int_count) {	/* exit via interrupt? */
! 		putc('\n', stdout);	/* get to left margin */
  		int_count = 0;	/* reset interrupt count */
  		return DA_NORM;	/* skip out of loops */
  	    }

--- 164,170 -----
  	  linenum<(firstpage?initlines:(special?slines:LINES)) );
  	  linenum++) {		/* for each line on page */
  	    if (int_count) {	/* exit via interrupt? */
! 		putchar('\n');	/* get to left margin */
  		int_count = 0;	/* reset interrupt count */
  		return DA_NORM;	/* skip out of loops */
  	    }
***************
*** 236,242
  		if (!UG)
  		    putchar(' ');
  		underprint(s);	/* print subject underlined */
! 		putc('\n', stdout);	/* and finish the line */
  	    }
  	    else if (hide_this_line) {	/* do not print line? */
  		linenum--;		/* compensate for linenum++ */

--- 236,242 -----
  		if (!UG)
  		    putchar(' ');
  		underprint(s);	/* print subject underlined */
! 		putchar('\n');	/* and finish the line */
  	    }
  	    else if (hide_this_line) {	/* do not print line? */
  		linenum--;		/* compensate for linenum++ */
***************
*** 260,266
  			underline();
  		    }
  		    if (*bufptr == '\n')
! 			putc(' ', stdout);
  		}
  #ifdef INNERSEARCH
  		outputok = !hide_everything;

--- 260,266 -----
  			underline();
  		    }
  		    if (*bufptr == '\n')
! 			putchar(' ');
  		}
  #ifdef INNERSEARCH
  		outputok = !hide_everything;
***************
*** 310,319
  			    if (rotate && !in_header
  			      && isalpha(*bufptr)) {
  				if ((*bufptr & 31) <= 13)
! 				    putc(*bufptr+13, stdout);
! 				else {
! 				    putc(*bufptr-13, stdout);
! 				}
  			    }
  			    else
  #endif

--- 310,318 -----
  			    if (rotate && !in_header
  			      && isalpha(*bufptr)) {
  				if ((*bufptr & 31) <= 13)
! 				    putchar(*bufptr+13);
! 				else
! 				    putchar(*bufptr-13);
  			    }
  			    else
  #endif
***************
*** 317,323
  			    }
  			    else
  #endif
! 				putc(*bufptr, stdout);
  			}
  			if (*UC && ((highlight==artline && marking == 1)
  #ifdef ULSMARTS

--- 316,322 -----
  			    }
  			    else
  #endif
! 				putchar(*bufptr);
  			}
  			if (*UC && ((highlight==artline && marking == 1)
  #ifdef ULSMARTS
***************
*** 350,356
  			if (outputok)
  #endif
  			    putchar('\n');
- 			fflush(stdout);	/* flush at end of line - DS */
  			restart = 0;
  			outpos = 1000;	/* signal normal \n */
  		    }

--- 349,354 -----
  			if (outputok)
  #endif
  			    putchar('\n');
  			restart = 0;
  			outpos = 1000;	/* signal normal \n */
  		    }
***************
*** 358,364
  #ifdef INNERSEARCH
  			if (outputok)
  #endif
! 			    putc(*bufptr, stdout);
  			bufptr++;
  			outpos += 8 - outpos % 8;
  		    }

--- 356,362 -----
  #ifdef INNERSEARCH
  			if (outputok)
  #endif
! 			    putchar(*bufptr);
  			bufptr++;
  			outpos += 8 - outpos % 8;
  		    }
***************
*** 378,384
  			if (outputok)
  #endif
  			{
! 			    putc('^', stdout);
  			    if (highlight == artline && *UC && marking == 1) {
  				backspace();
  				underchar();

--- 376,382 -----
  			if (outputok)
  #endif
  			{
! 			    putchar('^');
  			    if (highlight == artline && *UC && marking == 1) {
  				backspace();
  				underchar();
***************
*** 382,388
  			    if (highlight == artline && *UC && marking == 1) {
  				backspace();
  				underchar();
! 				putc(*bufptr+64, stdout);
  				backspace();
  				underchar();
  			    }

--- 380,386 -----
  			    if (highlight == artline && *UC && marking == 1) {
  				backspace();
  				underchar();
! 				putchar(*bufptr+64);
  				backspace();
  				underchar();
  			    }
***************
*** 387,393
  				underchar();
  			    }
  			    else
! 				putc(*bufptr+64, stdout);
  			}
  			bufptr++;
  			outpos += 2;

--- 385,391 -----
  				underchar();
  			    }
  			    else
! 				putchar(*bufptr+64);
  			}
  			bufptr++;
  			outpos += 2;
diff -c ./common.h old/common.h
*** ./common.h	Sun Mar 17 08:17:09 1985
--- old/common.h	Sun Mar 17 08:17:17 1985
***************
*** 1,9
- /*
- 
- This comment intentionally left justified (groan).
- 
- */
- /* MSB01 - generate rnhead temp file in /tmp */
  #include "config.h"	/* generated by installation script */
  #ifdef WHOAMI
  #    include <whoami.h>

--- 1,3 -----
  #include "config.h"	/* generated by installation script */
  #ifdef WHOAMI
  #    include <whoami.h>
***************
*** 10,23
  #endif
  
  #include <stdio.h>
- 
- /* DMS01 - Feb 1985 - buffering for speedup */
- #define printf	Dprintf
- #define fputs	Dfputs
- #undef putchar	/* replace the one define by stdio.h above */
- #define putchar(x)	putc(x,stdout),fflush(stdout)
- /* end DMS01 Feb 1985 speedup improvement */
- 
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <ctype.h>

--- 4,9 -----
  #endif
  
  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <ctype.h>
***************
*** 411,418
  
  /* file to pass header to followup article poster */
  #ifndef HEADNAME		/* % and ~ */
! /* MSB01: formerly	#   define HEADNAME "~/.rnhead" */
! #   define HEADNAME "/tmp/rnhead.%$"
  #endif
  
  #ifndef MAKEDIR

--- 397,404 -----
  
  /* file to pass header to followup article poster */
  #ifndef HEADNAME		/* % and ~ */
! #   define HEADNAME "~/.rnhead"
! /* or alternately #define HEADNAME "/tmp/rnhead.%$" */
  #endif
  
  #ifndef MAKEDIR
Only in .: old
diff -c ./rn.c old/rn.c
*** ./rn.c	Sun Mar 17 08:04:33 1985
--- old/rn.c	Sun Mar 17 08:06:45 1985
***************
*** 51,57
      ;
  }
  
- char BUFstdout[BUFSIZ];
  void
  main(argc,argv)
  int argc;

--- 51,56 -----
      ;
  }
  
  void
  main(argc,argv)
  int argc;
***************
*** 60,67
      bool foundany = initialize(argc,argv);
      register char *s;
      
- 	/* David Sherman, Feb 1985: buffer stdout */
- 	setbuf(stdout, BUFstdout);
      if (maxngtodo)
  	starthere = 0;
      else if (!foundany) {		/* nothing to do? */

--- 59,64 -----
      bool foundany = initialize(argc,argv);
      register char *s;
      
      if (maxngtodo)
  	starthere = 0;
      else if (!foundany) {		/* nothing to do? */
Only in .: speedup.c
diff -c ./term.c old/term.c
*** ./term.c	Sun Mar 17 08:04:32 1985
--- old/term.c	Sun Mar 17 08:06:47 1985
***************
*** 174,180
  putchr(ch)
  register char ch;
  {
!     putc(ch,stdout);	/* generates fflush(stdout) automatically - DMS */
  }
  
  /* input the 2nd and succeeding characters of a multi-character command */

--- 174,180 -----
  putchr(ch)
  register char ch;
  {
!     putchar(ch);
  }
  
  /* input the 2nd and succeeding characters of a multi-character command */
***************
*** 192,199
      do {
        top:
  	if (*s < ' ') {
! 	    putc('^', stdout);
! 	    putc(*s | 64, stdout);
  	}
  	else
  	    putc(*s, stdout);		/* echo previous character */

--- 192,199 -----
      do {
        top:
  	if (*s < ' ') {
! 	    putchar('^');
! 	    putchar(*s | 64);
  	}
  	else
  	    putchar(*s);		/* echo previous character */
***************
*** 196,202
  	    putc(*s | 64, stdout);
  	}
  	else
! 	    putc(*s, stdout);		/* echo previous character */
  	s++;
  re_read:
  	fflush(stdout);

--- 196,202 -----
  	    putchar(*s | 64);
  	}
  	else
! 	    putchar(*s);		/* echo previous character */
  	s++;
  re_read:
  	fflush(stdout);
***************
*** 285,292
  	    goto re_read;
  	}
  	else if (*s == Ctl('v')) {
! 	    putc('^', stdout);
! 	    backspace(); /* tputs->putchr->putchar->fflush so flushes - DS */
  	    getcmd(s);
  	    goto top;
  	}

--- 285,292 -----
  	    goto re_read;
  	}
  	else if (*s == Ctl('v')) {
! 	    putchar('^');
! 	    backspace();
  	    getcmd(s);
  	    goto top;
  	}
***************
*** 520,529
  #endif
  	    underline();
  	}
- 	/* David Sherman, Feb 85: mod to call putc instead
- 	 * of putchar here, so flushing only happens at the
- 	 * end of a line or block or whatever
- 	 */
  	for (i=0; i<COLS; i++) {
  	    if (!*s)
  		break;

--- 520,525 -----
  #endif
  	    underline();
  	}
  	for (i=0; i<COLS; i++) {
  	    if (!*s)
  		break;
***************
*** 528,534
  	    if (!*s)
  		break;
  	    if (*s >= ' ')
! 		putc(*s, stdout);
  	    else if (*s == '\t') {
  		putc(*s, stdout);
  		i = ((i+8) & ~7) - 1; 

--- 524,530 -----
  	    if (!*s)
  		break;
  	    if (*s >= ' ')
! 		putchar(*s);
  	    else if (*s == '\t') {
  		putchar(*s);
  		i = ((i+8) & ~7) - 1; 
***************
*** 530,536
  	    if (*s >= ' ')
  		putc(*s, stdout);
  	    else if (*s == '\t') {
! 		putc(*s, stdout);
  		i = ((i+8) & ~7) - 1; 
  	    }
  	    else if (*s == '\n') {

--- 526,532 -----
  	    if (*s >= ' ')
  		putchar(*s);
  	    else if (*s == '\t') {
! 		putchar(*s);
  		i = ((i+8) & ~7) - 1; 
  	    }
  	    else if (*s == '\n') {
***************
*** 538,545
  	    }
  	    else {
  		i++;
! 		putc('^', stdout);
! 		putc(*s + 64, stdout);
  	    }
  	    s++;
  	}

--- 534,541 -----
  	    }
  	    else {
  		i++;
! 		putchar('^');
! 		putchar(*s + 64);
  	    }
  	    s++;
  	}
***************
*** 543,549
  	    }
  	    s++;
  	}
- 	fflush(stdout);
  	if (i) {
  	    if (hilite == STANDOUT)
  		un_standout();

--- 539,544 -----
  	    }
  	    s++;
  	}
  	if (i) {
  	    if (hilite == STANDOUT)
  		un_standout();
***************
*** 565,571
      register int i;
  
      for (i = num; i; --i)
! 	putc(PC, stdout);
      fflush(stdout);
  }
  

--- 560,566 -----
      register int i;
  
      for (i = num; i; --i)
! 	putchar(PC);
      fflush(stdout);
  }
  
***************
*** 577,584
  {
      if (verify && buf[1] == FINISHCMD) {
  	if (*buf < ' ') {
! 	    putc('^', stdout);
! 	    putc(*buf | 64, stdout);
  	    backspace();
  	    backspace();
  	}

--- 572,579 -----
  {
      if (verify && buf[1] == FINISHCMD) {
  	if (*buf < ' ') {
! 	    putchar('^');
! 	    putchar(*buf | 64);
  	    backspace();
  	    backspace();
  	}
***************
*** 583,589
  	    backspace();
  	}
  	else {
! 	    putc(*buf, stdout);
  	    backspace();
  	}
  	fflush(stdout);

--- 578,584 -----
  	    backspace();
  	}
  	else {
! 	    putchar(*buf);
  	    backspace();
  	}
  	fflush(stdout);
***************
*** 595,601
  rubout()
  {
      backspace();			/* do the old backspace, */
!     putc(' ', stdout);			/*   space, */
      backspace();			/*     backspace trick */
  }
  

--- 590,596 -----
  rubout()
  {
      backspace();			/* do the old backspace, */
!     putchar(' ');			/*   space, */
      backspace();			/*     backspace trick */
  }
  
***************
*** 607,614
      fputs("^R\n",stdout);
      for (s = buf; *s; s++) {
  	if (*s < ' ') {
! 	    putc('^', stdout);
! 	    putc(*s | 64, stdout);
  	}
  	else
  	    putc(*s, stdout);

--- 602,609 -----
      fputs("^R\n",stdout);
      for (s = buf; *s; s++) {
  	if (*s < ' ') {
! 	    putchar('^');
! 	    putchar(*s | 64);
  	}
  	else
  	    putchar(*s);
***************
*** 611,617
  	    putc(*s | 64, stdout);
  	}
  	else
! 	    putc(*s, stdout);
      }
      fflush(stdout);
  }

--- 606,612 -----
  	    putchar(*s | 64);
  	}
  	else
! 	    putchar(*s);
      }
  }
  
***************
*** 613,618
  	else
  	    putc(*s, stdout);
      }
-     fflush(stdout);
  }
  

--- 608,612 -----
  	else
  	    putchar(*s);
      }
  }
  
-- 
{utzoo pesnta nrcaero utcs hcr}!lsuc!dave
{allegra decvax ihnp4 linus}!utcsri!lsuc!dave