Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

advantage of ruby symbols over strings

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> The Mechanics of Ruby programming
View previous topic :: View next topic  
Author Message
umamahesh_nyros



Joined: 17 Jul 2007
Posts: 224
Location: KAKINADA

PostPosted: Tue Nov 03, 2009 10:58 am    Post subject: advantage of ruby symbols over strings Reply with quote

Symbols in Ruby plays an important role in performance issue of the application. Symbols in ruby is an great gift.

A symbol in Ruby is an instance of the class Symbol. symbol is defined by prefixing a colon with an identifier. :name, :id, :user etc.

Symbol class in Ruby contains one class method all_symbols and instance methods id2name, inspect, to_i, to_int, to_s and to_sym.

* all_symbols – Returns an array of all the symbols in Ruby’s symbol table.
* id2name – Returns the string representation of the symbol – :name.id2name returns “name”.
* inspect – Returns the symbol literal
* to_i – Returns an integer unique for each symbol
* to_int – Same as to_i
* to_s - Same as id2name
* to_sym – Convert the symbol to a symbol!

But the advantage in using symbols is the efficient use of memory. Maximum space taken by a symbol is never more than the space taken by an integer. This is because internally symbol is stored as an integer. In case of strings the memory space depends on the size of the string.

Also whenever a string is used in the program, a new instance is created. But for symbols, same identifier points to the same memory location! This can be easily checked by running the following,

The memory saved may look trivial in this case. But when a predefined hash key structure is used as parameter to a method and if the method is used many times in the program, the savings can be substantial! For example, in RoR a sample link is defined as,


The first parameter is the label used for the link and the second parameter is a hash. The second parameter is defined as a hash to enable dynamic parameters.

Now in the implementation of link_to method, a check is done to see if :action key exists in the hash and then corresponding value is used. Similarly :controller is also checked and if found is used. Hence using hashes is a good way to implement optional parameters.

In this example we could have used “controller” and “action” strings instead of symbols :controller and :action. But we will be missing two important advantages – efficient memory usage and speed of processing.

In a rails application, you might be using link_to hundreds of times. Assume that you have used it 10000 times in a big application. If we assume every call contained :controller and :action symbols, total memory required by the symbols = 2 * size of an integer. Whether we call link_to once or millions of times the memory taken remains same. But things would be different in case of strings as keys. Whenever a link_to is called 2 new strings “controller” and “action” will be created. Hence a total memory allocation of 10000*16 bytes would have done by Ruby interpreter (of course garbage collector will ensure memory usage at any point to be much less than this).

Second is the question of speed. We know that link_to internally compares hash key to :controller and :action. This is extremely fast since it is equivalent to comparing integers. But if we had used strings, comparison such as “controller”==”controller” is inefficient since it involves comparison of all the characters in it.

Strings are scoped and they get destroyed/garbage collected when the execution goes out of scope. But in case of symbols, they remain defined as long as the program is running. So if you have a very large number of symbols there could be a lot of unusable memory.
Back to top
View user's profile Send private message Send e-mail Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> The Mechanics of Ruby programming
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts validates_multiparameter_assignments plugin usage:
0 vijayalakshmi 27 Wed Jul 28, 2010 4:24 am
vijayalakshmi View latest post
No new posts paperclip plugin usage in rails:
0 vijayalakshmi 23 Wed Jul 28, 2010 4:07 am
vijayalakshmi View latest post
No new posts Polymorphism
1 phani.galla 46 Tue Jul 27, 2010 3:27 pm
umamahesh_nyros View latest post
No new posts Deploying Ruby on Rails Application with Passenger
2 sivakrishna.m 630 Tue Jul 27, 2010 2:16 pm
criether View latest post
No new posts Steps to implement star-rating using acts_as_rateable plugin
0 vijayalakshmi 46 Mon Jul 26, 2010 5:47 am
vijayalakshmi View latest post
No new posts Twitter Retweet Integartion
0 Anu 41 Thu Jul 22, 2010 12:27 pm
Anu View latest post
No new posts Usage of build method in rails
0 ktulasi 30 Thu Jul 22, 2010 12:19 pm
ktulasi View latest post
No new posts Facebook Like Integration in Rails
0 Anu 53 Thu Jul 22, 2010 12:16 pm
Anu View latest post
No new posts Fshare in your Rails Application.
0 Anu 39 Thu Jul 22, 2010 12:10 pm
Anu View latest post
No new posts Installation of Postgresql in windows.
1 swaminadhan 198 Thu Jul 15, 2010 10:17 am
proximity3 View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More