]> git.imager.perl.org - imager.git/commitdiff
fix X name lookup caching
authorTony Cook <tony@develop=help.com>
Mon, 6 Sep 2010 08:42:01 +0000 (08:42 +0000)
committerTony Cook <tony@develop=help.com>
Mon, 6 Sep 2010 08:42:01 +0000 (08:42 +0000)
Changes
lib/Imager/Color.pm

diff --git a/Changes b/Changes
index f72643988dc2fdfb19ed4459a571efbd703bb860..e1e12665b328f1ca6ad2c22b80cd7d3e8e93ad3a 100644 (file)
--- 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
 ===========
index cfebf73ec78cf6261d9156a223a3a4416c7ca929..9bad143e4cc0579ef9d53f577d71a72e19f12d1c 100644 (file)
@@ -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 ();