« Return to Thread: problem of feedtools with jruby

Re: problem of feedtools with jruby

by Michael Guterl :: Rate this Message:

Reply to Author | View in Thread

On Wed, Jul 23, 2008 at 12:56 AM, Yin AnPing <yapex.yin@...> wrote:

> another way to run it
> thanks everybody !
> ================================
> require 'rubygems'
> require "feed_tools"
> require 'jruby'
>
> JRuby.objectspace=true
>
> URL = "http://www.marstoday.com/rss/mars.xml"
> FeedTools.configurations[:feed_cache] = nil
> feed = FeedTools::Feed.open(URL)
> puts "Description: #{feed.title}\n"
>
> feed.entries.each {|item| puts item.title}
> =================================

I don't mean to hijack this thread and we can move it elsewhere if
necessary, but I am surprised to see such a "simple" library using
ObjectSpace.each_object.  I know the JRuby team made the decision to
turn ObjectSpace.each_object off by default because of a large
performance hit and the fact that ObjectSpace.each_object is seldom
used.

If I recall correctly test/unit used to use each_object and it was
able to be patched to avoid using it entirely, anyone remember if this
is correct?

The offending method and author's comment prior to it are listed at
the bottom of this email and at http://pastie.org/242073.

I guess I could ask elsewhere, but I do not understand the author's
motivation described in the comment below.  I am assuming there is a
simple hierarchy between feeds and feed items with a feed being the
parent of a feed item.  I am curious what type of havoc storing the
parent in the feed item could cause with GC?

Storing a reference to the parent object of a child object in a
hierarchy seems like a fairly common thing to do, is it not?

Also, I know that JRuby uses a different GC than MRI, would JRuby's GC
mechanism be less likely to experience the pitfalls (which I am still
trying to understand) than MRI?

I just guess I am surprised to see ObjectSpace.each_object used in
such a simple situation.  I always assumed most use cases could be
implemented using some other means.

Thanks,
Michael Guterl

<<-CODE

# Returns the parent feed of this feed item
# Warning, this method may be slow if you have a
# large number of FeedTools::Feed objects.  Can't
# use a direct reference to the parent because it plays
# havoc with the garbage collector.  Could've used
# a WeakRef object, but really, if there are multiple
# parent feeds, something is going to go wrong, and the
# programmer needs to be notified.  A WeakRef
# implementation can't detect this condition.

def feed
  parent_feed = nil
  ObjectSpace.each_object(FeedTools::Feed) do |feed|
    if feed.instance_variable_get("@entries").nil?
      feed.items
    end
    unsorted_items = feed.instance_variable_get("@entries")
    for item in unsorted_items
      if item.object_id == self.object_id
        if parent_feed.nil?
          parent_feed = feed
          break
        else
          raise "Multiple parent feeds found."
        end
      end
    end
  end
  return parent_feed
end

CODE

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: problem of feedtools with jruby

LightInTheBox - Buy quality products at wholesale price!