2004-08-08 Alexandre Duret-Lutz <adl@gnu.org>
+ * 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.
+
* automake.in (%am_file_cache): New hash.
(make_paragraphs): Cache .am files with comments stripped to save
some useless input and substitutions.
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;
# 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 => \@filtered_conds,
+ };
+ bless $self, $class;
+
+ $_disjcondition_singletons{$string} = $self;
return $self;
}
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'} = [@conds];
- return @conds;
+ return @{$self->{'conds'}};
}
=item C<$cond = $set-E<gt>one_cond>
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>