On Sun, Aug 13, 2006 at 05:35:04PM +0900, Thies C.  Arntzen wrote:
> * it cannot be implemented "as-cool" in a C-Extension cause it would
> not integrate "far" enough into ruby to have a real substantial value
> IMHO. (using Mauricios patch to eval would alow to implement it fully
> as an extension, but it would *slow* down *any* Hash creation in to

Not Hash.new ;)

> engine - so it's not an option)

Right. Here are some times just for fun; the overhead is around 50%:

$ cat $MESS/time-hash.rb

def time(exec, code = "1000000.times{ {1,1,1,1,1,1} }")
  `/usr/bin/time #{exec} -e "#{code}" 2>&1 `[/(\d\.\d+)user/, 1].to_f
end

RUNS = 5
base = time("./ruby", "1000000.times{}")
a = (0...RUNS).map{ time("./ruby.orig") - base }
b = (0...RUNS).map{ time("./ruby") - base}
ma = a.inject{|s,x| s+x}/a.size
mb = b.inject{|s,x| s+x}/b.size
sa = Math.sqrt(a.inject(0){|s,x| s+x**2}/a.size - ma**2)
sb = Math.sqrt(b.inject(0){|s,x| s+x**2}/b.size - mb**2)
puts "orig     %4.2f <%4.2f>" % [ma, sa]
puts "patched  %4.2f <%4.2f> (#{(100*mb/ma).round}%%)" % [mb, sb]

$ ruby $MESS/time-hash.rb
orig     2.11 <0.01>
patched  3.10 <0.06> (147%)


> * it was not meant as a patch to ruby for people to use - i would much
> rather like to have this feature a default in upcoming versions of
> ruby. so, i posted it for discussion...

Maybe this thread should be moved to ruby-core.

> * sure, changing the core-classes is a delicate thing, but -heck- why
> not? a) improvment most often comes with change, and b) i have have
> hacked on laguages-cores (PHP) before;-)
                            ===
You could have kept that to yourself };-)                            

> regarding possible memory-usage and speed tradeoffs:
> * the memory footprint increase is tiny and we should be able to ignore
> that. rubys implemenation is way more memory efficient that eg PHPs and
> noone has complaint there yet.
> * the same goes for speed. the lookup-speed is untoched, update is
> untouched, insert is O(1) slower (a few more pointers), deletion is a
> tad slower (but that could be fixed). sidenote: i even think we could
> squueze a tiny bit more out of our current hash-table implementation.
> i don't believe that you will be able to find any benchmark to prove
> this to slow down or bloat ruby in any way...

Your patch is similar to
http://www.rubyist.net/~nobu/ruby/ordered-hash.diff
which was not applied due to concerns about the memory overhead...

-- 
Mauricio Fernandez  -   http://eigenclass.org   -  singular Ruby