{"id":80,"date":"2006-02-18T10:50:44","date_gmt":"2006-02-18T14:50:44","guid":{"rendered":"http:\/\/benjamin.smedbergs.us\/blog\/2006-02-18\/xpcom-detecting-reference-cycles-or-switching-to-gc\/"},"modified":"2006-02-18T11:12:37","modified_gmt":"2006-02-18T15:12:37","slug":"xpcom-detecting-reference-cycles-or-switching-to-gc","status":"publish","type":"post","link":"http:\/\/benjamin.smedbergs.us\/blog\/2006-02-18\/xpcom-detecting-reference-cycles-or-switching-to-gc\/","title":{"rendered":"XPCOM: Detecting Reference Cycles, or Switching to GC?"},"content":{"rendered":"<p><a href=\"http:\/\/weblogs.mozillazine.org\/roadmap\/\">Brendan<\/a> proposes to <a href=\"http:\/\/weblogs.mozillazine.org\/roadmap\/archives\/009727.html\">break XPCOM reference cycles using cycle-detection algorithms<\/a>.<\/p>\n<p>I must admit that I&#8217;m skeptical: in order to break reference cycles you need to know what strong references an object is holding: to do that you either have to ask the object (which must implement a special interface), or you have to have special knowledge about the internal form of an object (presumably provided through some specialization of classinfo).<br \/>\nIMHO it would be a better use of our time and energy to develop a mark-and-sweep garbage collection for XPCOM objects:<!--more--><\/p>\n<pre>interface nsIGCThing\r\n{\r\nvoid mark();\r\nvoid QueryInterface(in nsIIDRef uuid, [iid_is(uuid),retval] out nsQIResult result);\r\n};<\/pre>\n<p>Note that this interface does not inherit from nsISupports! It is a parallel interface hierarchy built on a mark-and-sweep GC model.<\/p>\n<p>Each class would have a per-class object to perform &#8220;sweeping&#8221;:<\/p>\n<pre>interface nsIGCParticipant : nsIGCThing {\r\nvoid sweep();\r\n};<\/pre>\n<p>XPCOM would implement the following service which would be used to initialize GC:<\/p>\n<pre>interface nsIGCService : nsISupports {\r\nvoid GC();\r\nvoid registerGCParticipant(nsIGCParticipant);\r\nvoid unregisterGCParticipant(nsIGCParticipant);\r\nvoid registerGCRoot(nsIGCThing);\r\nvoid unregisterGCRoot(nsIGCThing);\r\n};<\/pre>\n<p>A typical DOM object would continue to implement both the existing frozen nsISupports-based interfaces (for backwards compatibility with existing code) as well as parallel GC-based interfaces. The reference count and the mark-sweep indicator could be combined as a union on a single word: if the refcnt was above zero, the object is automatically a root and would be marked during mark phase of collection.<\/p>\n<p>This system has the potential to greatly simplify a lot of our DOM and docshell code; it has the additional advantage that it can be completed incrementally. In addition, xpconnect wrappers (and hopefully javaxpcom and pyxpcom wrappers) can participate in a unified GC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brendan proposes to break XPCOM reference cycles using cycle-detection algorithms. I must admit that I&#8217;m skeptical: in order to break reference cycles you need to know what strong references an object is holding: to do that you either have to ask the object (which must implement a special interface), or you have to have special [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-mozilla"],"_links":{"self":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":0,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"wp:attachment":[{"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/benjamin.smedbergs.us\/blog\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}