There is a better (and faster) way to see if all the members of one list are
authorJeff Morriss <jeff.morriss@ulticom.com>
Sat, 12 Feb 2011 03:53:29 +0000 (03:53 -0000)
committerJeff Morriss <jeff.morriss@ulticom.com>
Sat, 12 Feb 2011 03:53:29 +0000 (03:53 -0000)
in another list: convert the 2nd list to a hash.  This speeds checking for ett_
variables up considerably.

Store the pattern to match ett_ variable names in a variable (since it's used 3
times).

Only match ett_ variable declarations that start on their own line (hopefully to
speed things up a bit).

svn path=/trunk/; revision=35929

tools/checkAPIs.pl

index 31cfc59db6a9d47018145643ab300a4bdc63bdf4..02aa4d13a5597be45d4dd9b71a098ac6052b1768 100755 (executable)
@@ -1022,23 +1022,27 @@ sub check_ett_registration($$)
 {
        my ($fileContentsRef, $filename) = @_;
        my @ett_declarations;
-       my @ett_registrations;
+       my %ett_registrations;
        my @unRegisteredEtts;
 
+       # A pattern to match ett variable names.  Obviously this assumes that
+       # they start with ett_
+       my $EttVarName = qr{ (?: ett_[a-z0-9_]+ (?:\[[0-9]+\])? ) }xi;
+
        # Remove macro lines
        my $fileContents = ${$fileContentsRef};
        $fileContents =~ s { ^\s*\#.*$} []xogm;
 
        # Find all the ett_ variables declared in the file
        @ett_declarations = ($fileContents =~ m{
-               static
+               ^\s*static              # assume declarations are on their own line
                \s+
                g?int                   # could be int or gint
                \s+
-               (ett_[a-z0-9_]+)        # variable name
+               ($EttVarName)           # variable name
                \s*=\s*
                -1\s*;
-       }xgios);
+       }xgiom);
 
        if (!@ett_declarations) {
                print "Found no etts in ".$filename."\n";
@@ -1060,7 +1064,7 @@ sub check_ett_registration($$)
                =
                \s*\{
                ((?:\s*&\s*             # address of the following variable
-               ett_[a-z0-9_]+          # variable name
+               $EttVarName             # variable name
                \s*,?                   # the comma is optional (for the last entry)
                \s*)+)                  # match one or more variable names
                \}
@@ -1075,33 +1079,33 @@ sub check_ett_registration($$)
                return;
        }
 
-       while (<@reg_blocks>) {
-               push @ett_registrations, ($_ =~ m{
+       while (@reg_blocks) {
+               my ($block) = @reg_blocks;
+               shift @reg_blocks;
+
+               # Convert the list returned by the match into a hash of the
+               # form ett_variable_name -> 1.  Then combine this new hash with
+               # the hash from the last registration block.
+               # (Of course) using hashes makes the lookups much faster.
+               %ett_registrations = map { $_ => 1 } ($block =~ m{
                        \s*&\s*                 # address of the following variable
-                       (ett_[a-z0-9_]+)        # variable name
+                       ($EttVarName)           # variable name
                        \s*,?                   # the comma is optional (for the last entry)
-               }xgios);
+               }xgios, %ett_registrations);
        }
-       #print "Found these ett registrations in ".$filename.": ".join(',', @ett_registrations)."\n";
+       #print "Found these ett registrations in ".$filename.": ";
+       #while( my ($k, $v) = each %ett_registrations ) {
+       #          print "$k\n";
+       #}
 
-       # Find which declared etts are not registered
-       # There has to be an easier way to do this...
-       my $foundit = 0;
+       # Find which declared etts are not registered.
+       # XXX - using <@ett_declarations> and $_ instead of $ett_var makes this
+       # MUCH slower...  Why?
        while (@ett_declarations) {
                my ($ett_var) = @ett_declarations;
                shift @ett_declarations;
 
-               $foundit = 0;
-               foreach $_ (@ett_registrations) {
-                       if ($_ eq $ett_var) {
-                               $foundit = 1;
-                               last;
-                       }
-               }
-
-               if (!$foundit) {
-                       push @unRegisteredEtts, $ett_var;
-               }
+               push(@unRegisteredEtts, $ett_var) if (!$ett_registrations{$ett_var});
        }
 
        if (@unRegisteredEtts) {