From: Robert Klemme Date: 2006-08-13T07:25:09+09:00 Subject: Re: PATCH to make internal Hash class retain order... Thies C. Arntzen wrote: > Brian McCallister schrieb: > >> On Aug 12, 2006, at 9:55 AM, Thies C. Arntzen wrote: >> >>> Hi there - >>> >>> beeing new to ruby (thru rails) and coming from a strong php >>> background >> >> >>> * remember the order in which elements have been added to a Hash (at >>> the cost of 2 extra pointers in every hash-bucket + 2 extra pointers >>> per hash-table) >>> * use this order for iterating (st_foreach will use this order) >>> * implement a call Hash.order! allows the user to change the internal >>> order;-) >> Nice :-) The better place for discussion hacking on the ruby >> interpreter itself is probably ruby-core. > > i wasn't aware of that list;-) subscribed... > >> How difficult would it be to do an OrderedHash data structure as a C >> extension instead of in Ruby? > > i really believe that it needs to be a 1st class citizen, - the reason > it's done in C is because userland imposes too much overhead (i > believe) and converting forward<->back to the normal Hash would be > slow, memory intensive and ordering information would get lost on the > way... > > also the amount of code added to the Hash class is ~20 lines of C-code > - this code adds zero overhead. the bigger chunk of the code was added > to the st.c which is the underlying library for handling hash-tables - > and duplicating > 600 lines of code to add ~60 lines didn't seem very > cool to me;-) Adding to David's comments: you're changing semantics of one of the core classes which is in itself a dangerous thing. Plus, you increase the memory footprint of a hash which is IMHO a very bad idea for such a widely used data structure (and some of these hashes grow huge). So, if you want to patch ruby core, make it at least a subclass of Hash so people can choose. Then of course there's a whole bunch of other things that one should consider. Is the name properly chosen? Do we have to provide a means for the user to *define* the standard order on creation? This is e.g. the way Java's TreeSet works - and IMHO it's superior vs. having to call order! manually. etc. Kind regards robert