Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Symbols vs. Strings - Performance Loss by switching back and forth?

So while a ruby String is globally unique, a ruby Symbol is not, because it's stored by id somewhere in the background:

http://thoughtsincomputation.com/posts/ruby-symbols-what-are-they

...which means using symbols use less memory, given that you have a specified set of values the symbol can be (you don't want to turn every string of user-entered text into a symbol for example).

My question is, while there is definitely a performance benefit of using symbols, is it worth it in the end? I'm asking because, for example, I write most of my hashes using symbols:

my_hash    = {:_id => "123", :key => "value"}

But in the ruby-mongo-driver, all the keys are returned as strings:

mongo_hash = {"_id" => "123", "key" => "value"}

So if I were to use both my hash and the mongo hash the same way, I'd have to do:

mongo_hash = Model.collection.find_one(:key => "value")
#=> {"_id" => "123", "key" => "value"}
mongo_hash.symbolize_keys!
#=> {:_id => "123", :key => "value"}

But that just adds another layer to the application:

  1. Create a bunch of strings for the keys in memory.
  2. Now create a symbol for each string (already in memory after the first time this is done).
  3. Now destroy the strings we just created.

It seems like something's wrong there... either everything should be symbols or strings, there shouldn't be conversion back and forth, at least in this case.

What do you think about this? Is this okay? Is this even a problem, or is the ruby garbage collector and all that (haven't gone there yet) okay with this?

like image 311
Lance Avatar asked May 01 '11 03:05

Lance


1 Answers

Unless you're seriously pushing the constraints of your server/system, the benefits or drawbacks of either method are going to be negligible.

When using a library that absolutely requires that you give it a string-keyed hash, it is obviously better to simply use strings, as it keeps your code clear and concise, and eliminates the need for you to cast the keys to strings.

Ruby aims to make programming more enjoyable for the developer, and makes no claim to be the most efficient. When I use Ruby, I take this to heart and use Symbols for the keys in my hashes simply because it makes them easier to read.

When it comes down to it, it's personal preference, and you won't notice a speed increase/decrease either way. Unless you're running into speed/memory constraint issues, you've got nothing to worry about. Other parts of the Ruby standard library will begin to fall apart before this becomes an issue.

"Premature optimization is the root of all evil" -- Donald Knuth

like image 175
Mike Trpcic Avatar answered Oct 26 '22 23:10

Mike Trpcic