🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #7441

Re: TIN is a huge pig -- still -- IS THERE A FIX FOR THIS COMING?

#7441
From: Steve Alvey
Date: Thu, 20 Jan 1994 10:33
494 lines
15593 bytes
Karl Denninger (karl@MCS.COM) wrote:
> In article <1994Jan19.072707.540@pony.ingres.com>,
> Sergio L Aponte <sergio@Ingres.COM> wrote:
> >

> Note also that this is a bug specific to 1.2 (PL2 even perhaps), and that
> PL2 is the only one which properly understands .overview files.


Yes we were exhibiting this behaviour when new newsgroups were appearing, I looked
deeper into the problem, and realised that the way the software sensed new
newsgroups and bogus newsgroups was rather memory inefficient.
 The descriptive comment from the code is shown  below.

SPA: my code only creates one sorted array (sorted_active), it then
uses bsearch (binary search) for each line read from the 'old'
active file, if it is found then the newsgroup is in both the old
and new active files. Mark it as visited in the active list. If it is
not found then it is a bogus group. After all 'old' groups have
been read we loop round the active lst to see is there are any that
have not been 'visited', these are the new groups.

The code used to work by creating two sorted lists of the 'old' and
'new' active. It then stepped through the 'old' list to match with
the new. The theory behind this operation is that the number of
comparisons is proportional to the number of groups. The
disadvantage with my approach is that the number of comparisons is
larger, a binary search does on average log2(n) searches (e.g. 8
comparisons for 4000 entries. However my approach also does one
less qsort, which is a big win. (not sure how many comparisons qsort does)


Note the following unofficial patch is only for VERSION 1.2, PATCHLEVEL 2. It uses
bsearch(), which is available on most systems, or from your favorite FTP site.
[n.b: i've only built it using gcc]

I recommend you change your patchlev.h file to signify unofficial patch level
status. e.g. 

#define PATCHLEVEL      "2 (SPA)"

Please Mail me (spa@galadriel.bt.co.uk) if you use this patch / have problems



---------------8<---------8<----------8<--------------------------------------
*** tin.h.orig	Fri Oct  1 15:11:47 1993
--- tin.h	Fri Dec  3 10:44:04 1993
***************
*** 908,918 ****
  	char *description;		/* text from LIBDIR/newsgroups file */
  	char *spooldir;			/* groups spool directory */
  	char moderated;			/* state of group moderation */
  	long max;			/* max. article number */
  	long min;			/* min. article number */
- 	int type;			/* grouptype - newsgroup / mailbox */
  	int next;			/* next active entry in hash chain */
- 	int my_group;			/* subscribed/unsubscribed to group */
  	int unread;			/* unread articles in group */
  	long newsrcmax;			/* newsrc max (lastmax) */
  	long newsrcmin;			/* newsrc min (lastmin) */
--- 908,919 ----
  	char *description;		/* text from LIBDIR/newsgroups file */
  	char *spooldir;			/* groups spool directory */
  	char moderated;			/* state of group moderation */
+ 	unsigned type:2;		/* grouptype - newsgroup / mailbox */
+ 	unsigned visit:1;		/* 1 means exists in both active files*/
+ 	unsigned  my_group:2;		/* subscribed/unsubscribed to group */
  	long max;			/* max. article number */
  	long min;			/* min. article number */
  	int next;			/* next active entry in hash chain */
  	int unread;			/* unread articles in group */
  	long newsrcmax;			/* newsrc max (lastmax) */
  	long newsrcmin;			/* newsrc min (lastmin) */
***************
*** 1032,1051 ****
      long	tzone;
  } TIMEINFO;
  
- /*
-  * Used for detecting new groups when reading news locally.  It's easy to be 
-  * confused by arrays of pointers to pointers, so typedef's are used for the 
-  * first level pointers to keep it clearer.
-  */
  
- struct t_notify
- {
- 	char name[PATH_LEN];
- 	int visited;
- };
- 
  typedef struct t_group *group_p;
- typedef struct t_notify *notify_p;
  
  /*
   * Determine signal return type
--- 1033,1040 ----
*** active.c.orig	Tue Sep 28 12:43:04 1993
--- active.c	Fri Dec  3 10:01:28 1993
***************
*** 19,24 ****
--- 19,25 ----
  int group_hash[TABLE_SIZE];			/* group name --> active[] */
  int reread_active_file = FALSE;
  int active_index = -1;
+ char *index(char*,char);
  
  /*
   *  Compare two pointers to "group_t" structures - used in qsort.
***************
*** 32,50 ****
  	return (strcmp ((*group1)->name, (*group2)->name));
  }
  
- /*
-  *  Compare two pointers to "notify_t" structures - used in qsort.
-  */
  
- int
- cmp_notify_p (notify1, notify2)
- 	notify_p *notify1;
- 	notify_p *notify2;
- {
- 	return (strcmp ((*notify1)->name, (*notify2)->name));
- }
- 
- 
  /*
   *  Get default array size for active[] from environment (AmigaDOS)
   *  or just return the standard default.
--- 33,39 ----
***************
*** 431,437 ****
--- 420,429 ----
  	char old_active_file_attribute[LEN];
  	char buf[LEN], *ptr;
  	FILE *fp = (FILE *) 0;
+ 	group_p *found = (group_p *) 0;
  	group_p *sorted_active = (group_p *) 0;
+ 	struct t_group tgroup;
+ 	group_p p_group = &tgroup;
  	int group_not_found;
  	int ch, num = 0;	/* num == num_old_active groups */
  	int ch_default = 'n';
***************
*** 441,448 ****
  	int old_active_size;
  	long epoch;
  	int compared;
- 	notify_p old_active = (notify_p) 0;
- 	notify_p *sorted_old_active = (notify_p *) 0;
  	char *autosubscribe, *autounsubscribe;
  	register int i, j;
  	struct stat sb;
--- 433,438 ----
***************
*** 514,549 ****
  
  	Raw (TRUE);
  
- 	old_active = (notify_p) my_malloc ((unsigned) sizeof (struct t_notify) * max_old_active);
- 	if (old_active == (notify_p) 0) {
- 		error_message (txt_out_of_memory, progname);
- 		goto notify_groups_done;
- 	}
  
- 	while (fgets (buf, sizeof (buf), fp) != NULL) {
- 		if (buf[0] == '.') {
- 			break;
- 		}
- 		strncpy (old_active[num].name, buf, sizeof (old_active[num].name));
- 		ptr = old_active[num].name;
- 		while (*ptr && *ptr != ' ' && *ptr != '\n') {
- 			ptr++;
- 		}
- 		*ptr = '\0';
- 		old_active[num].visited = FALSE;
- 		num++;
- 		if (num >= max_old_active) {
- 			max_old_active= max_old_active + (max_old_active / 2);
- 			old_active= (notify_p) my_realloc(
- 				(char *) old_active,
- 				(unsigned) sizeof(struct t_notify) * max_old_active);
- 			if (old_active == (notify_p) 0) {
- 				error_message (txt_out_of_memory, progname);
- 				goto notify_groups_done;
- 			}
- 		}
- 	}
- 
  	/* 
  	 * Check if there are user-set groups to be 
  	 * automatically subscribed or unsubscribed.
--- 504,510 ----
***************
*** 550,588 ****
  	 */
  	autosubscribe = getenv ("AUTOSUBSCRIBE");
  	autounsubscribe = getenv ("AUTOUNSUBSCRIBE");
! 
! 	if (read_news_via_nntp) {
! 		for (i = 0 ; i < num ; i++) {
! 			if (find_group_index (old_active[i].name) == -1) {
! 				continue;
! 			}
! 			if (! prompt_subscribe_group (old_active[i].name,
! 				autosubscribe, autounsubscribe)) {
! 				if (cmd_line) {
! 					printf ("\r\n");
! 					fflush (stdout);
! 				}
! 				goto notify_groups_done;
! 			}
! 		}
! 	} else {	
! 		wait_message (txt_checking_active_file);
! 		/*
! 		 * The following code replaces the original check for new newsgroups,
! 		 * a source of much aggravation.
! 		 *
! 		 * The original algorithm compared each group name in active to (on
! 		 * average) 1/2 the groups in old_active, for ~N*N/2 string compares.
! 		 * This works out to ~~ 8,000,000 comparisons for 4000 newsgroups.
! 		 *
! 		 * The new algorithm allocates an array of pointers to the active
! 		 * and old_active array elements, sorts each pointer array with
! 		 * quicksort, then scans through the two arrays in 1 pass; this takes
! 		 * 2*N*Log2(N) string compares for the sorts, N for the final pass.
! 		 * This should work out to ~~ 100,000 comparisons for the same 4000
! 		 * newsgroups, a BIG, BIG win.
! 		 */
! 
  		/* 
  		 * Allocate arrays to be sorted 
  		 */
--- 511,518 ----
  	 */
  	autosubscribe = getenv ("AUTOSUBSCRIBE");
  	autounsubscribe = getenv ("AUTOUNSUBSCRIBE");
! 	wait_message (txt_checking_active_file);
! 	if(!read_news_via_nntp) {
  		/* 
  		 * Allocate arrays to be sorted 
  		 */
***************
*** 592,681 ****
  			error_message (txt_out_of_memory, progname);
  			goto notify_groups_done;
  		}
- 		sorted_old_active = (notify_p *) my_malloc (
- 			(unsigned) sizeof (notify_p) * num);
- 		if (sorted_old_active == (notify_p *) 0) {
- 			error_message (txt_out_of_memory, progname);
- 			goto notify_groups_done;
- 		}
- 
  		/* 
  		 * Fill arrays to be sorted 
  		 */
  		for (i = 0 ; i < num_active ; i++) {
  			sorted_active[i] = &active[i];
  		}
- 		for (j = 0 ; j < num ; j++) {
- 			sorted_old_active[j] = &old_active[j];
- 		}
  
  		/* 
! 		 * Sort the arrays 
  		 */
  		qsort (sorted_active, num_active, sizeof (group_p), cmp_group_p);
- 		qsort (sorted_old_active, num, sizeof (notify_p), cmp_notify_p);
- 
- 		/* 
- 		 * Now do the pass through both arrays at once 
- 		 */
- 		for (i = 0, j = 0 ; i < num_active ; i++) {
- 			group_not_found = TRUE;
- 			compared = 1;
- 			while ((j < num) && 
- 				(0 > (compared = strcmp (
- 				sorted_old_active[j]->name, 
- 				sorted_active[i]->name)))) {
- 				j++;
- 			}
- 			if (compared == 0) {
- 				/* 
- 				 * found it so read in next group 
- 				 */
- 				group_not_found = FALSE;
- 				sorted_old_active[j]->visited = TRUE;
- 				j++;
- 			}
- 
- 			if (group_not_found == FALSE) {
- 				continue;
- 			}
- 
- 			update_old_active = TRUE;
- 
- 			/* 
- 			 * auto-subscribe/unsubscribe logic 
- 			 */
- 			if (! prompt_subscribe_group (sorted_active[i]->name,
- 				autosubscribe, autounsubscribe)) {
- 				if (cmd_line) {
- 					printf ("\r\n");
- 					fflush (stdout);
- 				}
- 				goto notify_groups_done;
- 			}
- 		}
  	}
  
! 	if (cmd_line) {
! 		fputc ('\r', stdout);
! 		fflush (stdout);
! 	}
! 	CleartoEOLN();
  
! 	if (! read_news_via_nntp) {
  		/*
  		 * Look for bogus groups 
  		 */
  		ch_default = 'y';
- 		for (j = 0 ; j < num ; j++)  {
- 			if (old_active[j].visited) {
- 				continue;
- 			}
  			do {	
  				update_old_active= 1;
  				fputc ('\r', stdout);
  				CleartoEOLN ();
! 				printf (txt_delete_bogus_group, old_active[j].name, ch_default);
  				fflush (stdout);
  				ch = ReadCh ();
  				if (ch == CR) {
--- 522,601 ----
  			error_message (txt_out_of_memory, progname);
  			goto notify_groups_done;
  		}
  		/* 
  		 * Fill arrays to be sorted 
  		 */
  		for (i = 0 ; i < num_active ; i++) {
  			sorted_active[i] = &active[i];
+ 			active[i].visit=0;	/*reset bitfield*/
  		}
  
  		/* 
! 		 * Sort the array
  		 */
  		qsort (sorted_active, num_active, sizeof (group_p), cmp_group_p);
  	}
+ /* SPA: my code only creates one sorted array (sorted_active), it then
+  * uses bsearch (binary search) for each line read from the 'old'
+  * active file, if it is found then the newsgroup is in both the old
+  * and new active files. Mark it as visited in the active list. If it is
+  * not found then it is a bogus group. After all 'old' groups have
+  * been read we loop round the active lst to see is there are any that
+  * have not been 'visited', these are the new groups.
+  *
+  * The code used to work by creating two sorted lists of the 'old' and
+  * 'new' active. It then stepped through the 'old' list to match with
+  * the new. The theory behind this operation is that the number of
+  * comparisons is proportional to the number of groups. The
+  * disadvantage with my approach is that the number of comparisons is
+  * larger, a binary search does on average log2(n) searches (e.g. 8
+  * comparisons for 4000 entries. However my approach also does one
+  * less qsort, which is a big win. (not sure how many comparisons qsort does)
+  */
+ 	while (fgets (buf, sizeof (buf), fp) != NULL) {
+ 		    /*NNTP terminates with '.' on a line by itself*/
+ 		if (buf[0] == '.' && buf[1] < 'A') {
+ 			break;
+ 		}
+ 		ptr = buf;
+ 		while (*ptr && *ptr != ' ' && *ptr != '\n') {
+ 			ptr++;
+ 		}
+ 		*ptr = '\0';
+ 		num++;
  
! 		if (read_news_via_nntp) {
! 		    if (find_group_index (buf) == -1) {
! 			    continue;
! 		    }
! 		    if (! prompt_subscribe_group (buf,
! 			    autosubscribe, autounsubscribe)) {
! 			    if (cmd_line) {
! 				    printf ("\r\n");
! 				    fflush (stdout);
! 			    }
! 			    goto notify_groups_done;
! 		    }
! 		} else {	/*Ok so we all have bsearch don't we?*/
  
! 		    tgroup.name=buf;
! 		    found = (group_p*)bsearch((char*)&p_group,
! 			(char*)sorted_active,num_active,sizeof(group_p),
! 			cmp_group_p);
! 
! 		    if (found) {
! 			(*found)->visit=1;
! 			continue;
! 		    }
  		/*
  		 * Look for bogus groups 
  		 */
  		ch_default = 'y';
  			do {	
  				update_old_active= 1;
  				fputc ('\r', stdout);
  				CleartoEOLN ();
! 				printf (txt_delete_bogus_group, buf, ch_default);
  				fflush (stdout);
  				ch = ReadCh ();
  				if (ch == CR) {
***************
*** 685,691 ****
  	
  			switch (ch) {
  				case 'y':
! 					delete_group (old_active[j].name);
  					break;	   
  				case 'q':
  				case ESC:
--- 605,611 ----
  	
  			switch (ch) {
  				case 'y':
! 					delete_group (buf);
  					break;	   
  				case 'q':
  				case ESC:
***************
*** 698,703 ****
--- 618,650 ----
  			fflush (stdout);
  		}
  	}
+ 
+ 	if(!read_news_via_nntp) {
+ 	    for (i = 0 ; i < num_active ; i++) {
+ 		if(sorted_active[i]->visit == 1) continue;
+ 			update_old_active = TRUE;
+ 
+ 			/* 
+ 			 * auto-subscribe/unsubscribe logic 
+ 			 */
+ 			if (! prompt_subscribe_group (sorted_active[i]->name,
+ 				autosubscribe, autounsubscribe)) {
+ 				if (cmd_line) {
+ 					printf ("\r\n");
+ 					fflush (stdout);
+ 				}
+ 				goto notify_groups_done;
+ 			}
+ 
+ 	    }
+ 	}
+ 
+ 	if (cmd_line) {
+ 		fputc ('\r', stdout);
+ 		fflush (stdout);
+ 	}
+ 	CleartoEOLN();
+ 
  	
  	/*
  	 *  write active[] to ~/.tin/active (local spooldir)
***************
*** 730,744 ****
  	/*
  	 * Free malloc'd arrays
  	 */
- 	if (old_active != (notify_p) 0) {
- 		free ((char *) old_active);
- 		old_active = (notify_p) 0;
- 	}
- 
- 	if (sorted_old_active != (notify_p *) 0) {
- 		free ((char *) sorted_old_active);
- 		sorted_old_active = (notify_p *) 0;
- 	}
  
  	if (sorted_active != (group_p *) 0) {
  		free ((char *) sorted_active);
--- 677,682 ----



-- 
Steve Alvey 				Email: spa@galadriel.bt.co.uk	
BT Labs 				Phone: +44 473 643871
Technical Details
Message-ID: <CJxBzz.Dxy@galadriel.bt.co.uk>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!pipex!bnr.co.uk!zaphod.axion.bt.co.uk!galadriel!steve
References: <2hic56$2n7@Mercury.mcs.com> <1994Jan19.072707.540@pony.ingres.com> <2hlc6j$dbk@Mercury.mcs.com>

Show Full Headers
Complete Article Headers
Message-ID: <CJxBzz.Dxy@galadriel.bt.co.uk>
Subject: Re: TIN is a huge pig -- still -- IS THERE A FIX FOR THIS COMING?
Date: Thu, 20 Jan 1994 10:33:35 GMT
From: steve@galadriel.bt.co.uk (Steve Alvey)
References: <2hic56$2n7@Mercury.mcs.com> <1994Jan19.072707.540@pony.ingres.com> <2hlc6j$dbk@Mercury.mcs.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!archive!mbox2nntp-news.software.readers.mbox.zip!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!pipex!bnr.co.uk!zaphod.axion.bt.co.uk!galadriel!steve
Newsgroups: news.software.readers
Sender: news@galadriel.bt.co.uk (USENET News System)
Nntp-Posting-Host: edoras
Organization: BTLabs, Martlesham, Ipswich, UK
X-Newsreader: TIN [version 1.2 PL2 (SPA)]
Lines: 494