From: Tony Cook Date: Mon, 6 Sep 2010 08:42:01 +0000 (+0000) Subject: fix X name lookup caching X-Git-Tag: Imager-0.79~66 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/173c91ba72e597a62db524820e1e31993c8f3d52 fix X name lookup caching --- diff --git a/Changes b/Changes index f7264398..e1e12665 100644 --- a/Changes +++ b/Changes @@ -12,6 +12,8 @@ Imager 0.78 - unreleased preparation for separate distribution. https://rt.cpan.org/Ticket/Display.html?id=49616 (partial) + - optimize filled box drawing (color, not fill) + Bug fixes: - Imager::Probe was calling ExtUtils::Liblist to initialize @@ -23,6 +25,11 @@ Bug fixes: then only when it would use its search mechanisms. https://rt.cpan.org/Ticket/Display.html?id=60509 + - fix the cache check for the X rgb.txt loader. This is typically + used for color translation on Unix-like systems, and improves + performance of supplying colors by name by about 80 times. Test + code that managed 3400 10x10 pixel boxes/second sped up to 25700 + boxes/second. Imager 0.77 - 11 Aug 2010 =========== diff --git a/lib/Imager/Color.pm b/lib/Imager/Color.pm index cfebf73e..9bad143e 100644 --- a/lib/Imager/Color.pm +++ b/lib/Imager/Color.pm @@ -63,6 +63,8 @@ my @gimp_search = '/usr/share/gimp/palettes/Named_Colors', ); +my $default_gimp_palette; + sub _load_gimp_palette { my ($filename) = @_; @@ -105,6 +107,17 @@ sub _get_gimp_color { if ($args{palette}) { $filename = $args{palette}; } + elsif (defined $default_gimp_palette) { + # don't search again and again and again ... + if (!length $default_gimp_palette + || !-f $default_gimp_palette) { + $Imager::ERRSTR = "No GIMP palette found"; + $default_gimp_palette = ""; + return; + } + + $filename = $default_gimp_palette; + } else { # try to make one up - this is intended to die if tainting is # enabled and $ENV{HOME} is tainted. To avoid that untaint $ENV{HOME} @@ -121,8 +134,11 @@ sub _get_gimp_color { } if (!$filename) { $Imager::ERRSTR = "No GIMP palette found"; + $default_gimp_palette = ""; return (); } + + $default_gimp_palette = $filename; } if ((!$gimp_cache{$filename} @@ -149,6 +165,8 @@ my @x_search = '/usr/openwin/lib/X11/rgb.txt', ); +my $default_x_rgb; + # called by the test code to check if we can test this stuff sub _test_x_palettes { @x_search; @@ -194,6 +212,13 @@ sub _get_x_color { if ($args{palette}) { $filename = $args{palette}; } + elsif (defined $default_x_rgb) { + unless (length $default_x_rgb) { + $Imager::ERRSTR = "No X rgb.txt palette found"; + return (); + } + $filename = $default_x_rgb; + } else { for my $attempt (@x_search) { if (-e $attempt) { @@ -203,16 +228,19 @@ sub _get_x_color { } if (!$filename) { $Imager::ERRSTR = "No X rgb.txt palette found"; + $default_x_rgb = ""; return (); } } if ((!$x_cache{$filename} - || (stat $filename)[9] != $x_cache{$filename}) + || (stat $filename)[9] != $x_cache{$filename}{mod_time}) && !_load_x_rgb($filename)) { return (); } + $default_x_rgb = $filename; + if (!$x_cache{$filename}{colors}{lc $args{name}}) { $Imager::ERRSTR = "Color '$args{name}' isn't in $filename"; return ();