[pacman-dev] [PATCH v2 3/6] pacsearch: removed redundant sorting

Andrew Gregory andrew.gregory.8 at gmail.com
Wed Feb 5 09:18:48 EST 2014


On 02/04/14 at 12:16am, Pierre Neidhardt wrote:
> Package are processed in the same order as pacman output, so there is no real
> need to sort, we can print directly. This makes the code simpler and faster.
> 
> The only difference is that local packages will always be printed at the
> end. Previously, they were printed before multilib for instance.
> 
> Signed-off-by: Pierre Neidhardt <ambrevar at gmail.com>
> ---
>  contrib/pacsearch.in | 46 ++++++++++++++++------------------------------
>  1 file changed, 16 insertions(+), 30 deletions(-)
 
This adds an 8 second delay between sync and local results for me on
a cold run.  It's not a deal-breaker for me, but I'd prefer to remove
the sort by just storing the results in a list.

> diff --git a/contrib/pacsearch.in b/contrib/pacsearch.in
> index d3d461f..66b1683 100644
> --- a/contrib/pacsearch.in
> +++ b/contrib/pacsearch.in
> @@ -81,21 +81,21 @@ if ($ARGV[0] eq "--nocolor" || $ARGV[0] eq "-n") {
>  # localization
>  my $LC_INSTALLED = `gettext pacman installed`;
>  
> -# Color a "repo/pkgname pkgver (groups) [installed]" line.
> -# We try to stick to pacman colors.
> -sub to_color {
> +# Print a "repo/pkgname pkgver (groups) [installed]" line.
> +# We stick to pacman colors.
> +sub print_pkg {
>  	my @v = @_;
> -	my $line = "$RESET$BOLD";
> +	print "$RESET$BOLD";
>  	if ( "$v[0]" eq "local" ) {
> -		$line .= "$RED";
> +		print "$RED";
>  	} else {
> -		$line .= "$MAGENTA";
> +		print "$MAGENTA";
>  	}
> -	$line .= "$v[0]/$RESET$BOLD$v[1] $GREEN$v[2]";
> -	$line .= " $BLUE$v[3]" if $v[3] ne "";
> -	$line .= " $CYAN$v[4]" if $v[4] ne "";
> -	$line .= " $RESET";
> -	return $line;
> +	print "$v[0]/$RESET$BOLD$v[1] $GREEN$v[2]";
> +	print " $BLUE$v[3]" if $v[3] ne "";
> +	print " $CYAN$v[4]" if $v[4] ne "";
> +	print " $RESET\n";
> +	print " $v[5]\n";
>  }
>  
>  my %allpkgs = ();
> @@ -108,8 +108,6 @@ if ($#syncpkgs >= 0) {
>  	chomp($syncpkgs[$#syncpkgs]);
>  }
>  
> -# counter var for packages, used here and in the query loop too
> -my $cnt = 0;
>  foreach $_ (@syncpkgs) {
>  	# we grab the following fields: repo, name, ver, group, installed, and desc
>  	my @pkgfields = /^(.*?)\/(.*?) (.*?) ?(\(.*?\))? ?(\[.*\])?\n(.*)$/s;
> @@ -121,10 +119,10 @@ foreach $_ (@syncpkgs) {
>  	# since 'group' and 'installed' are optional, we should fill it in if necessary
>  	$pkgfields[3] = "" if not defined $pkgfields[3];
>  	$pkgfields[4] = "" if not defined $pkgfields[4];
> -	# add a last field that indicates original order
> -	push (@pkgfields, $cnt++);
> -	# add each sync pkg by name/ver to a hash table for quick lookup
> -	$allpkgs{$pkgfields[1] . $pkgfields[2]} = [ @pkgfields ];
> +	# Add each sync pkg by name/ver to a hash table.
> +	# Any value is good since we only check for existence.
> +	$allpkgs{$pkgfields[1] . $pkgfields[2]} = 1;
> +	print_pkg(@pkgfields);
>  }
>  
>  my $queryout = `pacman -Qs '@ARGV'`;
> @@ -145,20 +143,8 @@ foreach $_ (@querypkgs) {
>  		# since 'group' is optional, we should fill it in if necessary
>  		$pkgfields[3] = "" if not defined $pkgfields[3];
>  		$pkgfields[4] = "[$LC_INSTALLED]";
> -		# add a last field that indicates original order (after sync)
> -		push (@pkgfields, $cnt++);
> -		# add our local-only package to the hash
> -		$allpkgs{$pkgfields[1] . $pkgfields[2]} = [ @pkgfields ];
> +		print_pkg(@pkgfields);
>  	}
>  }
>  
> -# sort by original order (the last field) and print
> -foreach $_ ( sort{ @{$allpkgs{$a}}[6] <=> @{$allpkgs{$b}}[6] } keys %allpkgs) {
> -	my @v = @{$allpkgs{$_}};
> -	my $line = to_color(@v);
> -	# print colorized "repo/pkgname pkgver ..." string with possible installed text
> -	print "$line\n";
> -	print "$v[5]\n";
> -}
> -
>  #vim: set noet:
> -- 
> 1.8.5.3


More information about the pacman-dev mailing list