Feature #5123

Alias Hash 1.9 as OrderedHash

Added by Alexey Muranov over 2 years ago. Updated over 1 year ago.

[ruby-core:38623]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:next minor

Description

I read that it was a controversial decision to make Hash in Ruby 1.9 ordered.
It is not clear if the present implementation is the best possible.
I would like to express my thought.

It would be nice if the ordered Hash in Ruby 1.9 was aliased as OrderedHash.
That way people who rely on preserving the insertion order in some application (me, for example) could explicitly use OrderedHash, and developers of Ruby would be free to redefine Hash in future versions if a better implementation that a doubly-linked circular list is found.
(I read something about a possibility of using "red–black tree".)

Thanks.

Alexey Muranov.

History

#1 Updated by Kazuhiro NISHIYAMA over 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

#2 Updated by Michael Edgar over 2 years ago

As a corollary, for a few months I had one portion of my code which used Sets, and relied on the fact that Sets in 1.9 retain their order because they in turn use a Hash. The only real way I could document this requirement was to put comments in capital letters in the relevant portions of code so that I didn't forget that I relied on this behavior.

If I had been creating "OrderedSets" (though this sounds too close to SortedSet despite meaning something different), and OrderedSets use an OrderedHash, then it would have been clear in the code that insertion order was preserved, especially for developers new to 1.9.

I like this idea.

#3 Updated by Thomas Sawyer over 2 years ago

IWon't this break orderedhash gem?

In any case, now that Hash preserves order, why would anyone ever think about changing it back and break old code?

#4 Updated by Anurag Priyam over 2 years ago

It would be nice if the ordered Hash in Ruby 1.9 was aliased as OrderedHash.
That way people who rely on preserving the insertion order in some application (me, for example) could explicitly use OrderedHash, and developers of Ruby would be free to redefine Hash in future versions if a better implementation that a doubly-linked circular list is found.
(I read something about a possibility of using "red–black tree".)

This sounds like one of those variable names with a type prefixed to
it, like sequencestring, or sequencearray. They look ugly. It would
have mad more sense if Ruby were ever to ship with two different Hash
implementations. Besides, the aliasing can be done at the application
level too. So if the Hash implementation changes tomorrow, you are
free to re-alias to some other ordered hash implementation.

--
Anurag Priyam
http://about.me/yeban/

#5 Updated by Alexey Muranov over 2 years ago

Thomas Sawyer wrote:

IWon't this break orderedhash gem?

In any case, now that Hash preserves order, why would anyone ever think about changing it back and break old code?

To improve performance, for example, or to restore the original meaning of "hash".

Alexey Muranov.

#6 Updated by Alexey Muranov over 2 years ago

Anurag Priyam wrote:

Besides, the aliasing can be done at the application
level too. So if the Hash implementation changes tomorrow, you are
free to re-alias to some other ordered hash implementation.

But then i will have to edit my code to upgrade to a new version of Ruby.

Alexey Muranov.


Update 2011-11-09. "It would have made more sense if Ruby were ever to ship with two different Hash implementations." -- Anurag Priyam
Why not? Implementing Hash and OrderedHash separately some day (50 years from now) may gain some speed for pure Hash.

Update 2011-12-04. This is where i first read about red-black trees for hash: http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/

Update 2012-05-04. I still like my original proposal because the use of OrderedHash constant documents the behavior, but if it is not accepted, another way to use unordered "faster" hashes would be to call them OptimizedHash, and have a method Hash#optimized for conversion:

a = {}.optimized

With the original proposal, the method would be Hash#ordered:

b = {}.ordered

#7 Updated by Yutaka HARA over 1 year ago

  • Category set to core
  • Target version set to next minor

#8 Updated by Charles Nutter over 1 year ago

The decision is already made. Hash has been ordered since 1.9.1 (or 1.9.2?) and changing it now would break all code that expects ordering. Ordering also does not necessarily limit options for implementation...it just makes certain implementations less efficient.

There's a separate bug still being evaluated to add RBTree to core...perhaps that would fulfill what you need?

#9 Updated by Alexey Muranov over 1 year ago

I was only proposing an alias for now. Maybe OHash, OSet?

Also available in: Atom PDF