Commit d386bd79 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

IRIS: Decrease execution time at the cost of slightly higher memory use

The first { $_->raw_id eq $id } @{ $self->{results} } calls effectively
handle the results as a linked list, while in both cases only a single
element (with a unique ID, too) is looked up. A hashmap makes this about
30% faster (580ms total -> 490ms total)
parent ea1386e9
Loading
Loading
Loading
Loading
+9 −7
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ sub new {
		serializable    => $opt{serializable},
		user_agent      => $ua,
		with_related    => $opt{with_related},
		departure_by_id => {},
	};

	bless( $self, $class );
@@ -292,8 +293,9 @@ sub add_result {
	# if scheduled departure and current departure are not within the
	# same hour, trains are reported twice. Don't add duplicates in
	# that case.
	if ( not first { $_->raw_id eq $id } @{ $self->{results} } ) {
	if ( not $self->{departure_by_id}{$id} ) {
		push( @{ $self->{results} }, $result, );
		$self->{departure_by_id}{$id} = $result;
	}

	return $result;
@@ -350,7 +352,7 @@ sub get_realtime {

		my %messages;

		my $result = first { $_->raw_id eq $id } $self->results;
		my $result = $self->{departure_by_id}{$id};

		if ( not $result ) {
			$result = $self->add_result( $station, $s );