dramatic speed improvements
authorgc <gc>
Wed, 26 Apr 2006 21:52:41 +0000 (21:52 +0000)
committergc <gc>
Wed, 26 Apr 2006 21:52:41 +0000 (21:52 +0000)
bin/booh-backend
lib/booh/booh-lib.rb

index e9fdf3f98802c1a8e3a7d9fca17e5a85bf5b9843..a524dcf3a3ca3794148f466ec93682d972c707cb 100755 (executable)
@@ -928,10 +928,15 @@ def walk_source_dir
                 counter = 0
                 pagecount = 0
                 reset_iterations(iterations)
+                #- preprocess the @filename->elem, rexml is very slow with that; we dramatically improve this part of the processing
+                optfilename = {}
+                xmldir.elements.each('image') { |elem|
+                    optfilename[elem.attributes['filename']] = elem
+                }
                 for file in entries
                     type = images.include?(file) ? 'image' : videos.include?(file) ? 'video' : nil
                     if type
-                        if type == 'image' && elem = xmldir.elements["image[@filename='#{utf8(file)}']"]
+                        if type == 'image' && elem = optfilename[utf8(file)]
                             if pano = pano_amount(elem)
                                 html_elem = run_iterations(iterations, pano)
                                 counter += count = pano.ceil
@@ -974,7 +979,7 @@ def walk_source_dir
                                                       img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
                             html_thumbnails_nojs.gsub!(/~~image_iteration~~/, 
                                                        '<a href="' + fullscreen_images[sizeobj['name']][index] + '" name="' + fullscreen_images[sizeobj['name']][index] + '">' +
-                                                           img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
+                                                       img_element("#{dest_dir}/#{thumbnail_images[sizeobj['name']][index]}") + '</a>')
                             #- remember in which thumbnails page is this element, for image->thumbnail link
                             if sizeobj == $images_size[0]
                                 image2thumbnailpage4js << pagecount
@@ -1228,16 +1233,19 @@ def walk_source_dir
         ios.close
 
         #- substitute multiple "return to albums", previous/next correctly
+        #- the following two statements are dramatical optimizations to executing for each substInFile callback
+        dirpresent = xmldir.elements['dir']
+        parentname = xmldir.parent.name
         if xmldir.child_byname_notattr('image', 'deleted') || xmldir.child_byname_notattr('video', 'deleted')
             for suffix in [ '', '-nojs' ]
                 for sizeobj in $images_size
                     Dir.glob("#{dest_dir}/thumbnails-#{size2js(sizeobj['name'])}#{suffix}-*.html") do |file|
                         substInFile(file) { |line|
                             sub_previous_next_album(previous_album, next_album, line)
-                            if xmldir.elements['dir']
+                            if dirpresent
                                 line.sub!(/~~return_to_albums~~/, '<a href="index.html">' + utf8(_('return to albums')) + '</a>')
                             else
-                                if xmldir.parent.name == 'dir'
+                                if parentname == 'dir'
                                     line.sub!(/~~return_to_albums~~/, '<a href="../index.html">' + utf8(_('return to albums')) + '</a>')
                                 else
                                     line.sub!(/~~return_to_albums~~/, '')
index f0df7dedb746217d060a6f84f3f5b0f01847591c..731b9ed1cf593c544e5e94779bbaa0b5970d0459 100644 (file)
@@ -28,6 +28,11 @@ bindtextdomain("booh")
 
 require 'booh/config.rb'
 require 'booh/version.rb'
+begin
+    require 'gtk2'
+rescue LoadError
+    $no_gtk2 = true
+end
 
 module Booh
     $verbose_level = 2
@@ -212,8 +217,23 @@ module Booh
     end
 
     def get_image_size(fullpath)
-        if !$no_identify && `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) /
-            return { :x => $1.to_i, :y => $2.to_i }
+        if !$no_identify
+            if $sizes_cache.nil?
+                $sizes_cache = {}
+            end
+            if $sizes_cache[fullpath].nil?
+                #- identify is slow, try with gdk if available (4ms vs 35ms)
+                if $no_gtk2
+                    if `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) /
+                        $sizes_cache[fullpath] = { :x => $1.to_i, :y => $2.to_i }
+                    end
+                else
+                    if pxb = Gdk::Pixbuf.new(fullpath)
+                        $sizes_cache[fullpath] = { :x => pxb.width, :y => pxb.height }
+                    end
+                end
+            end
+            return $sizes_cache[fullpath]
         else
             return nil
         end