Re: diff to bsearch the autoconf vendor/product match tables

2010-12-16 Thread Mark Kettenis
 Date: Thu, 16 Dec 2010 02:39:47 +0200
 From: Vladimir Kirillov pro...@uaoug.org.ua
 
 Hello, t...@!
 
 The pci/usb (and others) known_{products,vendors} generated from
 {pci,usb}devs are really big and scanning them is not so efficient.
 
 I took the reyk@'s bsearch() implementation found in ieee80211 code
 and moved it to libkern.
 
 To sort tables I've rewritten the devlist2h from awk to perl, since
 it can sort :).  Imho, this implementation is a bit cleaner and
 does not have that big license header.
 
 Since usb/pci is only supported so far, I hope if you like it I can
 continue replacing the rest of autoconf matching goo to bsearch
 
 What do you think?

My initial reaction is that you're trying to optimize something that
isn't worth optimizing.



diff to bsearch the autoconf vendor/product match tables

2010-12-15 Thread Vladimir Kirillov
Hello, t...@!

The pci/usb (and others) known_{products,vendors} generated from
{pci,usb}devs are really big and scanning them is not so efficient.

I took the reyk@'s bsearch() implementation found in ieee80211 code
and moved it to libkern.

To sort tables I've rewritten the devlist2h from awk to perl, since
it can sort :).  Imho, this implementation is a bit cleaner and
does not have that big license header.

Since usb/pci is only supported so far, I hope if you like it I can
continue replacing the rest of autoconf matching goo to bsearch

What do you think?

Index: conf/files
===
RCS file: /cvs/src/sys/conf/files,v
retrieving revision 1.504
diff -u -p -r1.504 files
--- conf/files  6 Oct 2010 22:19:20 -   1.504
+++ conf/files  16 Dec 2010 00:12:53 -
@@ -1085,6 +1085,7 @@ file lib/libkern/mcount.c gprof
 file lib/libkern/getsn.c
 file lib/libkern/random.c
 file lib/libkern/timingsafe_bcmp.c
+file lib/libkern/bsearch.c
 file lib/libkern/arch/${MACHINE_ARCH}/strchr.S | lib/libkern/strchr.c
 file lib/libkern/arch/${MACHINE_ARCH}/strrchr.S | lib/libkern/strrchr.c
 file lib/libkern/arch/${MACHINE_ARCH}/__main.S | lib/libkern/__main.c
Index: dev/pci/Makefile
===
RCS file: /cvs/src/sys/dev/pci/Makefile,v
retrieving revision 1.4
diff -u -p -r1.4 Makefile
--- dev/pci/Makefile14 Oct 1996 09:01:34 -  1.4
+++ dev/pci/Makefile16 Dec 2010 00:12:53 -
@@ -1,8 +1,6 @@
 #  $OpenBSD: Makefile,v 1.4 1996/10/14 09:01:34 deraadt Exp $
-#  $NetBSD: Makefile,v 1.1 1995/06/18 01:07:04 cgd Exp $
 
-AWK=   awk
+PERL?= perl
 
-pcidevs.h pcidevs_data.h: pcidevs devlist2h.awk
-   /bin/rm -f pcidevs.h pcidevs_data.h
-   ${AWK} -f devlist2h.awk pcidevs
+pcidevs.h pcidevs_data.h: pcidevs devlist2h.pl
+   ${PERL} devlist2h.pl  pcidevs
Index: dev/pci/devlist2h.pl
===
RCS file: dev/pci/devlist2h.pl
diff -N dev/pci/devlist2h.pl
--- /dev/null   1 Jan 1970 00:00:00 -
+++ dev/pci/devlist2h.pl16 Dec 2010 00:12:53 -
@@ -0,0 +1,167 @@
+#!/usr/bin/perl -w
+use strict;
+
+my $hfile = pcidevs.h;
+my $dfile = pcidevs_data.h;
+
+open HFILE, $hfile;
+open DFILE, $dfile;
+sub header {
+   my ($file, $version) = @_;
+   chomp $version;
+
+   print {$file} __END;
+/*
+ * THIS FILE AUTOMATICALLY GENERATED.  DO NOT EDIT.
+ *
+ * generated from:
+ * $version
+ */
+__END
+}
+
+my $version = readline;
+header \*HFILE, $version;
+header \*DFILE, $version;
+
+my %vendorhash;
+my @vendors;
+my @products;
+
+my $commentmode = 0;
+my @commentbuf;
+my $leadblank = 0;
+
+while () {
+   chomp;
+   if ($commentmode) {
+   push @commentbuf, $_;
+   if (/\*\/\s*$/) {
+   $commentmode = 0;
+   }
+   next;
+   }
+
+   if (/^vendor/) {
+   my @data = split /[ \t]+/;
+   my ($k, $vendor, $id) = splice @data, 0, 3;
+   my $descr = join ' ', @data;
+
+   my @copy = @commentbuf;
+   my %new = (
+   vendor  = $vendor,
+   id  = $id,
+   descr   = $descr,
+
+   lblank  = $leadblank,
+   comment = \...@copy,
+   );
+   push @vendors, \%new;
+   $vendorhash{$vendor} = \%new;
+
+   $leadblank = 0;
+   @commentbuf = ();
+   } elsif (/^product/) {
+   my @data = split /[ \t]+/;
+   my ($k, $vendor, $product, $id) = splice @data, 0, 4;
+   my $descr = join ' ', @data;
+
+   my @copy = @commentbuf;
+   my %new = (
+   vendor  = $vendor,
+   product = $product,
+   id  = $id,
+   descr   = $descr,
+   
+   lblank  = $leadblank,
+   comment = \...@copy,
+   );
+   push @products, \%new;
+
+   $leadblank = 0;
+   @commentbuf = ();
+   } elsif (/^$/) {
+   $leadblank = 1;
+   } elsif (/^\s*\/\*.*\*\/$/) { # oneline comment
+   push @commentbuf, $_;
+   } elsif (/^\s*\/\*/) {
+   $commentmode = 1;
+   push @commentbuf, $_;
+   }
+}
+die invalid syntax\n if $commentmode;
+
+sub prologue {
+   my $h = shift;
+   print HFILE \n if $h-{lblank};
+   if (@{$h-{comment}}) {
+   print HFILE join(\n, @{$h-{comment}});
+   print HFILE \n;
+   }
+}
+
+foreach my $v (@vendors) {
+   prologue $v;
+   print HFILE __END;
+#define PCI_VENDOR_$v-{vendor}$v-{id} /* $v-{descr} */
+__END
+}
+
+foreach my $p (@products) {
+   prologue $p;
+   print HFILE 

Re: diff to bsearch the autoconf vendor/product match tables

2010-12-15 Thread Vladimir Kirillov
On 02:39 Thu 16 Dec, Vladimir Kirillov wrote:
 +foreach my $v (sort { hex($a-{id}) = hex($b-{id}) } @vendors) {

Please also note that this won't fail on usbdevs if you replace 0X
hex prefixes with 0x.