[Rubygems-developers] Why does an install command an update

Jim Freeze rubygems at freeze.org
Fri Jun 3 16:21:16 EDT 2005


Nice optimization Hugh. More than double the savings.

Here is a shorter version (don't know if it
is windows compatible though):

  --- ftest.rb	Fri Jun  3 15:12:01 2005
  +++ ftest2.rb	Fri Jun  3 15:10:58 2005
  @@ -18,13 +18,20 @@
   puts files.to_yaml.size
   
   def optimize_file_list(files)
  -  hash = Hash.new { |h,k| h[k] = [] }
  +  hash = {}
     files.each { |file|
  -    dir      = File.dirname(file)
  -    filename = File.basename(file)
  -    hash[dir] << filename
  +    next if File.directory?(file)
  +    a = file.split(/\//)
  +    rtn = a.inject(hash) { |h,k| h[k] ||= {} }
     }
  -  hash
  +  recurs = lambda { |h,k| 
  +    if h[k].empty?
  +      h[k] = nil 
  +    else
  +      h[k].each { |kk,vv| recurs.call(h[k], kk) }
  +    end
  +  }
  +  hash.each { |k,v| recurs.call(hash,k) }
   end
   
   files["files"] = optimize_file_list(files["files"])
  --- 
The output is:

  files: 
    - lib/cgikit
    - lib/cgikit.rb
    - lib/cgikit/components
    - lib/cgikit/components/CKErrorPage
    - lib/cgikit/components/CKErrorPage/CKErrorPage.ckd
    - lib/cgikit/components/CKErrorPage/CKErrorPage.html
    - lib/cgikit/components/CKErrorPage/CKErrorPage.rb
  271
  --- 
  files: 
    lib: 
      cgikit.rb: 
      cgikit: 
        components: 
          CKErrorPage: 
            CKErrorPage.ckd: 
            CKErrorPage.html: 
            CKErrorPage.rb: 
  174

I get the same 36% savings as you did, so it looks to be the same.


* Hugh Sasse <hgs at dmu.ac.uk> [2005-06-03 19:22:30 +0100]:

> >A slight savings of 15%.

> diff -u files_in_rubygems.rb.orig files_in_rubygems.rb
> --- files_in_rubygems.rb.orig   Fri Jun  3 18:37:50 2005
> +++ files_in_rubygems.rb        Fri Jun  3 19:17:02 2005
> @@ -12,6 +12,7 @@
>    }
> 
>    require 'yaml'
> +  require 'pathname'
> 
>    puts files.to_yaml
>    puts files.to_yaml.size
> @@ -18,10 +19,26 @@
> 
>    def optimize_file_list(files)
>      hash = Hash.new { |h,k| h[k] = [] }
> +    newhash = nil
>      files.each { |file|
> -      dir      = File.dirname(file)
> -      filename = File.basename(file)
> -      hash[dir] << filename
> +      path = Pathname.new(file)
> +      key = nil
> +      # move down the pathname....
> +      path.each_filename do |name|
> +        if key.nil?
> +          key = name
> +          hash[key] = nil unless hash.has_key?(key)
> +          newhash = hash
> +        else
> +          if newhash[key]
> +            newhash[key][name] ||= nil
> +          else
> +            newhash[key] = {name => nil}
> +          end
> +          newhash = newhash[key]
> +          key = name
> +        end
> +      end
>      }
>      hash
>    end
> @@ -29,3 +46,5 @@
>    files["files"] = optimize_file_list(files["files"])
>    puts files.to_yaml
>    puts files.to_yaml.size
> +
> +# vim:set sw=2 et:
 
> 271
> 174


-- 
Jim Freeze
Theory and practice are the same, in theory. -- Ryan Davis


More information about the Rubygems-developers mailing list