diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2010-01-19 14:58:54 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2010-01-19 14:58:54 +0000 |
commit | 48b9616b47d311bf2d336e22205e8555fa12e454 (patch) | |
tree | 18c08ad252f0901edf4b84a6ef9996067a9e9ba8 /usr.sbin/pkg_add | |
parent | ae1970a0cd0a3aba7c728923280d4f72c5a6e3e1 (diff) |
simplify caching: return $cached->{$_} //= new_value;
remove stats
cache LibSpec as well (1000 c.53.0 !)
Diffstat (limited to 'usr.sbin/pkg_add')
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Error.pm | 7 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/LibSpec.pm | 10 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PackageName.pm | 40 |
3 files changed, 14 insertions, 43 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/Error.pm b/usr.sbin/pkg_add/OpenBSD/Error.pm index 14db4a55164..0816d4e0374 100644 --- a/usr.sbin/pkg_add/OpenBSD/Error.pm +++ b/usr.sbin/pkg_add/OpenBSD/Error.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Error.pm,v 1.22 2010/01/17 11:56:46 espie Exp $ +# $OpenBSD: Error.pm,v 1.23 2010/01/19 14:58:53 espie Exp $ # # Copyright (c) 2004-2010 Marc Espie <espie@openbsd.org> # @@ -24,10 +24,7 @@ sub cache(*&) my $callpkg = caller; my $actual = sub { my $self = shift; - if (!defined $self->{$sym}) { - $self->{$sym} = &$code($self); - } - return $self->{$sym}; + return $self->{$sym} //= &$code($self); }; no strict 'refs'; *{$callpkg."::$sym"} = $actual; diff --git a/usr.sbin/pkg_add/OpenBSD/LibSpec.pm b/usr.sbin/pkg_add/OpenBSD/LibSpec.pm index d586c24ab3a..fcd248dc1ec 100644 --- a/usr.sbin/pkg_add/OpenBSD/LibSpec.pm +++ b/usr.sbin/pkg_add/OpenBSD/LibSpec.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: LibSpec.pm,v 1.1 2010/01/19 14:26:24 espie Exp $ +# $OpenBSD: LibSpec.pm,v 1.2 2010/01/19 14:58:53 espie Exp $ # # Copyright (c) 2010 Marc Espie <espie@openbsd.org> # @@ -55,8 +55,16 @@ sub badspec "OpenBSD::LibSpec::BadSpec"; } +my $cached = {}; + sub from_string { + my ($class, $_) = @_; + return $cached->{$_} //= $class->new_from_string($_); +} + +sub new_from_string +{ my ($class, $string) = @_; if (my ($stem, $major, $minor) = $string =~ m/^(.*)\.(\d+)\.(\d+)$/o) { if ($stem =~ m/^(.*)\/([^\/]+)$/o) { diff --git a/usr.sbin/pkg_add/OpenBSD/PackageName.pm b/usr.sbin/pkg_add/OpenBSD/PackageName.pm index 8a100d91076..60c592503fa 100644 --- a/usr.sbin/pkg_add/OpenBSD/PackageName.pm +++ b/usr.sbin/pkg_add/OpenBSD/PackageName.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: PackageName.pm,v 1.40 2010/01/10 11:31:08 espie Exp $ +# $OpenBSD: PackageName.pm,v 1.41 2010/01/19 14:58:53 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -44,37 +44,11 @@ sub splitname } my $cached = {}; -my $calls = 0; -my $nohits = 0; -my $dewey_calls = 0; -my $dewey_hits = 0; -my $tocalls = 0; -my $strings = 0; - -sub stats -{ - require Devel::Size; - - print STDERR "Size=", Devel::Size::total_size($cached), "\n"; - print STDERR "Total calls: ", $calls, "\n"; - print STDERR "Cache hits: ", $calls - $nohits, "\n"; - print STDERR "Dewey calls: ", $dewey_calls, "\n"; - print STDERR "Dewey hits: ", $dewey_hits, "\n"; - print STDERR "to_string calls: ", $tocalls, "\n"; - print STDERR "string compares: ", $strings, "\n"; -# print STDERR join(',', sort keys %$cached), "\n"; -} sub from_string { my ($class, $_) = @_; - $calls++; - if (!defined $cached->{$_}) { - $nohits++; - return $cached->{$_} = $class->new_from_string($_); - } else { - return $cached->{$_}; - } + return $cached->{$_} //= $class->new_from_string($_); } sub new_from_string @@ -195,13 +169,7 @@ sub from_string sub make { my ($class, $string) = @_; - $dewey_calls++; - if (!defined $cache->{$string}) { - return $cache->{$string} = $class->from_string($string); - } else { - $dewey_hits++; - return $cache->{$string}; - } + return $cache->{$string} //= $class->from_string($string); } sub to_string @@ -308,7 +276,6 @@ sub from_string sub to_string { my $o = shift; - $tocalls++; my $string = $o->{dewey}->to_string; if (defined $o->{p}) { $string .= 'p'.$o->{p}; @@ -334,7 +301,6 @@ sub compare } # Simple case: only p number differs if ($a->{dewey} eq $b->{dewey}) { - $strings++; return $a->pnum_compare($b); } |