summaryrefslogtreecommitdiff
path: root/lib/Travel/Status/DE
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2016-10-05 21:04:28 +0200
committerDaniel Friesel <derf@finalrewind.org>2016-10-05 21:09:51 +0200
commitd386bd79dd918d72aa89eb8a077714e5a3d0242a (patch)
tree3dff1ff1db6fa839673eb0367010308cb5942cf6 /lib/Travel/Status/DE
parentea1386e96db3e4061b3b1b054e52d9683d741569 (diff)
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)
Diffstat (limited to 'lib/Travel/Status/DE')
-rw-r--r--lib/Travel/Status/DE/IRIS.pm16
1 files changed, 9 insertions, 7 deletions
diff --git a/lib/Travel/Status/DE/IRIS.pm b/lib/Travel/Status/DE/IRIS.pm
index ac7a076..056fef4 100644
--- a/lib/Travel/Status/DE/IRIS.pm
+++ b/lib/Travel/Status/DE/IRIS.pm
@@ -36,11 +36,12 @@ sub new {
iris_base => $opt{iris_base}
// 'http://iris.noncd.db.de/iris-tts/timetable',
lookahead => $opt{lookahead} // ( 4 * 60 ),
- main_cache => $opt{main_cache},
- rt_cache => $opt{realtime_cache},
- serializable => $opt{serializable},
- user_agent => $ua,
- with_related => $opt{with_related},
+ main_cache => $opt{main_cache},
+ rt_cache => $opt{realtime_cache},
+ 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 );