home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12954 < prev    next >
Encoding:
Text File  |  1992-08-26  |  7.1 KB  |  143 lines

  1. Path: sparky!uunet!synaptx!synaptics.com!daveg
  2. From: daveg@synaptics.com (Dave Gillespie)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Garbage Collection for C++
  5. Message-ID: <DAVEG.92Aug27002517@synaptx.synaptics.com>
  6. Date: 27 Aug 92 07:25:17 GMT
  7. References: <DAVEG.92Aug17224359@synaptx.synaptics.com> <boehm.714158406@siria>
  8.     <DAVEG.92Aug20022559@synaptx.synaptics.com>
  9.     <1992Aug25.183619.9541@microsoft.com>
  10. Sender: daveg@synaptx.Synaptics.Com
  11. Organization: Synaptics, Inc., San Jose, CA
  12. Lines: 128
  13. In-reply-to: jimad@microsoft.com's message of 25 Aug 92 18:36:19 GMT
  14.  
  15. In article <1992Aug25.183619.9541@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
  16.  
  17. > In article <DAVEG.92Aug20022559@synaptx.synaptics.com> daveg@synaptics.com (Dave Gillespie) writes:
  18. > |Do you really think incremental or generational collectors are
  19. > |feasible in C++?
  20.  
  21. > Generational GC is certainly feasible in C++.  In practice one needs compiler
  22. > support for the implied "smart pointers", so that programmers don't have
  23. > to remember to use special pointer syntax everywhere.
  24.  
  25. (And in a later message Jim continues...)
  26.  
  27. > But such cases can already be well handled in C++ using reference counting
  28. > schemes.  Again, I see a Modula-3 like approach where classes of objects
  29. > are either GC or not GC -- and the GC class objects are movable.  Backwards
  30. > compatibility is maintained because adding GC'ed classes is a pure extension.
  31. > Moveable objects need to be supported not merely for GC, but for more 
  32. > interesting problems of distributed networks, persistence, ODBMS, etc.
  33.  
  34. Suppose you have a vector class which you wish to be garbage-collectable.
  35. A typical vector class includes a pointer to a C-style array of data.
  36. Question:  Is this array also allocated as a collectable object, or
  37. is it allocated the old-fashioned way (deleted explicitly by the
  38. vector object's destructor)?
  39.  
  40. If the underlying array is not collectable, then you are still plagued
  41. with dangling reference problems:  The program drops all its references
  42. to the vector itself, but retains a reference to the i'th element of the
  43. array.  Since this element is in storage unrelated to the vector as far
  44. as the GC is concerned, the vector gets deleted and you have a dangling
  45. pointer.  It would be really, really nice to use GC to sidestep this
  46. problem.  Every third posting in the destruction-of-temporaries thread
  47. in this group ends with "if only we had GC so we could solve this
  48. problem once and for all..."
  49.  
  50. If the underlying array *is* collectable, then it must be of a collectable
  51. type.  That means it's not just class types that must be able to be
  52. collectable, but *all* C++ types.  C's array type is slippery enough
  53. already---are you sure you can pin a "collectable" property on it
  54. without breaking too much else?
  55.  
  56. If only class objects could be collectable, then only pointers to those
  57. classes would need special, slow pointer-dereferencing semantics with
  58. a generational collector.  If everything is collectable, you'd probably
  59. have to slow down pointer dereferencing for everything---not acceptable!
  60.  
  61. I still haven't seen anybody propose a way to do generational GC without
  62. having magic pointers (at least not without falling back on magic memory
  63. systems, which not all hardware can provide).
  64.  
  65.  
  66. Also, if you allow GC to move your objects, then you need a 100%
  67. foolproof way for the GC to identify exactly the set of words in memory
  68. which represent pointers to the moved objects.  You can't use the
  69. so-called "conservative" GC algorithms, because they can't tell the
  70. difference between a legitimate pointer and an integer that happens
  71. to look like a pointer.
  72.  
  73. Without conservative GC, you need RTTI for everything (including
  74. struct types which must be structurally compatible with C structs,
  75. unless you disallow pointer to GC objects in any struct that doesn't
  76. have any C++ features).  Your compiler needs to record enough
  77. information to allow the GC to figure out where the pointers are
  78. in every stack frame.  You need to initialize all pointers all the
  79. time.  An optimizing compiler will need to record a table showing,
  80. for every instruction in the program, which registers contain
  81. pointers and which contain integers at that moment.  All of these
  82. things carry unavoidable penalties that all code must pay for,
  83. whether it actually uses GC or not.  Also, existing libraries must
  84. be recompiled to include all this information.
  85.  
  86. Moving data during GC is only useful when you have arrays of data.
  87. When you have individual class objects, you generally have lots of
  88. things the of same size and simple per-size free lists are likely to
  89. be good enough, or at least malloc-style coalescing of adjacent free
  90. areas.
  91.  
  92. You could allow the GC to move only arrays of non-class objects
  93. which are declared specially as movable and which are accessed
  94. only through specially-declared pointers, but my guess is this
  95. would still be too much hassle to be worthwhile.
  96.  
  97.  
  98. > C++ needs to support moveable objects in any case, in order to handled
  99. > ODBMS, persistence, locality of reference, etc.  If you ever have done
  100. > store/restore of objects from moveable store,  you've had to handle the
  101. > problem of moveable objects.  This is a standard programming problem not
  102. > well handled by current C++.  It needs compiler support.  Immovable object
  103. > GC schemes do not solve this problem -- they simply leave it to someone else
  104. > to solve.
  105.  
  106. C++ does not "need" to support any of these things.  A proposal for
  107. adding GC to C++ has to be justifiable on its own grounds or there's
  108. no hope of it ever being adopted.  Locality of reference, for example,
  109. would be nice to optimize but it's not "required."  If there's nothing
  110. that can be done about it without movable objects, and movable objects
  111. are not practical in C++ for the reasons outlined above, then I guess
  112. there's no choice but to put locality of reference on the back burner.
  113.  
  114. Besides, storing and restoring objects is much different from just
  115. letting the GC move objects around in RAM, if I understand your terms
  116. correctly.  Store/restore happens at certain predictable times and the
  117. pointers being adjusted are likely to need identifying anyway; their
  118. very representation is likely to be changing.  GC is harder since it
  119. can happen at more or less any time, and can involve pointers all
  120. throughout an active program.  Store/restore is harder in a different
  121. way because of those changing representations.
  122.  
  123. There are plenty of features of other languages like Lisp that would
  124. be really nice to have in C++.  Arbitrary-size integers, for example.
  125. But I would be last to say that C++'s "int" type "needs" to be
  126. extended in this way; it would be nice, but it's just not practical
  127. for this language.  When I need arbitrary-size integers I use a
  128. special class, or I use Lisp.  If I need sophisticated GC, I'll
  129. probably also have to go to Lisp.  The question is whether there is
  130. a simple way to add a modest, unobtrusive GC that will be good
  131. enough for the vast majority of cases.
  132.  
  133. Stroustrup has warned against making the language topheavy with
  134. features.  Simple, conservative non-moving GC can be added with
  135. minimal effect to the rest of the language, and minimal danger of
  136. toppling it.
  137.  
  138.                                 -- Dave
  139. --
  140. Dave Gillespie
  141.   daveg@synaptics.com, uunet!synaptx!daveg
  142.   or: daveg@csvax.cs.caltech.edu
  143.