Today we will tell you how easily you can optimize your application. By making simple modifications you can achieve an increase of performance and reduce load on server. It is the first post to the topic of Ruby on Rails application performance. That’s why, we will study general and simple things concerning optimization of application functioning.
Let’s start studying this list.
It is necessary to avoid unnecessary repeated calculations. It especially concerns processing of big volumes of data. If you need to use a computed value many times, try to do with a one-time computation. It is better to cache a computed value and make repeated computations only in case of necessity.
If your application contains data not changing in the process of application work, then computation of these data should be made once – at the start of your application.
Ruby on Rails makes work with database easier and takes all low-level work on itself. However, it is not always optimal from the point of view of performance.
Suppose that at the execution of such a request:
Articles.find(:all, :conditions=>…)We receive N objects of Article class. Then we want to address to the author of the article via automatically generated accessors:
Arcticle.author.nameThen we get N queries more, which increases the load on database and application server. It is possible to avoid this using the following construction:
Articles.find(:all, :conditions=>… , :include=> :author)It will solve the problem of unnecessary queries to the DB.
As we know, helpers significantly simplify developer’s life. But, everything good should be within reasonable limits. For example, an excessive use of helpers for building links on pages can seriously reduce the speed of page display. Sometimes it is advisable to use HTML-tag to create a link. It will be faster from the point of view of application efficiency.
In conclusion we would like to note, that all advantages of Ruby on Rails should be used sensibly and without detriment to your application. In our next posts, we will touch the problem of Ruby on Rails application efficiency in detail. The post was prepared on the materials from InfoQ.