stas        2003/02/07 20:17:11

  Modified:    lib/ModPerl Code.pm
  Log:
  change the autogenerated flags lookup functions to use a switch, to speed
  up the lookup, since we now want to use these functions during request
  time and not only on startup.
  
  Revision  Changes    Path
  1.92      +23 -6     modperl-2.0/lib/ModPerl/Code.pm
  
  Index: Code.pm
  ===================================================================
  RCS file: /home/cvs/modperl-2.0/lib/ModPerl/Code.pm,v
  retrieving revision 1.91
  retrieving revision 1.92
  diff -u -r1.91 -r1.92
  --- Code.pm   3 Feb 2003 06:39:30 -0000       1.91
  +++ Code.pm   8 Feb 2003 04:17:11 -0000       1.92
  @@ -355,6 +355,7 @@
       while (my($class, $opts) = each %{ $self->{flags} }) {
           my $i = 0;
           my @lookup = ();
  +        my %lookup = ();
           my $lookup_proto = "";
           my @dumper;
           if ($flags_options{$class}) {
  @@ -372,16 +373,14 @@
           print $h_fh "\n#define ${class}Type $n\n";
           $n++;
   
  +        my $max_len = 0;
           for my $f (@$opts) {
               my $x = sprintf "0x%08x", $i;
               my $flag = "${class}_f_$f";
               my $cmd  = $class . $f;
               my $name = canon_name($f);
  -
  -            if (@lookup) {
  -                push @lookup, qq(   if (strEQ(str, "$name")) return $flag;);
  -            }
  -
  +            $lookup{$name} = $flag;
  +            $max_len = length $name if $max_len < length $name;
               print $h_fh <<EOF;
   
   /* $f */
  @@ -398,7 +397,25 @@
               $i += $i || 1;
           }
           if (@lookup) {
  -            print $c_fh join "\n", @lookup, "   return 0;\n}\n";
  +            my $indent1 = " " x 4;
  +            my $indent2 = " " x 8;
  +            my %switch = ();
  +            for (keys %lookup) {
  +                if (/^(\w)/) {
  +                    my $gap = " " x ($max_len - length $_);
  +                    push @{ $switch{$1} }, 
  +                        qq{if (strEQ(str, "$_"))$gap return $lookup{$_};};
  +                }
  +            }
  +
  +            push @lookup, '', $indent1 . "switch (*str) {";
  +            for (keys %switch) {
  +                push @lookup, $indent1 . "  case '$_':";
  +                push @lookup, map { $indent2 . $_ } @{ $switch{$_} };
  +            }
  +            push @lookup, map { $indent1 . $_ } ("}\n", "return 0;\n}\n\n");
  +
  +            print $c_fh join "\n", @lookup;
               print $h_fh "$lookup_proto;\n";
           }
   
  
  
  


Reply via email to