1

Working in rails 3.2.1, I am bilding an application which performs a large number of JSON requests and parses the returned data using Yajl. My dilemma is weather or not to initialize a Yajl parser object each time a JSON data is requested:

json = StringIO.new( some_jason_object)
#hmm I need something to parse this json data
parser = Yajl::Parser.new
hash = parser.parse(json)

or to initialize the parser a global constant PARSER = Yajl::Parser.new in config/initializers/yajl_parser.rb and call it from my application as follows:

json = StringIO.new( some_jason_object)
hash = PARSER.parse(json)

Given that requests requiring Yajl to parse data will be made on the majority of page request, which implementation offers the best performance.

rudolph9
  • 8,021
  • 9
  • 50
  • 80

1 Answers1

0

What is wrong with simply using Yajl::Parser.parse json_string? It shouldn't make much of a difference in this context.

omninonsense
  • 6,644
  • 9
  • 45
  • 66
  • My thought is by creating a constant or continuously reusing the an initialized object that my application may achieve an increase in performance as the object would likely then be cached in memory but I don't understand the orientation of Ruby or Ruby on Rails well enough to make an informed decision. – rudolph9 Mar 15 '12 at 23:04
  • I often have questions similar this one since adopting Ruby on Rails, I did several semesters doing work with HPC while in school and I would very much like to get a better understanding of how Ruby handels memory management, specifically in the context of a Ruby on Rails application. – rudolph9 Mar 15 '12 at 23:08
  • 1
    Hmm, I only rarely worked with Ruby on Rails, but if you're interested on how Ruby handles memory check [this presentation](http://timetobleed.com/garbage-collection-slides-from-la-ruby-conference/); if you use YARV it might be a little different than the presentation. `Yajl::Parser` already is a constant, to which you send `parse`. – omninonsense Mar 17 '12 at 01:50
  • Very helpful! From what I have gathered using the standard Ruby 1.87 interpreter, it will be slightly more efficient to initialize an object which is reused over calling `Yajl::Parser.parse json_string` as needed. My thought is 1.87 implementation is at best green threaded and this will prevent the `yajl::Parser` objects needing to be cleaned up every time `Yajl::Parser.parse json_string` is called. – rudolph9 Mar 17 '12 at 16:11
  • That being said, I intend to deploy my application with JRuby which has more efficient garbage clean up, runs truly parallel threads and will likely be faster calling `Yajl::Parser.parse json_string` as needed (although more work overall). – rudolph9 Mar 17 '12 at 16:11
  • Although long term I hope to come back to a standard Ruby interpreter, are you familiar with a talk, similar to one you originally recommended, that breaks down the memory management specifically for the 1.9 implementation? – rudolph9 Mar 17 '12 at 16:13
  • 1
    I wasn't able to find any good posts/documentation/presentations about memory management in YARV (unfortunately, since I would like to get my hands on them, too). The performance difference between sending `parse` to `Yajl::Parser`, or an instance of it is marginal. The instance version might be very slightly faster, but a little less convenient. – omninonsense Mar 17 '12 at 23:54