automake-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

FYI: speed up DisjConditions::new


From: Alexandre Duret-Lutz
Subject: FYI: speed up DisjConditions::new
Date: Sun, 08 Aug 2004 22:14:13 +0200
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50 (gnu/linux)

I'm checking this in.

Coreutils: 5.92s -> 5.4s
%Time spent in Automake::DisjConditions::new: 12.1% -> 6.32%

2004-08-08  Alexandre Duret-Lutz  <address@hidden>

        * lib/Automake/DisjConditions.pm (new): Precompute 'string' and 'conds'
        in place instead of as a side-effect of calling ->string and ->conds.
        This saves method-lookup time, simplify ->string and ->conds, and
        allows to create the object only when necessary.
        (string, conds): Simplify, now that the result is precomputed.

Index: lib/Automake/DisjConditions.pm
===================================================================
RCS file: /cvs/automake/automake/lib/Automake/DisjConditions.pm,v
retrieving revision 1.11
diff -u -r1.11 DisjConditions.pm
--- lib/Automake/DisjConditions.pm      7 Mar 2004 12:36:54 -0000       1.11
+++ lib/Automake/DisjConditions.pm      8 Aug 2004 20:11:37 -0000
@@ -145,11 +145,7 @@
 sub new ($;@)
 {
   my ($class, @conds) = @_;
-  my $self = {
-    hash => {},
-  };
-  bless $self, $class;
-
+  my @filtered_conds = ();
   for my $cond (@conds)
     {
       confess "`$cond' isn't a reference" unless ref $cond;
@@ -161,19 +157,40 @@
       # DisjConditions as false for this reason.
       next if $cond->false;
 
-      # Store conditions as keys AND as values, because blessed
-      # objects are converted to string when used as keys (so
-      # at least we still have the value when we need to call
-      # a method).
-      $self->{'hash'}{$cond} = $cond;
+      push @filtered_conds, $cond;
     }
 
-  my $key = $self->string;
-  if (exists $_disjcondition_singletons{$key})
+  my $string;
+  if (@filtered_conds)
+    {
+      @filtered_conds = sort { $a->string cmp $b->string } @filtered_conds;
+      $string = join (' | ', map { $_->string } @filtered_conds);
+    }
+  else
     {
-      return $_disjcondition_singletons{$key};
+      $string = 'FALSE';
     }
-  $_disjcondition_singletons{$key} = $self;
+
+  # Return any existing identical DisjConditions.
+  my $me = $_disjcondition_singletons{$string};
+  return $me if $me;
+
+  # Else, create a new DisjConditions.
+
+  # Store conditions as keys AND as values, because blessed
+  # objects are converted to string when used as keys (so
+  # at least we still have the value when we need to call
+  # a method).
+  my %h = map {$_ => $_} @filtered_conds;
+
+  my $self = {
+    hash => \%h,
+    string => $string,
+    conds => address@hidden,
+  };
+  bless $self, $class;
+
+  $_disjcondition_singletons{$string} = $self;
   return $self;
 }
 
@@ -186,11 +203,7 @@
 sub conds ($ )
 {
   my ($self) = @_;
-  return @{$self->{'conds'}} if exists $self->{'conds'};
-  my @conds = values %{$self->{'hash'}};
-  @conds = sort { $a->string cmp $b->string } @conds;
-  $self->{'conds'} = address@hidden;
-  return @conds;
+  return @{$self->{'conds'}};
 }
 
 =item C<$cond = $set-E<gt>one_cond>
@@ -241,21 +254,7 @@
 sub string ($ )
 {
   my ($self) = @_;
-
-  return $self->{'string'} if defined $self->{'string'};
-
-  my $res = '';
-  if ($self->false)
-    {
-      $res = 'FALSE';
-    }
-  else
-    {
-      $res = join (' | ', map { $_->string } $self->conds);
-    }
-
-  $self->{'string'} = $res;
-  return $res;
+  return $self->{'string'};
 }
 
 =item C<$cond-E<gt>human>

-- 
Alexandre Duret-Lutz





reply via email to

[Prev in Thread] Current Thread [Next in Thread]