Skip to content
sdsykes edited this page Sep 13, 2010 · 5 revisions

Slim Attributes

Slim Attributes boosts speed in Mysql/Rails ActiveRecord Models by avoiding instantiating Hashes for each result row, and lazily instantiating attributes as needed.

How fast is it?

Notice that the speed relative to using plain ActiveRecord depends on how many attributes are accessed in the model objects (because slim_attributes lazily instantiates them into strings).

Overview

Slim-attributes is a small patch to the ActiveRecord Mysql adaptor that stops rails from immediately making ruby strings from the column names and data from your database queries. Because you probably don’t need them all!

So ruby strings are lazily created on demand – it’s faster and uses less memory. And it drops directly in, requiring only the installation of a gem and adding 1 line to environment.rb.

Measuring with just ActiveRecord code – fetching stuff from the database – we see anything from very little up to a 50% (or more) speed increase, but it really depends on your system and environment, and what you are doing with the results from the database. The more columns your tables have, the better the improvement will likely be. Measure your own system and send me the results!

Installation

Try:

gem install slim-attributes — -with-mysql-config

or:
gem install slim-attributes

then add this to environment.rb:
require ‘slim_attributes’

Note that the latest ‘edge’ version is available from github:
sudo gem install sdsykes-slim-attributes —source http://gems.github.com/ - —with-mysql-config

Description

Normally the mysql adaptor in Rails returns a hash of the data returned from the database, one hash per active record object returned by the query. The routine that generates these hashes is called all_hashes, and this is what we replace. The reason for overriding all_hashes is threefold:

  • making a hash of each and every row returned from the database is slow
  • ruby makes frozen copies of each column name string (for the keys) which results in a great many strings which are not really needed
  • we observe that it’s not often that all the fields of rows fetched from the database are actually used

So this is an alternative implementation of all_hashes that returns a ‘fake hash’ which contains a hash of the column names (the same hash of names is used for every row), and also contains the row data in an area memcpy’d directly from the mysql API (which is much faster than creating ruby strings).

The field contents are then instantiated into Ruby strings on demand – ruby strings are only made if you need them. Note that if you always look at all the columns when you fetch data from the database then this won’t necessarily be faster that the unpatched mysql adapter. But it won’t be much slower either, and we do expect that most times not all the columns from a result set are accessed.

The returned objects (of class Mysql::Result::RowHash) quack almost exactly like hashes – if you do anything other than fetch or set an attribute then a real hash will be instantiated, and any methods called will be put to work on that.
So this should be safe to use even if you access @attributes directly from your code.

No warranty – this gem has been well tested, but not in all environments. We are using it in our production environment with good results.

Copyright 2008-2009 Stephen Sykes sdsykes at g m a i l dot c o m

Clone this wiki locally