[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: collections with values of fixed classes/lengths

Eric Mahurin

5/3/2005 3:01:00 PM

> > But, you still wouldn't have to store the class info for
> each
> > element. I'm not sure of the underlying Ruby object data
> > structure, but at a minimum, it should get rid of a pointer
> (to
> > the class) per element. I think you'd have to implement
> this
> > in C to do it right.
>
> I don't think it's worth the effort. That must be a very
> special case where
> you need huge collections of inhomogenous data where you want
> to squeeze out
> every byte. I can't think of an application of this. Plus,
> it'll be error
> prone and very inefficient.


I just did some memory measurements to see how much various
things take up. I tested a million objects using this:

ruby -e 'mem0 = `ps -p #{Process.pid} -o rsz`.
split("\n")[1].to_i; a=Array.new(2**20);a.each_index{|i|
a[i]=<object> };mem1 = `ps -p #{Process.pid} -o rsz`.
split("\n")[1].to_i; puts(((mem1-mem0)/1024.0).to_s+"MB")'

Here are some results (object ref + object):

bytes object
----- ------
4 nil,false,true,Fixnum
30 String 0-1 characters
46 String 2-11 characters
54 String 12-19 characters (+8 bytes per +8 characters)
30 Float
30 Array 0 nil entries
46 Array 1-3 nil entries
54 Array 4-5 nil entries (+8 bytes per +2 entries)
102 Hash empty
126 Hash 1 Fixnum=>nil entry (+24 bytes per +1 entry)

These overheads look pretty large to me. That 30 byte overhead
(includes 4-byte object ref) is quite dominate for many cases.
In C, the overhead would be 12 bytes for strings and arrays
(pointer, length, malloc entry). An array of strings/arrays
where each had a fixed length could be implemented with an
overhead of 4 bytes (an index into a concatenated
string/array). An array of fixed length strings/arrays could
be implemented with no overhead per entry. Maybe it is worth
it to handle these last 2 cases (could be done in Ruby easily),
but not the completely variable length case (would probably
need C).


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail...