home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12372 < prev    next >
Encoding:
Text File  |  1992-08-14  |  9.5 KB  |  192 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.mentorg.com!bcannard
  3. From: bcannard@hppcb36.mentorg.com (Bob Cannard @ PCB x5565)
  4. Subject: Re: Garbage Collection for C++
  5. Originator: bcannard@hppcb36
  6. Sender: news@news.mentorg.com (News User)
  7. Message-ID: <1992Aug15.074911.7965@news.mentorg.com>
  8. Date: Sat, 15 Aug 1992 07:49:11 GMT
  9. References: <1992Aug6.014619.2111@ucc.su.OZ.AU> <DAVEG.92Aug14194411@synaptx.synaptics.com>
  10. Nntp-Posting-Host: hppcb36.mentorg.com
  11. Organization: Mentor Graphics 
  12. Keywords: 
  13. Followup-To: 
  14. Lines: 176
  15.  
  16.  
  17. In article <DAVEG.92Aug14194411@synaptx.synaptics.com>, daveg@synaptics.com (Dave Gillespie) writes:
  18. |> In article <1992Aug14.021547.15215@news.mentorg.com> bcannard@hppcb36.mentorg.com (Bob Cannard @ PCB x5565) writes:
  19. |> > In article <DAVEG.92Aug13025629@synaptx.synaptics.com>, daveg@synaptics.com (Dave Gillespie) writes:
  20. |> > |> Couldn't we get away with having a garbage collector that didn't
  21. |> > |> need pointers to be declared `GC'?  The only thing that would need
  22. |> > |> special `GC' treatment would be "new" [...]
  23. |> > 
  24. [my comments deleted]
  25. |> 
  26. |> The "GCnew" approach means you're marking objects (rather than classes
  27. |> or pointers) as GC-able.  It's still true that non-GC-allocated objects
  28. |> would work just like they always did.
  29. |> 
  30. [my comments deleted]
  31. |> 
  32. |> So you use "GCnew" to allocate objects of those classes, and "new"
  33. |> elsewhere.
  34.  
  35. I have two problems with this.
  36.  
  37. The first is that the GC is forced to check every pointer to see if it might
  38. point to a GC object, and the compiler is required to provide the necessary
  39. information that identifies all these pointers. The GC is forced to check many
  40. more pointers than is necessary (I estimate that in my application, roughly
  41. 90% of the objects would be non-GC). The programmer knows damn well that these
  42. pointers are irrelevant to the GC, and I get very annoyed when I can't pass
  43. that information on to the compiler.
  44.  
  45. The second is that having to test for GCness at run time represents an
  46. additional overhead for the GC which I'd rather avoid.
  47.  
  48. In fact, I'm forced to wonder: if the GC has to figure out the difference
  49. at run time, would it not be more efficient to simply make everything GCable?
  50. It looks to me as if there is no point in having GC and nonGC coexist, unless
  51. the two can be distinguished at compile time.
  52.  
  53. |> The "extra treatment" that GC classes would need basically involves
  54. |> adding some kind of tag information.  If you declare an entire class
  55. |> to be GC, it makes sense to put such a tag in every instance of the
  56. |> class.  If you instead work by allocating regular classes in a
  57. |> special GC-able way, then it makes more sense to put the tag only
  58. |> in instances of the class that are in the GC heap.
  59.  
  60. That looks wrong to me. Suppose I have a pointer to some object. The GC
  61. comes along and finds this pointer. It must first determine whether or not
  62. the object is a GC object or a non-GC object. It must then find the tag. If
  63. the object is part of an array, the tag could be anywhere. How the hell does
  64. the GC figure out where the tag is? How does it even tell that the object is
  65. inside an array? The object could contain an index to show which element of the
  66. array it is, but it would be cheaper to tag each element individually.
  67.  
  68. |> I still think declaring pointers to be GC would be too much of a
  69. |> hassle for the programmer
  70.  
  71. ???? You've got to be kidding! You can cope with const (or haven't you got
  72. cfront 3.0 yet - heh heh - evil chortle) but not with GC? If you can't cope
  73. with hassle, you're in the wrong language! :-)
  74.  
  75. |> , so we're left having to assume all
  76. |> pointers may point to GC-allocated objects.  Without adding lots of
  77. |> tags in places that make C compatiblity awkward, we're left with a
  78. |> completely "conservative", i.e., brute-force GC algorithm.  We scan
  79. |> every word on the stack, every word in static storage, and every
  80. |> word on the non-GC heap for things that look like GC pointers.
  81. |> (Since this is slow, a good optimization might be to separate the
  82. |> static area and possibly the non-GC heap into things-which-contain-
  83. |> pointers and things-which-don't, like massive arrays of doubles
  84. |> which would be a waste of time to scan.)
  85.  
  86. Probably worse than the v*rd*mmt reference counting that I'm already using,
  87. especially if the GC part of the data structure is relatively small. Blech!
  88. You really want all that inescapable overhead in order to save a bit of hassle?
  89.  
  90. [interesting stuff deleted for brevity]
  91. |> To the user this scheme would feel exactly like C++ does now, with
  92. |> no loopholes as far as I can tell.  The only change is that you
  93. |> are allowed to say "GCnew" instead of "new", on any kind of data,
  94. |> in which case that data gets deleted automatically as soon as there
  95. |> are no pointers of any kind to it left.  Very clean, very simple
  96. |> (at least where it counts---to the C++ user).
  97.  
  98. What counts is the *end user*, the customer, the person who buys the
  99. program. They don't care if it's clean and simple, they care that it does
  100. the job, does it fast, and doesn't blow up. They notice when programs get
  101. slower, and they complain. If necessary, by buying from a competitor.
  102.  
  103. [well-deserved and thorough trashing of reference counting deleted, along
  104. with other interesting stuff]
  105.  
  106. |> And, "no passing pointers around to Xlib, because Xlib was written
  107. |> before GC pointers were invented."
  108.  
  109. Ahem. This is where the "promise not to do anything clever" stuff comes in.
  110. Just like string.h was invented before const, all it needed was to update the
  111. _declarations_: the _definitions_ could still be in C, Mongolian Cobol
  112. or whatever, and would not have to change. If a function can't make that
  113. promise, you can't give it a GC pointer. Them's the breaks.
  114.  
  115. |> You might be able to squeak out of it by noting that "printf" is not
  116. |> going to invoke a GC since only "GCnew" calls can do that, and "printf",
  117. |> being non-GC-aware, will not call "GCnew".
  118.  
  119. Printf can make the promise for exactly the reasons you give.
  120.  
  121. |> The only two remaining
  122. |> tricky points then are existing functions with callbacks to code that
  123. |> can do a GC (like X toolkits, say), and signal handlers that invoke
  124. |> a GC (which might have to be forbidden for all sorts of other reasons).
  125.  
  126. The callback problem might be another side of the promise. The signals
  127. I'm not sure about, but how do they differ from, say, doing a "new" or
  128. "delete" in a signal handle, which might be invoked while the program
  129. is in the middle of a "new" or "delete"? Lots to think about here.
  130.  
  131. [DANGER shears at work]
  132.  
  133. [comments about GC objects referring to ordinary ones]
  134.  
  135. |> This is the old when-are-temporaries-deleted issue.  At least there it
  136. |> can be resolved in some definitive way, by specifying in the language
  137. |> when temporaries are deleted (say, only on statement boundaries or
  138. |> whatever).  With GC you don't have this luxury because GC's happen so
  139. |> unpredictably.  (Especially if signal handlers can invoke GC!)
  140.  
  141. I think it only becomes a serious problem if a GC object refers to a non-GC
  142. object which refers to a GC object which... The non-GC object must not be
  143. deleted before the GC object gets collected, otherwise the GC is going to get
  144. soundly screwed even if it is conservative; yet the programmer has no way
  145. of ensuring that the GC object has already gone. Alternatively, the GC object's
  146. destructor deletes the non-GC object, and the programmer holds on to a pointer
  147. to the non-GC object until it's safe to get rid of it. Hmmm...
  148.  
  149. |> > |> What if a pointer to a GC-able object is passed to foreign (non-C++)
  150. |> > |> code, which stashes it somewhere the C++ collector doesn't know to
  151. |> > |> look?  (All garbage-collecting languages have this problem, and as
  152. |> > |> far as I know all they can do is shrug and warn programmers not to
  153. |> > |> let go of the last C++ reference to an object if they know there
  154. |> > |> might be non-C++ references still lurking around.)
  155. |> 
  156. |> > Hence the proposal for tagging GC-able objects, and having something
  157. |> > comparable to "const" which declares "I will not do any clever tricks
  158. |> > with this pointer, including converting it to an integer, performing
  159. |> > pointer arithmetic on it, storing it, etc". This only needs a change
  160. |> > to the C++ declarations of library functions, not to the functions
  161. |> > themselves. IMO this makes it feasible to mix GC and libraries.
  162. |> 
  163. |> I don't see how either tagging or declaring things helps here.  I
  164. |> didn't mean "what if some novice incorrectly stashes away a pointer
  165. |> to a GC object," I meant "what if someone *needs* to pass a pointer
  166. |> to a GC object to a function which will stash it away where the GC
  167. |> can't find it."
  168.  
  169. I wasn't talking about novices either, I was talking about detecting situations
  170. where things have gone wrong, and informing the programmer so that corrective
  171. action can be taken. To be honest, with the availability of static,
  172. automatic, and non-GC allocated data structures, I'm not sure that this
  173. will ever be an insurmountable barrier, because of the ability to make a
  174. permanent copy, _provided_ the compiler can give warning when it happens.
  175.  
  176. If there *are* situations where it is unavoidable, we are going to need a
  177. locking mechanism. I'll hold off judgement on that for the time being.
  178.  
  179. [snip]
  180. |>                                 -- Dave
  181. |> --
  182. |> Dave Gillespie
  183. |>   daveg@synaptics.com, uunet!synaptx!daveg
  184. |>   or: daveg@csvax.cs.caltech.edu
  185.  
  186. Thanks, Dave, interesting stuff. Here's hoping that all this discussion will
  187. eventually get hammered into concrete proposals for a viable system!
  188. -- 
  189. bob_cannard@mentorg.com         "Human beings? ... Well, I suppose they are a
  190.                                  form of life, even if they are unspeakable"
  191. Exprssed opinions are not necessarily those of Mentor Graphics Corporation.
  192.