This came up recently, could be relevant:
http://engineering.gomiso.com/2011/05/16/if-youre-using-to_json-youre-doing-it-wrong/

The upshot is that they built this:
https://github.com/nesquena/rabl

Cheers,
Paul

On Tuesday, 24 May 2011 at 6:09 PM, Lachie wrote: 
> Hey Samuel
> 
> On Tue, May 24, 2011 at 4:42 PM, Samuel Richardson <[email protected]> 
> wrote:
> > Hi List,
> > I've been struggling with a particular dynamic bit of JSON rendering that's
> > fairly slow on my website. A typical render would be:
> > Completed 200 OK in 1408ms (Views: 279.4ms | ActiveRecord: 148.8ms | Sphinx:
> > 0.0ms)
> 
> Looks like you're dumping out whole Genre models with lots of
> descriptive text, plus their children (e.g. medias including their
> descriptive text).
> However, in that list you're only using a couple of things from them
> (name, slug).
> 
> This is resulting in ~250kb transferred for a list of 10 links. If
> nothing else, cutting down the amount of json you're encoding and
> sending will speed things up immensely.
> 
> In your Genre model, you could override #as_json to send only the
> fields you need. YMMV, especially when you need different json in
> different situations, but it seems worth it here!
> 
> 
> > The view and ActiveRecord aren't too bad but the total time is awful. Nearly
> > 1.5 seconds. I thought for a while that it might have been my JSON encoding
> > so I added the "yajl-ruby" gem to my Gemfile under the assumption that Rails
> > would pick this up and use it automatically (it uses C bindings instead of
> > pure Ruby for encoding) but it didn't seem to have an effect.
> > I've also bumped my Rails version to 3.0.7, using MySQL in the backend
> > hoping that later point release might have helped.
> 
> I'm not totally sure where the discrepancy is coming from, but
> serialising and sending 250k of json will take a while no matter what
> :)
> 
> You can verify your json backend by inspecting
> ActiveSupport::JSON.backend
> 
> If you want to make sure that Rails really is picking up yajl, set it
> (e.g. in an initialiser) with
> ActiveSupport::JSON.backend = 'yajl'
> 
> 
> hth
> :lachie
> 
> > If you want to see the behaviour in action, you can load up:
> > http://beta.knowyourgenre.com and have a play flicking backwards and
> > forwards in the popular genres section down the bottom of the page. You'll
> > also notice that the unpopular genres section, which has no songs, is quite
> > a bit faster, so whatever is causing it is related to the number of songs.
> > I've dumped out a log of the queries etc used to generate that chunk. For
> > all I know that could be the problem but I don't think it is due to AR only
> > report 100ms or so to run.
> > www.richardson.co.nz/render.txt
> > What I'm after here is what should I be looking at for debugging? JSON
> > encoding, slow ActiveRecord or something else?
> > 
> > Samuel Richardson
> > www.richardson.co.nz | 0405 472 748
> > 
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Ruby or Rails Oceania" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/rails-oceania?hl=en.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rails-oceania?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to