[net.unix-wizards] Possible TROFF speedup

jbn@wdl1.UUCP (08/23/85)

     In n3.c of device independent TROFF, there is a CPP variable INCORE
which if defined seems to turn on some I/O optimizations.  But I don't have
the time to trace through the miserable and uncommented code in TROFF to
figure out what is really going on.  But someone with more time might 
find it useful to define INCORE, build TROFF, and see what happens.
Maybe things will speed up.

				John Nagle

guy@sun.uucp (Guy Harris) (08/25/85)

>      In n3.c of device independent TROFF, there is a CPP variable INCORE
> which if defined seems to turn on some I/O optimizations.  But I don't have
> the time to trace through the miserable and uncommented code in TROFF to
> figure out what is really going on.  But someone with more time might 
> find it useful to define INCORE, build TROFF, and see what happens.
> Maybe things will speed up.

If it's anything like the INCORE in the S3/S5 TROFF, it won't speed up.
INCORE is for large virtual-memory systems which aren't running UNIX.  They
do "malloc" instead of "sbrk", and use standard I/O instead of
"read"/"write".  This (especially "malloc") slows things down quite a bit.
I whipped up a VMUNIX option for TROFF once that was based on INCORE but 1)
supported compressed macro packages and 2) used "sbrk" and "read"/"write".
"time"ing that TROFF (NROFF, actually) vs. older ones and vs. 4.2BSD's NROFF
indicated that it was faster; however, the output was bursty, rather than
continuous as 4.2BSD's NROFF output was, so it seemed slower.  Go figure.

	Guy Harris

mckay@pur-ee.UUCP (Dwight D McKay) (08/26/85)

The INCORE #define is only half implimented in the release of DWB we have
(version 1.0).  Defining it will cause troff to use an incore memory buffer
AND setup a tempfile.  Hardly a speed-up.

--Dwight Mckay, ECN Software Support

chris@umcp-cs.UUCP (Chris Torek) (08/27/85)

I had a set of n/troff changes for 4.1BSD that gave a ~10% improvement,
as I recall.  Let me see if I can find them....  Ah, here we go.
Please note that these are for 4.1BSD; I haven't reinstalled them
in 4.2 myself (perhaps someone else has), and we don't run System N.

First, add the following at the end of tdef.h:
-------------------------------------------------------------------
/* NOTE: THIS WON'T WORK UNDER NON-VMUNIX!! */
/* Troff spends about 20% of its time in getch() and getch0(); this
   is an attempt to speed that up. */
#ifdef	FAST
int	_ch_, ch, nlflg, ch0, nchar, rchar, app;
int	*rbuf, Buf[NBLIST*BLK + NEV*EVS], *olinep;
filep	roff;

#define	getch()	(ch?((((_ch_=ch)&CMASK)=='\n'?nlflg++:0),ch=0,_ch_):\
		    nlflg?'\n':_getch_())
#define	getch0() (ch0?(_ch_=ch0,ch0=0,_ch_):nchar?(--nchar,rchar):_getch0_())
#define	rbf0(p) (((p)&~(BLK-1))==roff?rbuf[(p)&(BLK-1)]:\
		 (roff=(p)&(~(BLK-1)),rbuf= &Buf[roff],rbuf[p&(BLK-1)]))
#define	rbf()	(((_ch_=rbf0(ip))==0?(app?0:(_ch_=popi()))\
				    :(ip=incoff(ip))),_ch_)
#define wbt(i)	(wbf(i),wbfl())

#ifdef NROFF
int oline[LNSIZE+1];

#define ptout(i) ((olinep>= &oline[LNSIZE]?0:(*olinep++=(i))),\
		  ((i)&CMASK)=='\n'?_ptout_(i):0)
#else (TROFF)
#define	ptout(i) (((i)&CMASK)!='\n'?*olinep++=(i):_ptout_())
#endif NROFF

#endif	FAST
-------------------------------------------------------------------
Now, the RCS diffs for the other files.  There are some other changes
in here; you can try using just the code that is conditional on FAST,
if you wish.

RCS file: RCS/n1.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -c2 -r1.1 -r1.2
*** /tmp/,RCSt1001198	Tue Aug 27 07:37:26 1985
--- /tmp/,RCSt2001198	Tue Aug 27 07:37:28 1985
***************
*** 142,145
  int acctf;
  #endif
  
  main(argc,argv)

--- 142,146 -----
  int acctf;
  #endif
+ char *getenv();
  
  main(argc,argv)
***************
*** 250,253
  			dpn = 0;
  			continue;
  #endif
  		default:

--- 251,260 -----
  			dpn = 0;
  			continue;
+ 		case 'F':
+ 			{
+ 			  extern char *fontfile;
+ 			  fontfile = &argv[0][2];
+ 			}
+ 			continue;
  #endif
  		default:
***************
*** 418,421
  {
  	static int mode;
  
  	if (ttyp==0)

--- 425,430 -----
  {
  	static int mode;
+ 	char *tbptr;
+ 	char tbuf[1024];
  
  	/* 11-3-83 EMA & BNI
***************
*** 419,422
  	static int mode;
  
  	if (ttyp==0)
  		return;

--- 428,440 -----
  	char tbuf[1024];
  
+ 	/* 11-3-83 EMA & BNI
+ 	   Added following check so that messages are only turned off
+ 	   if the output is going to the terminal, and the terminal
+ 	   is a hard copy terminal or of unknown type */
+ 
+ 	tbptr = getenv ("TERM");
+ 	if (! isatty(2) || (tgetent (tbuf,tbptr) > 0) && ! tgetflag("hc"))
+ 		return;
+ 
  	if (ttyp==0)
  		return;
***************
*** 449,453
  {
  	register i,j;
! 	extern filep boff();
  
  	i = a;

--- 467,471 -----
  {
  	register i,j;
! /*	extern filep boff(); */
  
  	i = a;
***************
*** 473,477
  	return(i);
  }
! getch(){
  	register int i, j, k;
  

--- 491,497 -----
  	return(i);
  }
! 
! #ifndef	FAST
! getch () {
  	register int i, j, k;
  
***************
*** 477,480
  
  	level++;
  g0:
  	if(ch){

--- 497,509 -----
  
  	level++;
+ #else	FAST
+ _getch_ () {
+ 	register int i, j, k;
+ 
+ 	level++;
+ 	/* when we get here we know ch and nlflag are both zero */
+ 	goto gfast;
+ #endif	FAST
+ 
  g0:
  	if(ch){
***************
*** 490,493
  	}
  
  	if((k = (i = getch0()) & CMASK) != ESC){
  		if(i & MOT)goto g2;

--- 519,523 -----
  	}
  
+ gfast:
  	if((k = (i = getch0()) & CMASK) != ESC){
  		if(i & MOT)goto g2;
***************
*** 702,706
  }
  char ifilt[32] = {0,001,002,003,0,005,006,007,010,011,012};
! getch0(){
  	register int i, j;
  

--- 732,738 -----
  }
  char ifilt[32] = {0,001,002,003,0,005,006,007,010,011,012};
! 
! #ifndef	FAST
! getch0 () {
  	register int i, j;
  
***************
*** 707,710
  	if(ch0){i=ch0; ch0=0; return(i);}
  	if(nchar){nchar--; return(rchar);}
  
  again:

--- 739,746 -----
  	if(ch0){i=ch0; ch0=0; return(i);}
  	if(nchar){nchar--; return(rchar);}
+ #else	FAST
+ _getch0_ () {
+ 	register int i, j;
+ #endif	FAST
  
  again:
===================================================================
RCS file: RCS/n3.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -c2 -r1.1 -r1.2
*** /tmp/,RCSt1001198	Tue Aug 27 07:37:36 1985
--- /tmp/,RCSt2001198	Tue Aug 27 07:37:37 1985
***************
*** 16,19
  */
  
  unsigned blist[NBLIST];
  extern struct s *frame, *stk, *nxf;

--- 16,22 -----
  */
  
+ #define	blisti(i)	(((i)-NEV*EVS)/(BLK))
+ #define	boff(i)		(((filep)i)*BLK + NEV*EVS)
+ 
  unsigned blist[NBLIST];
  extern struct s *frame, *stk, *nxf;
***************
*** 134,138
  }
  findmn(i)
! int i;
  {
  	register j;

--- 137,141 -----
  }
  findmn(i)
! register i;
  {
  	register j;
***************
*** 137,140
  {
  	register j;
  
  	for(j=0;j<NM;j++){

--- 140,144 -----
  {
  	register j;
+ 	register struct contab *p;
  
  	for (p = contab; p < &contab[NM]; p++)
***************
*** 138,145
  	register j;
  
! 	for(j=0;j<NM;j++){
! 		if(i == (contab[j].rq & ~MMASK))break;
! 	}
! 	if(j==NM)j = -1;
  	return(j);
  }

--- 142,151 -----
  	register struct contab *p;
  
! 	for (p = contab; p < &contab[NM]; p++)
! 		if (i == (p->rq & ~MMASK))
! 			break;
! 	j = p - contab;
! 	if (j == NM)
! 		j = -1;
  	return(j);
  }
***************
*** 147,151
  int i;
  {
- 	extern filep boff();
  	if(i >= 0){
  		if(contab[i].rq & MMASK)ffree(((filep)contab[i].x.mx)<<BLKBITS);

--- 153,156 -----
  int i;
  {
  	if(i >= 0){
  		if(contab[i].rq & MMASK)ffree(((filep)contab[i].x.mx)<<BLKBITS);
***************
*** 158,162
  {
  	register i;
- 	extern filep boff();
  	register filep savip;
  	extern filep alloc();

--- 163,166 -----
  {
  	register i;
  	register filep savip;
  	extern filep alloc();
***************
*** 274,278
  {
  	register i;
- 	extern filep boff();
  	filep j;
  

--- 278,281 -----
  {
  	register i;
  	filep j;
  
***************
*** 299,307
  	blist[j] = 0;
  }
! filep boff(i)
! int i;
! {
! 	return(((filep)i)*BLK + NEV*EVS);
! }
  wbt(i)
  int i;

--- 302,306 -----
  	blist[j] = 0;
  }
! #ifndef	FAST
  wbt(i)
  int i;
***************
*** 310,313
  	wbfl();
  }
  wbf(i)
  int i;

--- 309,313 -----
  	wbfl();
  }
+ #endif	FAST
  wbf(i)
  int i;
***************
*** 346,354
  	woff = 0;
  }
! blisti(i)
! filep i;
! {
! 	return((i-NEV*EVS)/(BLK));
! }
  rbf(){
  	register i;

--- 346,351 -----
  	woff = 0;
  }
! 
! #ifndef	FAST
  rbf(){
  	register i;
***************
*** 362,365
  	return(i);
  }
  rbf0(p)
  filep p;

--- 359,363 -----
  	return(i);
  }
+ 
  rbf0(p)
  filep p;
***************
*** 378,381
  	return(rbuf[p & (BLK-1)]);
  }
  filep incoff(p)
  filep p;

--- 376,381 -----
  	return(rbuf[p & (BLK-1)]);
  }
+ #endif	FAST
+ 
  filep incoff(p)
  register filep p;
***************
*** 379,383
  }
  filep incoff(p)
! filep p;
  {
  	register i;

--- 379,383 -----
  
  filep incoff(p)
! register filep p;
  {
  	register i;
***************
*** 382,388
  {
  	register i;
! 	register filep j;
! 	if(!((j = (++p)) & (BLK-1))){
! 		if((i = blist[blisti(--p)]) == -1){
  			prstr("Bad storage allocation.\n");
  			done2(-5);

--- 382,388 -----
  {
  	register i;
! 
! 	if (!(++p & (BLK-1))) {
! 		if ((i = blist[blisti(--p)]) == -1) {
  			prstr("Bad storage allocation.\n");
  			done2(-5);
***************
*** 388,392
  			done2(-5);
  		}
! 		j = ((filep)i)<<BLKBITS;
  	}
  	return(j);

--- 388,392 -----
  			done2(-5);
  		}
! 		p = ((filep) i) << BLKBITS;
  	}
  	return p;
***************
*** 390,394
  		j = ((filep)i)<<BLKBITS;
  	}
! 	return(j);
  }
  popi(){

--- 390,394 -----
  		p = ((filep) i) << BLKBITS;
  	}
! 	return p;
  }
  
***************
*** 392,395
  	return(j);
  }
  popi(){
  	register struct s *p;

--- 392,396 -----
  	return p;
  }
+ 
  popi(){
  	register struct s *p;
===================================================================
RCS file: RCS/t6.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -c2 -r1.1 -r1.2
*** /tmp/,RCSt1001198	Tue Aug 27 07:37:42 1985
--- /tmp/,RCSt2001198	Tue Aug 27 07:37:44 1985
***************
*** 49,54
  extern int ch0;
  extern int lg;
! char fontfile[] = "/usr/lib/font/ftXX";
! int ffi = 16;
  extern int bd;
  extern int level;

--- 49,54 -----
  extern int ch0;
  extern int lg;
! char *fontfile = "/usr/lib/font/ftXX";
! int ffi = 0;
  extern int bd;
  extern int level;
***************
*** 110,114
  }
  getcw(i)
! int i;
  {
  	register j,k;

--- 110,114 -----
  }
  getcw(i)
! register i;
  {
  	register j,k;
***************
*** 506,509
  	if(((i = (getch() & CMASK) - '0' -1) < 0) || (i >3)){prstr("fp: bad font position\n"); return;}
  	if(skip() || !(j = getrq())){prstr("fp: no font name\n"); return;}
  	fontfile[ffi] = j & BMASK;
  	fontfile[ffi+1] = j>>BYTE;

--- 506,512 -----
  	if(((i = (getch() & CMASK) - '0' -1) < 0) || (i >3)){prstr("fp: bad font position\n"); return;}
  	if(skip() || !(j = getrq())){prstr("fp: no font name\n"); return;}
+ 	if (ffi == 0)
+ 		while (fontfile[ffi] != 'X')
+ 			ffi++;
  	fontfile[ffi] = j & BMASK;
  	fontfile[ffi+1] = j>>BYTE;
===================================================================
RCS file: RCS/n10.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -c2 -r1.1 -r1.2
*** /tmp/,RCSt1001198	Tue Aug 27 07:37:48 1985
--- /tmp/,RCSt2001198	Tue Aug 27 07:37:49 1985
***************
*** 82,88
  	}
  }
! ptout(i)
! int i;
! {
  	*olinep++ = i;
  	if(olinep >= &oline[LNSIZE])olinep--;

--- 82,88 -----
  	}
  }
! 
! #ifndef	FAST
! ptout (i) int i; {
  	*olinep++ = i;
  	if(olinep >= &oline[LNSIZE])olinep--;
***************
*** 88,91
  	if(olinep >= &oline[LNSIZE])olinep--;
  	if((i&CMASK) != '\n')return;
  	olinep--;
  	lead += dip->blss + lss - t.Newline;

--- 88,94 -----
  	if(olinep >= &oline[LNSIZE])olinep--;
  	if((i&CMASK) != '\n')return;
+ #else	FAST
+ _ptout_ () {
+ #endif	FAST
  	olinep--;
  	lead += dip->blss + lss - t.Newline;
***************
*** 144,147
  		}
  	}
  	if(xfont == ulfont){
  		for(k=w/t.Char;k>0;k--)oput('_');

--- 147,157 -----
  		}
  	}
+ 
+ 	/*
+ 	 * This next section of code was moved to after the characters are
+ 	 * output, so that underlining follows the characters, which is the
+ 	 * way the Printronix printer likes it. 2/24/81 FLB
+ 	 *
+ 
  	if(xfont == ulfont){
  		for(k=w/t.Char;k>0;k--)oput('_');
***************
*** 148,151
  		for(k=w/t.Char;k>0;k--)oput('\b');
  	}
  	while(*codep != 0){
  		if(*codep & 0200){

--- 158,165 -----
  		for(k=w/t.Char;k>0;k--)oput('\b');
  	}
+ 
+ 	 *
+ 	 */
+ 
  	while(*codep != 0){
  		if(*codep & 0200){
***************
*** 161,164
  	}
  	if(!w)for(k=phyw/t.Char;k>0;k--)oput('\b');
  	}
  }

--- 175,189 -----
  	}
  	if(!w)for(k=phyw/t.Char;k>0;k--)oput('\b');
+ 
+ 	/*
+ 	 * Do underlining here.
+ 	 * See previous comment as regards underlining.
+ 	 */
+ 
+ 	if(xfont == ulfont){
+ 		for(k=w/t.Char;k>0;k--)oput('\b');
+ 		for(k=w/t.Char;k>0;k--)oput('_');
+ 	}
+ 
  	}
  }
===================================================================
RCS file: RCS/t10.c,v
retrieving revision 1.1
retrieving revision 1.3
diff -c2 -r1.1 -r1.3
*** /tmp/,RCSt1001198	Tue Aug 27 07:37:52 1985
--- /tmp/,RCSt2001198	Tue Aug 27 07:37:53 1985
***************
*** 66,70
  	oput(0140); /*some initial lead*/
  }
! ptout(i)
  int i;
  {

--- 66,75 -----
  	oput(0140); /*some initial lead*/
  }
! 
! #ifndef	FAST
! ptout (i)
! #else	FAST
! _ptout_ (i)
! #endif	FAST
  int i;
  {
***************
*** 73,76
  	int psl[16];
  
  	if((i & CMASK) != '\n'){
  		*olinep++ = i;

--- 78,82 -----
  	int psl[16];
  
+ #ifndef	FAST
  	if((i & CMASK) != '\n'){
  		*olinep++ = i;
***************
*** 77,80
  		return;
  	}
  	if(olinep == oline){
  		lead += lss;

--- 83,87 -----
  		return;
  	}
+ #endif	FAST
  	if(olinep == oline){
  		lead += lss;
***************
*** 149,153
  }
  ptout0(i)
! int i;
  {
  	register j, k, w;

--- 156,160 -----
  }
  ptout0(i)
! register int i;
  {
  	register j, k, w;
***************
*** 275,279
  	while(k > 0){
  		if((i=127) > k)i = k;
! 		if(((j = (esct + i*(1-2*escm))) > (46*72+18-T_IESC)) ||
  		   (j < 0))break;
  /*

--- 282,290 -----
  	while(k > 0){
  		if((i=127) > k)i = k;
! 		/*
! 		 * the magic number in the next line is
! 		 * # of inches max line length * #picas/inch * #bu's/pica
! 		 */
! 		if(((j = (esct + i*(1-2*escm))) > (12*6*72+18-T_IESC)) ||
  		   (j < 0))break;
  /*
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland