# Marshaled data ordering inconsistent

**URL:** <https://www.ruby-forum.com/t/marshaled-data-ordering-inconsistent/120755>\
**Category:** Ruby\
**Created:** [November 20, 2007, 11:07am UTC](https://www.ruby-forum.com/t/marshaled-data-ordering-inconsistent/120755 "2007-11-20T11:07:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@mla](https://www.ruby-forum.com/u/mla)\
**Post date:** [November 20, 2007, 11:07am UTC](https://www.ruby-forum.com/t/marshaled-data-ordering-inconsistent/120755/1 "2007-11-20T11:07:34Z")

</div>

It seems that when you marshal hashes the binary representations aren’t  
stable.

This is an issue with PStore because it uses the md5 of the marshaled  
data to decide whether it’s changed and whether a disk write is needed.

Perl’s serialization module, Storable, has a $Storable::canonical option  
which, if true, sorts the keys of hashes. Would it be reasonable to add  
such an option to Marshal?

I read that Ruby 1.9 will have ordered hashes. So maybe that will make  
the issue moot?

Script that illustrates the issue:  
[http://mla.homeunix.com/tmp/marshal-test.rb](http://mla.homeunix.com/tmp/marshal-test.rb)

Output that I see:  
[http://mla.homeunix.com/tmp/marshal-test.txt](http://mla.homeunix.com/tmp/marshal-test.txt)

ruby 1.8.6 (2007-11-18 patchlevel 5000) [i686-linux]

---

<div class="post-metadata">

**Author:** ![mla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b77776/32.png) [@mla](https://www.ruby-forum.com/u/mla)\
**Post date:** [November 20, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/marshaled-data-ordering-inconsistent/120755/2 "2007-11-20T13:40:34Z")

</div>

2007/11/20, mla mla [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> It seems that when you marshal hashes the binary representations aren’t  
> stable.
> 
> This is an issue with PStore because it uses the md5 of the marshaled  
> data to decide whether it’s changed and whether a disk write is needed.
> 
> Perl’s serialization module, Storable, has a $Storable::canonical option  
> which, if true, sorts the keys of hashes. Would it be reasonable to add  
> such an option to Marshal?

Sounds good to me. What happens if keys are not sortable? Fallback  
to default, i.e. store unsorted?

> I read that Ruby 1.9 will have ordered hashes. So maybe that will make  
> the issue moot?

Probably not because not all applications need ordered Hashes.  
Usually ordering imposes an overhead that you might not want to pay.  
So I’d say even in 1.9 this issue is not (fully) gone.

Kind regards

robert
