From nobody@FreeBSD.org  Thu Dec 27 21:28:31 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 508B416A421
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 27 Dec 2007 21:28:31 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 4976613C465
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 27 Dec 2007 21:28:31 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id lBRLRrp3021129
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 27 Dec 2007 21:27:53 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id lBRLRr8h021128;
	Thu, 27 Dec 2007 21:27:53 GMT
	(envelope-from nobody)
Message-Id: <200712272127.lBRLRr8h021128@www.freebsd.org>
Date: Thu, 27 Dec 2007 21:27:53 GMT
From: Michal Botka <michal.botka@seznam.cz>
To: freebsd-gnats-submit@FreeBSD.org
Subject: sysinstall - reading packages from index is extremely slow 
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         119077
>Category:       bin
>Synopsis:       [patch] sysinstall(8) - reading packages from index is extremely slow
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-sysinstall
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Dec 27 21:30:00 UTC 2007
>Closed-Date:    
>Last-Modified:  Tue Jul 13 15:00:14 UTC 2010
>Originator:     Michal Botka
>Release:        6.2
>Organization:
>Environment:
>Description:
I installed FreeBSD from bootonly cd and then I tried to install some
packages by sysinstall from FTP mirror. It took about 6 minutes to read
index on my computer (Pentium II 350Mhz, 128MB). There is about 15.000
packages and used sorting algorithm - bubble sort - is ineffective. See
function index_sort in file usr.sbin/sysinstall/index.c.
>How-To-Repeat:

>Fix:
We should use some better sorting algorithm. My implementation of quick
sort is attached to this problem report.


Patch attached with submission follows:

418c418,456
< /* Use a disgustingly simplistic bubble sort to put our lists in order */
---
> /* Quick sort algorithm */
> void
> index_list_qsort(PkgNodePtr first, PkgNodePtr stop)
> {
>     /* empty or one-item list is already sorted */
>     if (first == stop || first->next == stop)
>         return;
> 	
>     /* quick sort */
>     PkgNodePtr i, p;
>     int size1, size2;
>     char* pivotName;
>  
>     size1 = 0;
>     size2 = 0;
>     pivotName = first->name;
>     for (p = first, i = p->next; i!=stop; i = i->next) {
>         if (strcmp(i->name, pivotName)<0) {
>             p = p->next;
>             swap_nodes(p, i);
>             size1++;
>         } else {
>             size2++;
>         }
>     }
>     swap_nodes(first, p);
>     
>     /* we handle the shorter part before the longer
>        recursion depth never exceeds logarithm of the total list length */
>     if (size1 <= size2) { 
>         index_list_qsort(first, p);
>         index_list_qsort(p->next, stop);
>     } else {
>         index_list_qsort(p->next, stop);
>         index_list_qsort(first, p);
>     }
> }
> 
> /* Use a quick sort to put our lists in order */
425,430c463
<     for (p = top->kids; p; p = p->next) {
< 	for (q = top->kids; q; q = q->next) {
< 	    if (q->next && strcmp(q->name, q->next->name) > 0)
< 		swap_nodes(q, q->next);
< 	}
<     }
---
>     index_list_qsort(top->kids, NULL);
434,435c467,469
< 	if (p->kids)
< 	    index_sort(p);
---
>         if (p->kids) {
>             index_sort(p);
>         }


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->re 
Responsible-Changed-By: remko 
Responsible-Changed-When: Fri Dec 28 08:10:16 UTC 2007 
Responsible-Changed-Why:  
Dear Release engineers, I think this is something for you to 
have a look (or someone who dares touching sysinstall ;-)) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=119077 
Responsible-Changed-From-To: re->freebsd-bugs 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Tue Mar 10 03:41:31 UTC 2009 
Responsible-Changed-Why:  
These days, we don't assign sysinstall PRs to re@. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=119077 
Responsible-Changed-From-To: freebsd-bugs->freebsd-sysinstall 
Responsible-Changed-By: gavin 
Responsible-Changed-When: Tue Jul 13 13:36:54 UTC 2010 
Responsible-Changed-Why:  
Over to maintainer(s) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=119077 

From: Garrett Cooper <yanegomi@gmail.com>
To: bug-followup@FreeBSD.org, michal.botka@seznam.cz
Cc:  
Subject: Re: bin/119077: [patch] sysinstall(8) - reading packages from index 
	is extremely slow
Date: Tue, 13 Jul 2010 07:52:47 -0700

 I don't think that this algorithm is correct, because instead of
 bisecting the set (like quicksort typically does), it's taking the
 first element, and then doing a bubble-like sort.
 
 Regardless of the correctness of the algorithm, if one is concerned
 about using quicksort, the replacement algorithm should be using qsort
 in libc (because it's more tested and most likely more correct).
 
 Thanks,
 -Garrett
>Unformatted:
